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

2021-10-25 05:43:55 字數 3449 閱讀 4030

目錄

一、hive中預設的分割符如下

二、分隔符的指定與使用

三、建好表之後更改字段分隔符

分隔符描述\n

行分隔符

^a欄位分隔符 \001

^barray、struct的元素間的分隔符,map的鍵值對與鍵值對間分隔符 \002

^cmap中鍵與值之間的 分隔符\003

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.lazy******serde' --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_t***(

> id int comment'編號',

> name string comment '名字'

> )

> row format delimited fields terminated by '\001' --這裡可以指定別的分隔符,如『\t』,'$'等分隔符

> collection items terminated by '\002' -- 集合間的分隔符

> map keys terminated by '\003' -- map鍵與值之間的分隔符

> lines terminated by '\n' -- 行分隔符

> stored as textfile; -- 儲存格式為textfile

hive (fdm_sor)> show create table fdm_sor.mytest_t***;

okcreatetab_stmt

create table `fdm_sor.mytest_t***`(

`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_t***'

tblproperties (

'transient_lastddltime'='1526176859')

如上可以看出hive預設的列分割型別為org.apache.hadoop.hive.serde2.lazy.lazy******serde,而這其實就是^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)來指定分隔符。

alter table *** set serdeproperties('field.delim'='\t');

Hive的列分隔符和行分隔符

在建立hive表時,預設行分隔符 a 列分隔符 n 這兩項也是可以設定的。在實際開發中,一般預設使用預設的分隔符,當然有些場景下也會自定義分隔符。spark hive use test db 建立外部表 create external table test tb user id bigint com...

hive實現多分隔符

一 測試外表 create external 外表標識 table tmp.deli tmp 0117 z1 string,z2 string partitioned by pt string comment yyyymm row format serde org.apache.hadoop.hiv...

Sqoop匯入hive分隔符問題

sqoop從oracle匯入資料到hive,示例 plain view plain copy sqoop import connect jdbc oracle thin oracle host port orcl username name password passwd hive import t...