Alex

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

参考资料:http://stackoverflow.com/questions/16459790/hive-insert-overwrite-directory-command-output-is-not-separated-by-a-delimiter

 

问题描述:

Hive insert into directory 命令输出的文件没有指定列分隔符,输出结果就像变成了一个字符串。

通过CREATE EXTERNAL TABLE 和load 方式,尝试了多种分隔符都不能正确的区分,所有的字段内容合起来变成一个字符串放在了第一个字段,而后面的字段全部为NULL。

 

问题分析:

1. 导出前的Hive表是以'\t'作为分隔符的,用hadoop fs -cat 看到数据是有分隔符的,如:

001 000
001 000
002 001
003 002
004 003
005 004
006 005
007 006
008 007
099 007

2. 导出语句如下:

insert overwrite directory '/tmp/hdfs_out' select a.* from invites a where a.ds='<date>';

3. 查看导出的文件内容:

hadoop dfs -cat /tmp/hdfs_out/000000_0

001000
001000
002001
003002
004003
005004
006005
007006
008007
099007


解决方法:

引文中的满意答案如下:

Are you sure there is no delimiter in the output directory? By default, Hive uses '\1' for the field delimiter, so you should try 
`hadoop dfs -cat "/tmp/hdfs_out/*" | tr '\001' ' ' –  libjack May 9 '13 at 17:11

意思是,在没有指定分隔符的默认情况下,hive用'\1' 来做列分隔符,因此,对于这样的文件,要用'\001'来区分。

 

据此,将建表语句改为后装载成功:

CREATE EXTERNAL TABLE tmp_06_table_name(
……
)
COMMENT 'this is tmp_06_table_name'
ROW FORMAT DELIMITED 
  FIELDS TERMINATED BY '\1' 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  'hdfs://yncm/tmp/hdfs_out/';

 

posted on 2015-05-18 21:09  疯狂矩阵  阅读(6400)  评论(0编辑  收藏  举报