lateral view json_tuple(t.source,'logid','msg','level') v1 as logid,msg,level
2. HIVE行转列
2.1 使用以下函数进行行转列 collect_set/collect_list + concat_ws() + group by
select
t.app_uid as app_uid
,concat_ws(',',collect_set(t.tag))as tags
from(select
app_uid
,casewhentype=1then'1'whentype=2then'2'whentype=3then'3'whentype=4then'4'else'其他'endas tag
from ymtods.test
whereday='{ptdate}'andstatus=1) t
groupby t.app_uid
;
例如:订单信息中有多个字段 created_time,buyer_pay_time,seller_send_goods_time,buyer_recieve_goods_time,order_refund_time
LATERAL VIEW explode (map('100',created_time
,'200',created_time
,'300',buyer_pay_time
,'410',seller_send_goods_time
,'500',buyer_recieve_goods_time
,'501',order_refund_time
)) b as order_status,order_status_time
3.2 多列转单列多行 concat_ws() LATERAL VIEW explode()
例如:一个供应有多个类目,一级类目,二级类目,三级类目,需要多个类目下求聚合指标
select
e.category_id
,c.logid
from(select
concat_ws(',',cast(parsed_cid1_id as string),cast(parsed_cid2_id as string),cast(parsed_product_id as string),cast(parsed_breed_id as string))as category_id
,logid
from ymtcube.dwd_search_impress_track_h
where pthour = from_unixtime(unix_timestamp('${pthour}','yyyyMMddHH')-3600,'yyyyMMddHH')) c
lateral view explode(split(c.category_id,",")) e as category_id
where e.category_id >0
3.3 单列转单列多行 LATERAL VIEW explode() 数组类型的直接打开
lateral view explode(split(c.category_id,",")) e as category_id
3.4 使用union进行列转行
例如:
数据 学生号 语文 英语 数学
01809070
学生号 科目 成绩
01 语文 8001 英语 9002 语文 60selectselect
学生号
,'语文'as 科目
,语文 as 成绩
union
学生号
,'英语'as 科目
,英语 as 成绩
union
学生号
,'数学'as 科目
,数学 as 成绩
fromtable
3.5 使用posexplode()函数实现多列转多行
a. 比如数据如下:
id tim
a,b,c,d 2:00,3:00,4:00,5:00
a,b,c,d 2:00,3:00,4:00,5:00
a,b,c,d 2:00,3:00,4:00,5:00
a,b,c,d 2:00,3:00,4:00,5:00
f,b,c,d 1:10,2:20,3:30,4:40
f,b,c,d 1:10,2:20,3:30,4:40
f,b,c,d 1:10,2:20,3:30,4:40
f,b,c,d 1:10,2:20,3:30,4:40
b. 我们要实现 id的第一列对应tim的第一列,以此类推,可以用以下函数posexplode(),可以将数据和index都取出来
select id,tim,single_id_index,single_id from test.a
lateral view posexplode(split(id,',')) t as single_id_index, single_id;
id tim single_id_index single_id
a,b,c,d 2:00,3:00,4:00,5:000 a
a,b,c,d 2:00,3:00,4:00,5:001 b
a,b,c,d 2:00,3:00,4:00,5:002 c
a,b,c,d 2:00,3:00,4:00,5:003 d
f,b,c,d 1:10,2:20,3:30,4:400 f
f,b,c,d 1:10,2:20,3:30,4:401 b
f,b,c,d 1:10,2:20,3:30,4:402 c
f,b,c,d 1:10,2:20,3:30,4:403 d
c. 使用两次posexplode()函数实现多列转多行
select id,tim,single_id,single_tim from test.a
lateral view posexplode(split(id,',')) t as single_id_index, single_id
lateral view posexplode(split(tim,',')) t as single_yim_index, single_tim
where single_id_index = single_yim_index;
id tim single_id single_tim
a,b,c,d 2:00,3:00,4:00,5:00 a 2:00
a,b,c,d 2:00,3:00,4:00,5:00 b 3:00
a,b,c,d 2:00,3:00,4:00,5:00 c 4:00
a,b,c,d 2:00,3:00,4:00,5:00 d 5:00
f,b,c,d 1:10,2:20,3:30,4:40 f 1:10
f,b,c,d 1:10,2:20,3:30,4:40 b 2:20
f,b,c,d 1:10,2:20,3:30,4:40 c 3:30
f,b,c,d 1:10,2:20,3:30,4:40 d 4:40