2017-03-03 23 views
1

我有一個字段爲'str'的表格。 當我跑如何將' N'字符轉換爲使用Hive的空字符串

select str from mytable where str is null 

它打印空。但是,當我運行:

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

有 '\ n' 字符

「/路徑/到/目錄」

我想知道如果有一種方法在將結果寫入本地文件時將'\ N'字符更改爲空字符串。

回答

0

好了,你可以隨時使用CASE-WHEN語句是這樣的:

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

記住,你可以改變你的方式對待使用此表屬性空字段:

「serialization.null .format「

0

是需要小改動。您查詢返回總是null結果,所以直接把" " (empty string)文件。

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

如何對其它列也可以爲空,寫爲「 – user4234323

+0

@ user4234323檢查答案,我修改。 –

1

使用此: -

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; 

您可能不需要時

2

演示了@AshishSingh答案刪除FIELDS TERMINATED BY "\t"

蜂巢

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 
相關問題