您好,登錄后才能下訂單哦!
這篇文章主要介紹hive原生和復合型數據的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
原生類型
原生類型包括TINYINT,SMALLINT,INT,BIGINT,BOOLEAN,FLOAT,DOUBLE,STRING,BINARY (Hive 0.8.0以上才可用),TIMESTAMP (Hive 0.8.0以上才可用),這些數據加載很容易,只要設置好列分隔符,按照列分隔符輸出到文件就可以了。
假設有這么一張用戶登陸表
CREATE TABLE login ( uid BIGINT, ip STRING ) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE;
這表示登陸表ip字段和uid字段以分隔符','隔開。
輸出hive表對應的數據
# printf "%s,%s\n" 3105007001 192.168.1.1 >> login.txt # printf "%s,%s\n" 3105007002 192.168.1.2 >> login.txt
login.txt的內容:
#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');
查看數據
select uid,ip from login where dt='20130101'; 3105007001 192.168.1.1 3105007002 192.168.1.2
假設登陸表是
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數組之間的元素以'|'隔開。
輸出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
假設登陸表是
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
假設登陸表是
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';
用的比較少,暫時不講
以上是“hive原生和復合型數據的示例分析”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。