介绍
做数据分析的时候,经常会用到hive -e "sql" > xxx.txt
或者最原始的hive命令行来获得查询结果,然后再将查询结果放到Excel
等工具中,但是如果查询的字段太多,这时候将查询结果放到Excel
会经常会碰到错位问题,很是头疼.
解决方案一:借助linux管道替换输出分隔符
样例如下:
代码语言:javascript复制# 方法一:sed
hive -e "select * from db.table_name" | sed 's/t/,/g' > ./abc.txt
# 方法二:tr
hive -e "select * from db.table_name" | tr "t" ","
结果查看如下:
代码语言:javascript复制$ cat abc.txt
解决方案二:借助Hive的insert
语法
代码如下:
代码语言:javascript复制insert overwrite local directory 'path'
row format delimited
fields terminated by ','
select xxxx
from xxxx;
上面的sql
将会把查询结果写到指定
目录中,字段之间以‘,’
分隔
结果如下:
代码语言:javascript复制$ ls path
代码语言:javascript复制000000_0
代码语言:javascript复制
官方介绍:
代码语言:javascript复制Standard syntax:
代码语言:javascript复制INSERT OVERWRITE [LOCAL] DIRECTORY directory1
代码语言:javascript复制 [ROW FORMAT row_format] [STORED AS file_format] (Note: Only available starting with Hive 0.11.0)
代码语言:javascript复制 SELECT ... FROM ...
代码语言:javascript复制
代码语言:javascript复制Hive extension (multiple inserts):
代码语言:javascript复制FROM from_statement
代码语言:javascript复制INSERT OVERWRITE [LOCAL] DIRECTORY directory1 select_statement1
代码语言:javascript复制[INSERT OVERWRITE [LOCAL] DIRECTORY directory2 select_statement2] ...
代码语言:javascript复制row_format
代码语言:javascript复制 : DELIMITED [FIELDS TERMINATED BY char [ESCAPED BY char]] [COLLECTION ITEMS TERMINATED BY char]
代码语言:javascript复制 [MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char]
代码语言:javascript复制 [NULL DEFINED AS char] (Note: Only available starting with Hive 0.13)