将hive查询结果输出为引号

时间:2016-09-21 07:05:06

标签: csv hive export-to-csv

我必须从csv文件中的hive表导出数据,其中的字段用双引号括起来。

到目前为止,我可以使用以下查询生成不带引号的csv

INSERT OVERWRITE DIRECTORY '/user/vikas/output'
ROW FORMAT DELIMITED 
FIELDS TERMINATED BY ','
SELECT QUERY

生成的输出类似于

1,Vikas Saxena,Banking,JL5

但是,我需要输出为

"1","Vikas Saxena","Banking","JL5" 

我尝试将查询更改为

INSERT OVERWRITE DIRECTORY '/user/vikas/output'
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
WITH SERDEPROPERTIES (
   "separatorChar" = ",",
   "quoteChar"     = "\"",
   "escapeChar"    = "\\"
)
SELECT QUERY

但它显示错误

Error while compiling statement: FAILED: ParseException line 1:0 cannot recognize input near 'ROW' 'FORMAT' 'SERDE'

1 个答案:

答案 0 :(得分:0)

创建外部表:

CREATE EXTERNAL TABLE new_table(field1 type1, ...)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
WITH SERDEPROPERTIES (
"separatorChar" = ",",
"quoteChar" = "\""
)
STORED AS TEXTFILE
LOCATION '/user/vikas/output';

然后选择进入该表:

insert into new_table select * from original_table;

然后,您的CSV将位于/ user / vikas / output

上的磁盘上