Hive特殊分隔符处理

案例数据
11||zhangsan||22
22||lisi||22
33||wangwu||22
失败案例:
创建表录入数据
#创建表
create table bi_test(id int,name string,num string) row format delimited fields terminated by '||';
#导入数据
load data local inpath '/home/bigdata/data/bi.txt' into table bi_test;
#查询数据
select * from bi_test;

解决方案一:
将数据文件里的||
变成|
11|zhangsan|22
22|lisi|22
33|wangwu|22
解决方案二:
使用 RegexSerDe
通过正则表达式来抽取字段
#创建表
create table h_bi_reg(id string,name string,num string) row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe' with serdeproperties('input.regex'='(.*)\\|\\|(.*)\\|\\|(.*)','output.format.string'='%1$s %2$s %3$s') stored as textfile;
#导入数据
load data local inpath '/home/bigdata/data/bi.txt' into table h_bi_reg;
#查询数据
select * from h_bi_reg;

解决方案三:
通过自定义 InputFormat
解决特殊分隔符问题
其原理是在 inputformat 读取行的时候将数据中的“多字节分隔符”替换为 hive 默认的分隔符(ctrl+A 亦即 \001)或用于替代的单字符分隔符,以便 hive 在 serde 操作时按照默认的单字节分隔符进行字段抽取
网友评论