1. 程式人生 > >hive複雜型別實戰

hive複雜型別實戰

1、hive 陣列簡單實踐:

CREATE TABLE `emp`(
  `name` string, 
  `emps` array<string>)
ROW FORMAT SERDE 
  'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  'hdfs://node:9000/user/hive/warehouse/daxin.db/emp'

存入資料,藉助insert into ... select :

insert into emp select "daxin",array('zhangsan','lisi','wangwu') from ptab;


hive> select *  from emp;
OK
daxin	["zhangsan","lisi","wangwu"]
mali	["jack","lixisan","fala"]
Time taken: 0.045 seconds, Fetched: 2 row(s)
hive> 
    > 
    > select *  from emp LATERAL VIEW explode(emps) tmp ;
OK
daxin	["zhangsan","lisi","wangwu"]	zhangsan
daxin	["zhangsan","lisi","wangwu"]	lisi
daxin	["zhangsan","lisi","wangwu"]	wangwu
mali	["jack","lixisan","fala"]	jack
mali	["jack","lixisan","fala"]	lixisan
mali	["jack","lixisan","fala"]	fala
Time taken: 0.047 seconds, Fetched: 6 row(s)
hive> select *  from emp LATERAL VIEW explode(emps) tmp as empeeName ;
OK
daxin	["zhangsan","lisi","wangwu"]	zhangsan
daxin	["zhangsan","lisi","wangwu"]	lisi
daxin	["zhangsan","lisi","wangwu"]	wangwu
mali	["jack","lixisan","fala"]	jack
mali	["jack","lixisan","fala"]	lixisan
mali	["jack","lixisan","fala"]	fala
Time taken: 0.038 seconds, Fetched: 6 row(s)
hive> 
    > set hive.cli.print.header=true;
hive> select *  from emp LATERAL VIEW explode(emps) tmp as empeeName ;
OK
emp.name	emp.emps	tmp.empeename
daxin	["zhangsan","lisi","wangwu"]	zhangsan
daxin	["zhangsan","lisi","wangwu"]	lisi
daxin	["zhangsan","lisi","wangwu"]	wangwu
mali	["jack","lixisan","fala"]	jack
mali	["jack","lixisan","fala"]	lixisan
mali	["jack","lixisan","fala"]	fala
Time taken: 0.046 seconds, Fetched: 6 row(s)

  

LATERAL VIEW explode(emps) tmp as empeeName 其中as後面的名字指定被拆分陣列的欄位名字為empeeName;