这篇文章主要介绍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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。