您的位置:首页 > 其它

[bigdata-008]将bson文件转储到hive[step by step]

2016-12-24 10:53 148 查看
(对若干名词进行修改,不能直接执行,仅作示意)

1. mongodb的数据导出为bson文件,例如a.bson。

 

2. mongodb提供一个工具bsondump,用它将bson文件转成json文件,命令为: bsondump a.bson > a.json

    a.json的每一行,是一个json格式的完整记录,比如:

    {"_id":{"$oid":"09f89b8bb2"},"name":"WX","pageUrl":"start","time":{"$date":"2016-09-21"},"event":"page","userId":null,"createTime":{"$date":"2016-09-21T08:47:07.271Z"}}

 

3. 用python3对a.json逐行读取,处理成想要的数据格式,然后写入到文本文件a.txt。a.txt将后面被导入到hive。示意性代码如下,其中process_data函数可以根据实际需要修改代码。

#!/usr/bin/env python
#! -*- coding:utf-8 -*-
import json

f_json = open('a.json', 'r')
f_txt = oepn('a.txt', 'w')

def process_data(json_obj):
return str(json_obj)

while True:
line = f_json.readline()
if None == line or 0 == len(line):
break
json_obj = json.loads(line)
processed_str = process_data(json_obj)
f_txt.write(processed_str+'\n')
f_txt.close()
f_json.close()


 

4. a.txt的每一行是一个记录,比如:

1||start|2016-0|page|2016-09-22||||2016-09-22

这里,'|'是字段分隔符,且有些字段可能会没有值。

 

5. 将a.txt的数据导入到Hive。将a.txt复制到hive集群的一个目录,然后执行hive,进入交互界面,然后依次执行命令,示意性代码如下(请根据具体问题修改使用):

create table t_1(id int, userId string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE;
load data local inpath 'a.txt' into table t_1;
select count(*) from t_1;
 
 

6. 创建分区表:经各种尝试发现,从已经建好的未作分区的表创建一个新的有分区的表是最简单最快捷的,其他方式都比它复杂。示意性代码如下(请根据具体问题修改使用):
(在Hive交互界面依次执行如下三条命令,将从t1表创建t2表,且使用createTimeDate作分区)

set hive.exec.dynamic.partition.mode=nonstrict;
create table t2(id int, userid string, page string, time timestamp, even string, createtime timestamp, param string) partitioned by (createtimedate date);

insert overwrite table t2 partition(createtimedate) select
x.id, x.userid, x.page, x.time, x.event, x.createtime, x.param, x.createtimedate from t1 x;

 

 

7.分区表创建的注意事项:
    对于语句“insert overwrite table ”,新表使用createTimeDate作为分区,它是指后面select出来的 最后一个 字段,而并不关心旧表的最后一个字段叫什么,比如,“insert overwrite table t2 partition(createtimedate) selectx.id,
x.userid, x.pageurl, x.time, x.eventtype, x.createtime, x.param, x.ctimedate from xloan_wx_np x;”,如果t1表存在字段x.ctimedate且是date,就能正常做分区。如果分区partition对应多个字段,意味着是导入数据的最后若干个字段。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: