当前位置:   article > 正文

详解hive的列分隔符和行分隔符的使用_hive建表指定换行符

hive建表指定换行符

      hive中在创建表时,一般会根据导入的数据格式来指定字段分隔符和列分隔符。一般导入的文本数据字段分隔符多为逗号分隔符或者制表符(但是实际开发中一般不用着这种容易在文本内容中出现的的符号作为分隔符),当然也有一些别的分隔符,也可以自定义分隔符。有时候也会使用hive默认的分隔符来存储数据。

  1. hive (fdm_sor)> create table fdm_sor.mytest_tmp2(
  2.               >  id int comment'编号',
  3.               >  name string comment '名字'
  4.               >  );
  5. hive (fdm_sor)> show create table mytest_tmp2;
  6. CREATE  TABLE `mytest_tmp2`(
  7.   `id` int COMMENT '编号', 
  8.   `name` string COMMENT '名字')
  9. ROW FORMAT SERDE 
  10.   'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' --hive默认的分割方式,即行为\n,列为^A
  11. STORED AS INPUTFORMAT 
  12.   'org.apache.hadoop.mapred.TextInputFormat'  --hive默认的存储格式为textfile
  13. OUTPUTFORMAT 
  14.   'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
  15. LOCATION --内部表的默认的存储路径
  16.   'hdfs://hadoop102:9000/user/hive/warehouse/fdm_sor.db/mytest_tmp2'
  17. TBLPROPERTIES (
  18.   'transient_lastDdlTime'='1526176805')
  19. hive (fdm_sor)> create table  fdm_sor.mytest_tmp3(
  20.               >   id int comment'编号',
  21.               >  name string comment '名字'
  22.               >  )
  23.               >  row format delimited fields terminated by '\001' --这里可以指定别的分隔符,如‘\t’,'$'等分隔符
  24.               >  lines terminated by '\n'
  25.               >  stored as textfile;
  26. hive (fdm_sor)> show create table fdm_sor.mytest_tmp3;
  27. OK
  28. createtab_stmt
  29. CREATE  TABLE `fdm_sor.mytest_tmp3`(
  30.   `id` int COMMENT '编号', 
  31.   `name` string COMMENT '编号')
  32. ROW FORMAT DELIMITED 
  33.   FIELDS TERMINATED BY '\u0001' 
  34.   LINES TERMINATED BY '\n' 
  35. STORED AS INPUTFORMAT 
  36.   'org.apache.hadoop.mapred.TextInputFormat' 
  37. OUTPUTFORMAT 
  38.   'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
  39. LOCATION
  40.   'hdfs://hadoop102:9000/user/hive/warehouse/fdm_sor.db/mytest_tmp3'
  41. TBLPROPERTIES (
  42.   'transient_lastDdlTime'='1526176859')

       如上可以看出hive默认的列分割类型为org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe,而这其实就是^A分隔符,hive中默认使用^A(ctrl+A)作为列分割符,如果用户需要指定的话,等同于row format delimited fields terminated by '\001',因为^A八进制编码体现为'\001'.所以如果使用默认的分隔符,可以什么都不加,也可以按照上面的指定加‘\001’为列分隔符,效果一样。

       hive默认使用的行分隔符是'\n'分隔符 ,也可以加一句:LINES TERMINATED BY '\n' ,加不加效果一样。但是区别是hive可以通过row format delimited fields terminated by '\t'这个语句来指定不同的分隔符,但是hive不能够通过LINES TERMINATED BY '$$'来指定行分隔符,目前为止,hive的默认行分隔符仅支持‘\n’字符。否则报错。

  1. hive (fdm_sor)> create table fdm_sor.mytest_tm4(
  2. > id int comment'编号',
  3. > name string comment '名字'
  4. > )
  5. > lines terminated by '\t';
  6. FAILED: ParseException line 5:1 missing EOF at 'lines' near ')'

       一般来说hive的默认行分隔符都是换行符,如果非要自定义行分隔符的话,可以通过自定义Inputformat和outputformat类来指定特定行分隔符和列分隔符,一般公司实际开发中也都是这么干的,具体使用,见后面博客。

       当然如hive中集合数据类型struct ,map,array,也都有默认的字段分隔符,也都可以指定字段分隔符。hive中对于上述三个集合数据类型的默认字段分隔符是^B,八进制体现为‘\002’,用collection items terminated by '\002'语句来指定分隔符,对于map来说,还有键值之间的分割符,可以用map keys terminated by  '\003'(^C)来指定分隔符。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号