4

Using Hive I would like to get the result of a query inside a csv file, I would also like to get the header.

To achieve this I'm executing:

hive -e '
    use database;
    set hive.cli.print.header=true;
    select * from table;
' > /home/centos/user/result.csv

But the first line of the csv is:

table.field1_name,table.field2_name,table.field3_name

How can I get insead:

field1_name,field2_name,field3_name
starball
  • 20,030
  • 7
  • 43
  • 238
user3636476
  • 1,357
  • 2
  • 11
  • 22

4 Answers4

15

I just needed to add:

set hive.resultset.use.unique.column.names=false;
user3636476
  • 1,357
  • 2
  • 11
  • 22
0

sed will do the job.

Suppose fun.csv contains the following:

table.field1_name,table.field2_name,table.field3_name

Then I do the following in the command line:

sed -e 's/table.//g' < fun.csv > fun2.csv

Now fun2.csv has:

field1_name,field2_name,field3_name
xxks-kkk
  • 2,336
  • 3
  • 28
  • 48
0

you can use select column1,column2 from tablename instead of select * from tablename, in that case tablename will not be added into the column name. In that case need not give sed command.

-1

I use

hive -e 'set hive.cli.print.header=true; select * from database.table;' | sed 's/[\t]/,/g' > /your_path_to_csv

It works well for me.

Reference: Hive query output delimiter

Community
  • 1
  • 1