hivesql匯出本地檔案
阿新 • • 發佈:2021-01-11
介紹
做資料分析的時候,經常會用到hive -e "sql" > xxx.txt
或者最原始的hive命令列來獲得查詢結果,然後再將查詢結果放到Excel
等工具中,但是如果查詢的欄位太多,這時候將查詢結果放到Excel
會經常會碰到錯位問題,很是頭疼.
解決方案一:藉助linux管道替換輸出分隔符
樣例如下:
# 方法一:sed hive -e "select * from db.table_name" | sed 's/\t/,/g' > ./abc.txt # 方法二:tr hive -e "select * from db.table_name" | tr "\t" ","
結果檢視如下:
$ cat abc.txt
解決方案二:藉助Hive的insert
語法
程式碼如下:
insert overwrite local directory 'path' row format delimited fields terminated by ',' select xxxx from xxxx;
上面的sql
將會把查詢結果寫到指定
目錄中,欄位之間以‘,’
分隔
結果如下:
$ ls path
000000_0
官方介紹:
Standard syntax:
INSERT OVERWRITE [LOCAL] DIRECTORY directory1
[ROW FORMAT row_format] [STORED AS file_format] (Note: Only available starting with Hive
0.11.
0)
SELECT ... FROM ...
Hive extension (multiple inserts):
FROM from_statement
INSERT OVERWRITE [LOCAL] DIRECTORY directory1 select_statement1
[INSERT OVERWRITE [LOCAL] DIRECTORY directory2 select_statement2] ...
row_format
: DELIMITED [FIELDS TERMINATED BY char [ESCAPED BY char]] [COLLECTION ITEMS TERMINATED BY char]
[MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char]
[NULL DEFINED AS char] (Note: Only available starting with Hive
0.13)