I am trying to export my hive table as a csv in beeline hive. When I run the command !sql select * from database1 > /user/bob/output.csv
it gives me syntax error.
I have successfully connected to the database at this point using the below command. The query outputs the correct results on console.
beeline -u 'jdbc:hive2://[databaseaddress]' --outputformat=csv
Also, not very clear where the file ends up. It should be the file path in hdfs correct?
When hive version is at least 0.11.0 you can execute:
from hive/beeline to store the table into a directory on the local filesystem.
Alternatively, with beeline, save your SELECT query in yourSQLFile.sql and run:
Also this will store the result into a file in the local file system.
From hive, to store the data somewhere into HDFS:
then you can collect the data to a local file using:
This is another option to get the data using beeline only:
Working on:
You have different options.
1) You can control up to a point how the beeline output is made and then just save it to a file with linux. For example
beeline --outputformat=csv2 xxx > output.csv ( see the relevant parameters from the beeline help below )
2) For more control and better performance I wrote a little Java tool once. Its really only a couple lines of jdbc code.
3) and finally as Ana wrote. Yopu can just write a table into an external table in HDFS and specify the output format you want.
Like
create external table test ROW FORMAT delimited fields terminated by '|' location "/tmp/myfolder" as select * from mytable;
you can then get that output in the local file system with
hadoop fs -getmerge /tmp/myfolder myoutput.csv