4

I have to export data from a hive table in a csv file in which fields are enclosed in double quotes.

So far I am able to generate a csv without quotes using the following query

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

The output generated looks like

1,Vikas Saxena,Banking,JL5

However, I need the output as

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

I tried changing the query to

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

But it displays error

Error while compiling statement: FAILED: ParseException line 1:0 cannot recognize input near 'ROW' 'FORMAT' 'SERDE'
Vikas Saxena
  • 1,073
  • 1
  • 12
  • 21

1 Answers1

0

Create an external table:

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';

Then select into that table:

insert into new_table select * from original_table;

Your CSV is then on disk at /user/vikas/output

la_kal
  • 31
  • 2