详解hive的列分隔符和行分隔符的使用

您所在的位置:网站首页 换行符有啥用 详解hive的列分隔符和行分隔符的使用

详解hive的列分隔符和行分隔符的使用

2023-10-04 13:51| 来源: 网络整理| 查看: 265

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

hive (fdm_sor)> create table fdm_sor.mytest_tmp2(               >  id int comment'编号',               >  name string comment '名字'               >  ); hive (fdm_sor)> show create table mytest_tmp2; CREATE  TABLE `mytest_tmp2`(   `id` int COMMENT '编号',    `name` string COMMENT '名字') ROW FORMAT SERDE    'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' --hive默认的分割方式,即行为\n,列为^A STORED AS INPUTFORMAT    'org.apache.hadoop.mapred.TextInputFormat'  --hive默认的存储格式为textfile OUTPUTFORMAT    'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat' LOCATION --内部表的默认的存储路径   'hdfs://hadoop102:9000/user/hive/warehouse/fdm_sor.db/mytest_tmp2' TBLPROPERTIES (   'transient_lastDdlTime'='1526176805') hive (fdm_sor)> create table  fdm_sor.mytest_tmp3(               >   id int comment'编号',               >  name string comment '名字'               >  )               >  row format delimited fields terminated by '\001' --这里可以指定别的分隔符,如‘\t’,'$'等分隔符               >  lines terminated by '\n'               >  stored as textfile; hive (fdm_sor)> show create table fdm_sor.mytest_tmp3; OK createtab_stmt CREATE  TABLE `fdm_sor.mytest_tmp3`(   `id` int COMMENT '编号',    `name` string COMMENT '编号') ROW FORMAT DELIMITED    FIELDS TERMINATED BY '\u0001'    LINES TERMINATED BY '\n'  STORED AS INPUTFORMAT    'org.apache.hadoop.mapred.TextInputFormat'  OUTPUTFORMAT    'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat' LOCATION   'hdfs://hadoop102:9000/user/hive/warehouse/fdm_sor.db/mytest_tmp3' TBLPROPERTIES (   '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’字符。否则报错。

hive (fdm_sor)> create table fdm_sor.mytest_tm4( > id int comment'编号', > name string comment '名字' > ) > lines terminated by '\t'; 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)来指定分隔符。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3