将配置单元查询结果输出为csv,并用引号括起来

j1dl9f46  于 2021-06-28  发布在  Hive
关注(0)|答案(1)|浏览(292)

我必须从csv文件中的配置单元表导出数据,其中的字段用双引号括起来。
到目前为止,我能够生成一个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'
2ekbmq32

2ekbmq321#

创建外部表:

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的磁盘上

相关问题