前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >使用 Hive 炸裂函数 explode (map<string,string>) 宽表转高表

使用 Hive 炸裂函数 explode (map<string,string>) 宽表转高表

作者头像
一个会写诗的程序员
发布于 2021-12-24 06:34:57
发布于 2021-12-24 06:34:57
2.5K00
代码可运行
举报
运行总次数:0
代码可运行

Hive 炸裂函数 explode(map<string,string>) 宽表转高表SQL:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
select  
slice_id,
user_id,
shop_id,
'user_stats_public' as table_code,
explode(kv) as (field_code,field_value)
from 
(   select 
    user_id,
    -1 as shop_id,
    abs(hash(user_id) % 20000) as  slice_id, 
    map(
    'residence_city_name',residence_city_name,
    'residence_city_level',residence_city_level,
    'gender', gender,
    'age', age,
    'activeness_level' , activeness_level,
    'consuming_level' , consuming_level,
    'pay_amt_td_level' , pay_amt_td_level,
    'old_ecom_user' , old_ecom_user
    ) as kv
from  ecom_kunlun.types_mapping_table_13_1639904624993 
where date = max_pt('ecom_kunlun.types_mapping_table_13_1639904624993') ) a;

explode() : Usage Examples

explode (array)
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
select explode(array('A','B','C'));
select explode(array('A','B','C')) as col;
select tf.* from (select 0) t lateral view explode(array('A','B','C')) tf;
select tf.* from (select 0) t lateral view explode(array('A','B','C')) tf as col;
explode (map)
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
select explode(map('A',10,'B',20,'C',30));
select explode(map('A',10,'B',20,'C',30)) as (key,value);
select tf.* from (select 0) t lateral view explode(map('A',10,'B',20,'C',30)) tf;
select tf.* from (select 0) t lateral view explode(map('A',10,'B',20,'C',30)) tf as key,value;
posexplode (array)
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
select posexplode(array('A','B','C'));
select posexplode(array('A','B','C')) as (pos,val);
select tf.* from (select 0) t lateral view posexplode(array('A','B','C')) tf;
select tf.* from (select 0) t lateral view posexplode(array('A','B','C')) tf as pos,val;
inline (array of structs)
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
select inline(array(struct('A',10,date '2015-01-01'),struct('B',20,date '2016-02-02')));
select inline(array(struct('A',10,date '2015-01-01'),struct('B',20,date '2016-02-02'))) as (col1,col2,col3);
select tf.* from (select 0) t lateral view inline(array(struct('A',10,date '2015-01-01'),struct('B',20,date '2016-02-02'))) tf;
select tf.* from (select 0) t lateral view inline(array(struct('A',10,date '2015-01-01'),struct('B',20,date '2016-02-02'))) tf as col1,col2,col3;
stack (values)
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
select stack(2,'A',10,date '2015-01-01','B',20,date '2016-01-01');
select stack(2,'A',10,date '2015-01-01','B',20,date '2016-01-01') as (col0,col1,col2);
select tf.* from (select 0) t lateral view stack(2,'A',10,date '2015-01-01','B',20,date '2016-01-01') tf;
select tf.* from (select 0) t lateral view stack(2,'A',10,date '2015-01-01','B',20,date '2016-01-01') tf as col0,col1,col2;

Using the syntax "SELECT udtf(col) AS colAlias..." has a few limitations:

  • No other expressions are allowed in SELECT
    • SELECT pageid, explode(adid_list) AS myCol... is not supported
  • UDTF's can't be nested
    • SELECT explode(explode(adid_list)) AS myCol... is not supported
  • GROUP BY / CLUSTER BY / DISTRIBUTE BY / SORT BY is not supported
    • SELECT explode(adid_list) AS myCol ... GROUP BY myCol is not supported

Please see LanguageManual LateralView for an alternative syntax that does not have these limitations.

Also see Writing UDTFs if you want to create a custom UDTF.

参考资料

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验