xxx.txt或者最原始的hive命令行来获得查询结果,然后再将查询结果放到Excel等工具中,但是如果查询的字段太多,这时候将查询结果放到Excel会经常会碰到错位问题,很是头疼.解决方案一:借助linux管道替换输出分隔符样例如下:# 方法一:sedhive -e "select * from ..._hive表分隔符查询">
当前位置:   article > 正文

[Hive]Hive指定查询输出分隔符_hive表分隔符查询

hive表分隔符查询

 

业务场景

做数据分析的时候,经常会用到hive -e "sql" > xxx.txt或者最原始的hive命令行来获得查询结果,然后再将查询结果放到Excel等工具中,但是如果查询的字段太多,这时候将查询结果放到Excel会经常会碰到错位问题,很是头疼.

解决方案一:借助linux管道替换输出分隔符

样例如下:

  1. # 方法一:sed
  2. hive -e "select * from pms.pms_algorithm_desc" | sed 's/\t/,/g' > ./aaa.txt
  3. # 方法二:tr
  4. hive -e "select * from pms.pms_tp_config" | tr "\t" ","
  • 1
  • 2
  • 3
  • 4
  • 5

结果如下:

  1. $ cat aaa.txt
  2. id,algorithm_id,algorithm_name,algorithm_desc,is_delete,update_time
  3. 1,0,默认,,0,2015-11-02 18:14:25.0
  4. 2,1,相关,相关分类或者买了还买,0,2015-11-02 18:14:25.0
  5. 3,2,相似,,0,2015-11-02 18:14:25.0
  6. 4,3,购物车商品为空时类目热销,,0,2015-11-02 18:14:25.0
  7. 5,4,热销补余(销量,GMV),,0,2015-11-02 18:14:25.0
  8. 6,5,指定类目选品,APP首页价比JD低补余逻辑中指定CE类目选品,0,2015-11-02 18:14:25.0

解决方案二:借助Hive的insert语法

样例如下:

  1. insert overwrite local directory '/home/pms/workspace/ouyangyewei/data/bi_lost'
  2. row format delimited
  3. fields terminated by ','
  4. select xxxx
  5. from xxxx;
  • 1
  • 2
  • 3
  • 4
  • 5

上面的sql将会把查询结果写到/home/pms/workspace/ouyangyewei/data/bi_lost_add_cart目录中,字段之间以,分隔

结果如下:

  1. $ ls ~/workspace/ouyangyewei/data/bi_lost
  2. 000000_0
  3. $ cat ~/workspace/ouyangyewei/data/bi_lost/000000_0
  4. 125171836,11565,6225443584836

Apache Hive官网上的介绍如下:

  1. Standard syntax:
  2. INSERT OVERWRITE [LOCAL] DIRECTORY directory1
  3. [ROW FORMAT row_format] [STORED AS file_format] (Note: Only available starting with Hive 0.11.0)
  4. SELECT ... FROM ...
  5. Hive extension (multiple inserts):
  6. FROM from_statement
  7. INSERT OVERWRITE [LOCAL] DIRECTORY directory1 select_statement1
  8. [INSERT OVERWRITE [LOCAL] DIRECTORY directory2 select_statement2] ...
  9. row_format
  10. : DELIMITED [FIELDS TERMINATED BY char [ESCAPED BY char]] [COLLECTION ITEMS TERMINATED BY char]
  11. [MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char]
  12. [NULL DEFINED AS char] (Note: Only available starting with Hive 0.13)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/389863
推荐阅读
相关标签
  

闽ICP备14008679号