如何使用配置单元将'\n'字符转换为空字符串

mrwjdhj3  于 2021-06-02  发布在  Hadoop
关注(0)|答案(4)|浏览(512)

我有一个字段为“str”的表。当我跑的时候

select str from mytable where str is null

它打印空。但当我跑的时候:

INSERT OVERWRITE LOCAL DIRECTORY "/path/to/dir" 
select str from mytable where str is null;

中有'\n'个字符
“/path/到/dir”
我想知道在将结果写入本地文件时是否有方法将'\n'字符改为空字符串。

ewm0tg9j

ewm0tg9j1#

@ashishsingh答案演示
Hive

create table mytable (i int,j int,k int);
insert into mytable values (1,2,null),(null,5,null),(7,null,9);

select * from mytable
;
+-----------+-----------+-----------+
| mytable.i | mytable.j | mytable.k |
+-----------+-----------+-----------+
| 1         | 2         | NULL      |
| NULL      | 5         | NULL      |
| 7         | NULL      | 9         |
+-----------+-----------+-----------+
insert overwrite local directory "/tmp/mytable" 
row format delimited fields terminated by "," null defined as '' 
select * from mytable
;

猛击

cat /tmp/mytable/*
1,2,
,5,
7,,9
hfwmuf9z

hfwmuf9z2#

是的,我需要零钱。您总是查询返回 null 结果如此直接 " " (empty string) 归档。

select if(str is null, " ", str) str from mytable where str is null
vuktfyat

vuktfyat3#

使用this:-

INSERT OVERWRITE LOCAL DIRECTORY "/path/to/dir" 
ROW FORMAT DELIMITED FIELDS TERMINATED BY "\t" NULL DEFINED AS '' 
select str from mytable where str is null;

您可以删除 FIELDS TERMINATED BY "\t" 如果不需要

mkshixfv

mkshixfv4#

好吧,你总是可以使用case语句,比如:

SELECT CASE WHEN str is null then '' ELSE str END;

请记住,可以使用以下表属性更改处理空字段的方式:
“序列化.null.format”

相关问题