一、目的
在用Flume把Kafka的數(shù)據(jù)采集寫入Hive的ODS層表的HDFS文件路徑后,發(fā)現(xiàn)HDFS文件中沒問題,但是ODS層表中字段的數(shù)據(jù)卻有問題,字段中的JSON數(shù)據(jù)不全
二、Hive處理JSON數(shù)據(jù)方式
(一)將Flume采集Kafka的JSON數(shù)據(jù)以字符串的方式整個寫入Hive表中,然后再用get_json_object或json_tuple進行解析
1、ODS層建靜態(tài)分區(qū)外部表,F(xiàn)lume直接寫入ODS層表的HDFS路徑下
create external table if not exists ods_evaluation( evaluation_json string ) comment '評價數(shù)據(jù)外部表——靜態(tài)分區(qū)' partitioned by (day string) stored as SequenceFile ;
2、用get_json_object進行解析
select get_json_object(evaluation_json,'$.deviceNo') device_no, get_json_object(evaluation_json,'$.createTime') create_time, get_json_object(evaluation_json,'$.cycle') cycle, get_json_object(evaluation_json,'$.laneNum') lane_num, get_json_object(evaluation_json,'$.evaluationList') evaluation_list from hurys_dc_ods.ods_evaluation ;
(二)在導入Hive表之前將JSON數(shù)據(jù)已拆分好,需要使用JsonSerDe
create external table if not exists ods_track(
device_no string comment '設備編號',
create_time timestamp comment '創(chuàng)建時間',
track_data string comment '軌跡數(shù)據(jù)集合(包含多個目標點)'
)
comment '軌跡數(shù)據(jù)表——靜態(tài)分區(qū)'
partitioned by (day date)
row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
with serdeproperties (
"separatorChar" = ",",
"quoteChar" = "\"",
"escapeChar" = "\\"
)
tblproperties("skip.header.line.count"="1") ;
注意:使用JsonSerDe時,每行必須是一個完整的JSON,一個JSON不能跨越多行,否則不能使用JsonSerDe
三、ODS層原有建表SQL
create external table if not exists ods_evaluation(
evaluation_json string
)
comment '評價數(shù)據(jù)外部表——靜態(tài)分區(qū)'
partitioned by (day string)
row format delimited fields terminated by '\x001'
lines terminated by '\n'
stored as SequenceFile
;
四、HDFS文件中的數(shù)據(jù)
HDFS文件中JSON數(shù)據(jù)完整,數(shù)據(jù)沒問題
五、報錯詳情
查看表數(shù)據(jù)時發(fā)現(xiàn)evaluation_json字段的數(shù)據(jù)不完整
六、解決方法
(一)重新建表,建表語句中刪除其中兩行
--row format delimited fields terminated by '\x001'
--lines terminated by '\n'
(二)新建表SQL
create external table if not exists ods_evaluation( evaluation_json string ) comment '評價數(shù)據(jù)外部表——靜態(tài)分區(qū)' partitioned by (day string) stored as SequenceFile ;
七、查詢新表中evaluation_json字段的數(shù)據(jù)
數(shù)據(jù)解析成功!
文章來源:http://www.zghlxwxcb.cn/news/detail-776275.html
又解決了一個問題,賓果!文章來源地址http://www.zghlxwxcb.cn/news/detail-776275.html
到了這里,關于二百一十、Hive——Flume采集的JSON數(shù)據(jù)文件寫入Hive的ODS層表后字段的數(shù)據(jù)殘缺的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!