Bhupendra Shukla
Bhupendra Shukla

Reputation: 3904

How to export the query output to csv in Hive

I want to save the query output in the .csv file using Hive. I am using following command in the hadoop command line.

hive -e "SELECT t4 AS sev, COUNT(*) AS cnt FROM Samplelogs WHERE t4 LIKE '[%' GROUP BY t4" | sed 's/[\t]/,/g'  >> outputfile.csv

But it is producing the Parsing error "Cannot recognise symbol near 'hive' '-' 'e'"

Upvotes: 0

Views: 7191

Answers (2)

Dattatrey Sindol
Dattatrey Sindol

Reputation: 63

I had a similar issue and this is how I was able to address it.

Step 1 - Loaded the data from hive table into another table as follows

DROP TABLE IF EXISTS TestHiveTableCSV; CREATE TABLE TestHiveTableCSV ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' AS SELECT Column List FROM TestHiveTable;

Step 2 - Copied the blob from hive warehouse to the new location with appropriate extension

Start-AzureStorageBlobCopy -DestContext $destContext -SrcContainer "Source Container" -SrcBlob "hive/warehouse/TestHiveTableCSV/000000_0" -DestContainer "Destination Container" ` -DestBlob "CSV/TestHiveTable.csv"

Hope this helps!

Upvotes: 2

dimamah
dimamah

Reputation: 2903

this command has to be executed from a shell and not hadoop command line as hive is a binary executable. In addition, you can use the insert overwrite directory command described here https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DML#LanguageManualDML-Writingdataintothefilesystemfromqueries

Upvotes: 1

Related Questions