博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hive过滤脏数据的一些经验
阅读量:5888 次
发布时间:2019-06-19

本文共 2687 字,大约阅读时间需要 8 分钟。

如下文件需要处理,每个文件大概13G,其中字段以空格(32)分隔的7个字段;最麻烦的是中间有脏数据:

-rw-r--r-- 1 hadoop ifengdev 1895843464 May  6 14:56 feedback201503_201.tar.gz-rw-r--r-- 1 hadoop ifengdev 1896885848 May  6 14:59 feedback201503_202.tar.gz-rw-r--r-- 1 hadoop ifengdev 1891790676 May  6 15:00 feedback201503_203.tar.gz-rw-r--r-- 1 hadoop ifengdev 1894197100 May  6 15:01 feedback201503_204.tar.gz-rw-r--r-- 1 hadoop ifengdev 1894074074 May  6 15:02 feedback201503_205.tar.gz-rw-r--r-- 1 hadoop ifengdev 1829224750 May  6 16:13 feedback201504_201.tar.gz-rw-r--r-- 1 hadoop ifengdev 1831709571 May  6 16:14 feedback201504_202.tar.gz-rw-r--r-- 1 hadoop ifengdev 1824710879 May  6 16:30 feedback201504_203.tar.gz-rw-r--r-- 1 hadoop ifengdev 1827164031 May  6 16:31 feedback201504_204.tar.gz-rw-r--r-- 1 hadoop ifengdev 1827911208 May  6 16:31 feedback201504_205.tar.gz

直接Load进Hive报错:

Loading data to table default.tmp_20150506Failed with exception Wrong file format. Please check the file's format.FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.MoveTask

没办法中间格式有问题:

网上说改变存储格式可以避免报错:

CREATE  TABLE tmp_20150506(  dt string,  unknown1 string,  unknown2 string,  reurl string,  uid string,  num1 int,  num2 int)ROW FORMAT DELIMITED  FIELDS TERMINATED BY '32'  LINES TERMINATED BY '10'STORED AS INPUTFORMAT  'org.apache.hadoop.hive.ql.io.RCFileInputFormat'OUTPUTFORMAT  'org.apache.hadoop.hive.ql.io.RCFileOutputFormat'改为:CREATE  TABLE tmp_20150506(  dt string,  unknown1 string,  unknown2 string,  reurl string,  uid string,  num1 int,  num2 int)ROW FORMAT DELIMITED  FIELDS TERMINATED BY '32'  LINES TERMINATED BY '10'STORED AS TEXTFILE;

确实不报错了,根据具体需求也算一个方法;

 

最直接的方法:

zcat feedback201503_201.tar.gz|gawk -F ' ' 'NF==7 {print $1, "\t", $2, "\t", $3, "\t", $4, "\t", $5, F ' ' 'NF==7 {print $1, "\t", $2, "\t", $3, "\t", $4, "\t", $5, "\t", $6, "\t", $7}' >> feedback20150, "\t", $6, "\t", $7}' >> feedback201503_204.log

功能:替换空格为制表符;并且过滤字段不满足要求的脏数据;

接着Load进Hive即可;

上述方法比较直接,但觉得“体力劳动“过多,可能我比较懒,所以相对喜欢下边的方法:

基本思路就是把一行作为一个字段load进Hive,利用Hive本身筛选数据:

CREATE  TABLE tmp_20150506_raw(  allfilds string)ROW FORMAT DELIMITED  FIELDS TERMINATED BY '10'  LINES TERMINATED BY '10'STORED AS TEXTFILE;
FIELDS TERMINATED BY '10'
LINES TERMINATED BY '10' 都设置成换行符即可,进入Hive以后使用Hive筛选数据即可。 筛选数据并存入另外一张表中,本例的后续处理过程如下:
from(from(select allfilds from tmp_20150506_raw where size(split(allfilds, ' ')) = 7) aselect split(allfilds, ' ')[0] as dt, split(allfilds, ' ')[1] as unknown1, split(allfilds, ' ')[2] as unknown2, split(allfilds, ' ')[3] as reurl, split(allfilds, ' ')[4] as uid, split(allfilds, ' ')[5] as num1, split(allfilds, ' ')[6] as num2) binsert overwrite table tmp_20150506 partition(month = '2015-04')select *
 

 

转载地址:http://szwsx.baihongyu.com/

你可能感兴趣的文章
cairo-1.14.6 static compiler msys mingw32
查看>>
Mac osx 下让android 模拟器横屏
查看>>
SQL创建触发器
查看>>
喜爱看剑雨,数据流的本人对各主角…
查看>>
luogu P1387 最大正方形
查看>>
Android图片圆角效果
查看>>
MSSQL跨服务器数据库查询
查看>>
WeChat Official Account Admin Platform API Introduction
查看>>
C语言写单链表的创建、释放、追加(即总是在最后的位置增加节点)
查看>>
poj1635
查看>>
C# LINQ详解(一)
查看>>
视频直播点播nginx-rtmp开发手册中文版
查看>>
iphone 添加CFNetwork.framework时,报错 socket
查看>>
ruby学习总结04
查看>>
Binary Tree Paths
查看>>
RESTful 架构详解(转)
查看>>
Ueditor自定义ftp上传
查看>>
线程以及多线程
查看>>
PHP队列的实现
查看>>
单点登录加验证码例子
查看>>