hive表脏数据和null值处理方案

hive表脏数据情况及解决方案:

1.制表分隔符和数据列分割符不一致
案例:本地文件导入hive表出现查询数值为null值
本地导入数据入hive表sql:

hive> load data local inpath "/home/01/temp/employee.txt" overwrite into table employee;
Loading data to table emp.employee
Table emp.employee stats: [numFiles=1, numRows=0, totalSize=901, rawDataSize=0]
OK
Time taken: 0.731 seconds
hive> select * from employee;
OK
NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL
NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL
NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL
NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL
NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL
NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL
NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL
NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL
NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL
NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL
NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL
NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL
NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL
Time taken: 0.148 seconds, Fetched: 13 row(s)

造成上面的原因一般是因为建表时分隔符和数据实际的分隔符不一致所致,修改下表的分隔符问题就可以解决。

alter table employee set SERDEPROPERTIES('field.delim'=',');

2.建表语句类型和数据实际类型不一致
案例:

hive> create table if not exists emp.employee(> id int comment '员工id',> name string comment '姓名',> gender int comment '性别(0:女,1:男)',> birthday date comment '生日',> entry_time date comment '入职时间',> department string comment '部门',> position string comment '职位',> salary double comment '薪资',> leader string comment '负责人'> )comment '员工表'> row format delimited fields terminated by ',';
OK
Time taken: 0.24 seconds
hive> load data local inpath "/home/01/temp/employee.txt" overwrite into table employee;
Loading data to table emp.employee
Table emp.employee stats: [numFiles=1, numRows=0, totalSize=901, rawDataSize=0]
OK
Time taken: 0.566 seconds
hive> select * from employee;
OK
1000	刘能	NULL	1976-06-07	2009-10-10	管理	总经理	15000.0	刘能
1001	张三	NULL	1987-10-05	2010-03-16	人事	经理	8000.0	刘能
1002	李四	NULL	1990-04-13	2013-06-08	生产	主任	10000.0	刘能
1003	王五	NULL	1992-10-08	2015-09-06	财务	经理	7000.0	刘能
1004	赵六	NULL	1991-08-06	2015-06-09	人事	普通职员	5000.0	张三
1005	牛备	NULL	1991-08-09	2015-04-09	生产	普通职员	4500.0	李四
1006	赵甜	NULL	1991-08-06	2015-06-09	生产	普通职员	4000.0	李四
1007	郭富	NULL	1991-06-06	2015-09-09	生产	普通职员	3500.0	李四
1008	李晨	NULL	1992-08-06	2016-06-09	生产	普通职员	5000.0	李四
1009	郭靖	NULL	1991-09-06	2014-06-09	财务	普通职员	5200.0	王五
1010	杨幂	NULL	1991-02-06	2016-06-09	后勤	经理	6000.0	刘能
1011	童大祥	NULL	1991-08-06	2015-06-09	后勤	普通职员	3500.0	杨幂
1012	谢贤	NULL	1993-08-06	2018-06-09	后勤	普通职员	3000.0	杨幂

数据:

1000,刘能,,1976-06-07,2009-10-10,管理,总经理,15000,刘能
1001,张三,,1987-10-05,2010-03-16,人事,经理,8000,刘能
1002,李四,,1990-04-13,2013-06-08,生产,主任,10000,刘能
1003,王五,,1992-10-08,2015-09-06,财务,经理,7000,刘能
1004,赵六,,1991-08-06,2015-06-09,人事,普通职员,5000,张三
1005,牛备,,1991-08-09,2015-04-09,生产,普通职员,4500,李四
1006,赵甜,,1991-08-06,2015-06-09,生产,普通职员,4000,李四
1007,郭富,,1991-06-06,2015-09-09,生产,普通职员,3500,李四
1008,李晨,,1992-08-06,2016-06-09,生产,普通职员,5000,李四
1009,郭靖,,1991-09-06,2014-06-09,财务,普通职员,5200,王五
1010,杨幂,,1991-02-06,2016-06-09,后勤,经理,6000,刘能
1011,童大祥,,1991-08-06,2015-06-09,后勤,普通职员,3500,杨幂
1012,谢贤,,1993-08-06,2018-06-09,后勤,普通职员,3000,杨幂

解决方案:修改表字段类型即可
3.inset into插入表数据乱码
案例:

hive> insert into table employee(id,name,gender,birthday,entry_time,department,position,salary,leader)values(1011,"童大祥",1,"1991-08-06","2015-06-09","后勤","普通职员",3500,"杨幂");
OK
Time taken: 163.629 secondshive> select * from employee;
OK
1011	�'e	1	1991-08-06	2015-06-09	�	nLX	3500.0	hB

中文乱码
解决方案:

insert into employee select 1011,decode(binary('童大祥'),'utf-8'),1,'1991-08-06','2015-06-09',decode(binary('后勤'),'utf-8'),decode(binary('普通职员'),'utf-8'),3500,decode(binary('杨幂'),'utf-8');

注意:1、使用decode函数,按utf-8解码插入;2、insert into后不要用values,而使用select,因为values不支持使用decode等函数。不然会报错


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部