hive中操作struct与map三例
生活随笔
收集整理的這篇文章主要介紹了
hive中操作struct与map三例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
例子一
beeline中進行:
create table test (id int,course struct<course:string,score:int>) row format delimited fields terminated by ',' collection items terminated by ':';/home/appleyuchi/test.txt
中加入以下內容:
1,english:80
2,math:89
beeline中繼續:
load data local inpath '/home/appleyuchi/test.txt' overwrite into table test;?
select * from test;
得到:
+----------+----------------------------------+ | test.id | test.course | +----------+----------------------------------+ | 1 | {"course":"english","score":80} | | 2 | {"course":"math","score":89} | +----------+----------------------------------+例子二
?
create table tb_test2 (name string,score_list array<map<string,int>>);insert into tb_test2 select "A", array(map({"math",100},{"history",85})) from (select 1) x;insert into tb_test2 select "A", array(map("math",100,"english",90,"history",85)) from (select 1) x;插入效果如下:
0: jdbc:hive2://Desktop:10000> select * from tb_test2; +----------------+-----------------------------------------------+ | tb_test2.name | tb_test2.score_list | +----------------+-----------------------------------------------+ | A | [{"math":100},{"english":90},{"history":85}] | | A | [{"history":85,"english":90,"math":100}] | +----------------+-----------------------------------------------+?
例子三
create table sales_info_new( sku_id string comment '商品id', sku_name string comment '商品名稱', state_map map<string,string> comment '商品狀態信息', id_array array<string> comment '商品相關id列表' ) partitioned by( dt string comment '年-月-日' ) row format delimitedfields terminated by '|'collection items terminated by ','map keys terminated by ':';~/test.txt中寫入:
123|華為Mate10|id:1111,token:2222,user_name:zhangsan1|1235,345 456|華為Mate30|id:1113,token:2224,user_name:zhangsan3|89,635 789|小米5|id:1114,token:2225,user_name:zhangsan4|452,63 1235|小米6|id:1115,token:2226,user_name:zhangsan5|785,36 4562|OPPO Findx|id:1116,token:2227,user_name:zhangsan6|7875,3563?
load data local inpath '/home/appleyuchi/test.txt' overwrite into table sales_info_new partition(dt='2019-04-26')導入效果如下:
0: jdbc:hive2://Desktop:10000> select * from sales_info_new; +------------------------+--------------------------+----------------------------------------------------+--------------------------+--------------------+ | sales_info_new.sku_id | sales_info_new.sku_name | sales_info_new.state_map | sales_info_new.id_array | sales_info_new.dt | +------------------------+--------------------------+----------------------------------------------------+--------------------------+--------------------+ | 123 | 華為Mate10 | {"id":"1111","token":"2222","user_name":"zhangsan1"} | ["1235","345"] | 2019-04-26 | | 456 | 華為Mate30 | {"id":"1113","token":"2224","user_name":"zhangsan3"} | ["89","635"] | 2019-04-26 | | 789 | 小米5 | {"id":"1114","token":"2225","user_name":"zhangsan4"} | ["452","63"] | 2019-04-26 | | 1235 | 小米6 | {"id":"1115","token":"2226","user_name":"zhangsan5"} | ["785","36"] | 2019-04-26 | | 4562 | OPPO Findx | {"id":"1116","token":"2227","user_name":"zhangsan6"} | ["7875","3563"] | 2019-04-26 | +------------------------+--------------------------+----------------------------------------------------+--------------------------+--------------------+?
Reference:
[1]How to insert array<map<string,int>> into hive table?
[2]Hive 導入array,map,要定義好分隔符,不用帶雙引號
總結
以上是生活随笔為你收集整理的hive中操作struct与map三例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux MMC介绍
- 下一篇: 常见的那些模糊不清的计算机相关概念(长期