hive原生和複合型數據

 

原生類型apache

原生類型包括TINYINT,SMALLINT,INT,BIGINT,BOOLEAN,FLOAT,DOUBLE,STRING,BINARY (Hive 0.8.0以上纔可用),TIMESTAMP (Hive 0.8.0以上纔可用),這些數據加載很容易,只要設置好列分隔符,按照列分隔符輸出到文件就能夠了。數組

假設有這麼一張用戶登錄表oop

CREATE TABLE login (  
  uid  BIGINT,  
  ip  STRING  
)  
ROW FORMAT DELIMITED  
FIELDS TERMINATED BY ','  
STORED AS TEXTFILE;

這表示登錄表ip字段和uid字段以分隔符','隔開。ui

輸出hive表對應的數據spa

# printf "%s,%s\n" 3105007001 192.168.1.1 >> login.txt  
# printf "%s,%s\n" 3105007002 192.168.1.2 >> login.txt

login.txt的內容:code

#cat login.txt                                                                                                                        
3105007001,192.168.1.1
3105007002,192.168.1.2

加載數據到hive表遊戲

LOAD DATA LOCAL INPATH '/home/hadoop/login.txt' OVERWRITE INTO TABLE login PARTITION (dt='20130101');

查看數據ip

select uid,ip from login where dt='20130101';
3105007001    192.168.1.1
3105007002    192.168.1.2

array

假設登錄表是hadoop

CREATE TABLE login_array (
  ip  STRING,
  uid  array<BIGINT>
)
PARTITIONED BY (dt STRING)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
COLLECTION ITEMS TERMINATED BY '|'
STORED AS TEXTFILE;

這表示登錄表每一個ip有多個用戶登錄,ip和uid字段之間使用','隔開,而uid數組之間的元素以'|'隔開。get

輸出hive表對應的數據

# printf "%s,%s|%s|%s\n" 192.168.1.1 3105007010 3105007011 3105007012 >> login_array.txt
# printf "%s,%s|%s|%s\n" 192.168.1.2  3105007020 3105007021 3105007022 >> login_array.txt

login_array.txt的內容:

cat login_array.txt                                                                                                                   
192.168.1.1,3105007010|3105007011|3105007012
192.168.1.2,3105007020|3105007021|3105007022

加載數據到hive表

LOAD DATA LOCAL INPATH '/home/hadoop/login_array.txt' OVERWRITE INTO TABLE login_array PARTITION (dt='20130101');

查看數據

select ip,uid from login_array where dt='20130101';
192.168.1.1    [3105007010,3105007011,3105007012]
192.168.1.2    [3105007020,3105007021,3105007022]

使用數組

select ip,uid[0] from login_array where dt='20130101'; --使用下標訪問數組
select ip,size(uid) from login_array where dt='20130101'; #查看數組長度
select ip from login_array where dt='20130101'  where array_contains(uid,'3105007011');#數組查找

更多操做參考 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-CollectionFunctions

map

假設登錄表是

CREATE TABLE login_map (
  ip  STRING,
  uid  STRING,
  gameinfo map<string,bigint>
)
PARTITIONED BY (dt STRING)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
COLLECTION ITEMS TERMINATED BY '|'
MAP KEYS TERMINATED BY ':'
STORED AS TEXTFILE;

這表示登錄表每一個用戶都會有遊戲信息,而用戶的遊戲信息有多個,key是遊戲名,value是遊戲的積分。map中的key和value以'':"分隔,map的元素以'|'分隔。 

輸出hive表對應的數據

# printf "%s,%s,%s:%s|%s:%s|%s:%s\n" 192.168.1.1  3105007010 wow 10 cf 1 qqgame 2  >> login_map.txt
# printf "%s,%s,%s:%s|%s:%s|%s:%s\n" 192.168.1.2  3105007012 wow 20 cf 21 qqgame 22  >> login_map.txt

login_map.txt的內容:

# cat login_map.txt
192.168.1.1,3105007010,wow:10|cf:1|qqgame:2
192.168.1.2,3105007012,wow:20|cf:21|qqgame:22

加載數據到hive表

LOAD DATA LOCAL INPATH '/home/hadoop/login_map.txt' OVERWRITE INTO TABLE login_map PARTITION (dt='20130101');

查看數據

select ip,uid,gameinfo from login_map where dt='20130101';
192.168.1.1    3105007010    {"wow":10,"cf":1,"qqgame":2}
192.168.1.2    3105007012    {"wow":20,"cf":21,"qqgame":22}

使用map

select ip,uid,gameinfo['wow'] from login_map where dt='20130101'; --使用下標訪問map
select ip,uid,size(gameinfo) from login_map where dt='20130101'; #查看map長度
select ip,uid from login_map where dt='20130101'  where array_contains(map_keys(gameinfo),'wow');#查看map的key,找出有玩wow遊戲的記錄

更多操做參考 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-CollectionFunctions

struct

假設登錄表是

CREATE TABLE login_struct (
  ip  STRING,
  user  struct<uid:bigint,name:string>
)
PARTITIONED BY (dt STRING)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
COLLECTION ITEMS TERMINATED BY '|'
MAP KEYS TERMINATED BY ':'
STORED AS TEXTFILE;

user是一個struct,分別包含用戶uid和用戶名。

輸出hive表對應的數據

printf "%s,%s|%s|\n" 192.168.1.1  3105007010 blue  >> login_struct.txt
printf "%s,%s|%s|\n" 192.168.1.2  3105007012 ggjucheng  >> login_struct.txt

login_struct.txt的內容: 

# cat login_struct.txt
192.168.1.1,3105007010|blue
192.168.1.2,3105007012|ggjucheng

加載數據到hive表

LOAD DATA LOCAL INPATH '/home/hadoop/login_struct.txt' OVERWRITE INTO TABLE login_struct PARTITION (dt='20130101');

查看數據

select ip,user from login_struct where dt='20130101';
192.168.1.1    {"uid":3105007010,"name":"blue"}
192.168.1.2    {"uid":3105007012,"name":"ggjucheng"}

使用struct

select ip,user.uid,user.name from login_map where dt='20130101';

union

用的比較少,暫時不講

相關文章
相關標籤/搜索