Hive 如何将数据转移到配置单元
这是我的蜂巢桌:Hive 如何将数据转移到配置单元,hive,hiveql,unpivot,Hive,Hiveql,Unpivot,这是我的蜂巢桌: dept_id emp_cnt emp_cnt_prev_yr sales_cnt sales_cnt_prev_yr 1 10 8 10000 5000 2 15 9 20000 12000 3 12 10 30000 15000 4 6 12 40000
dept_id emp_cnt emp_cnt_prev_yr sales_cnt sales_cnt_prev_yr
1 10 8 10000 5000
2 15 9 20000 12000
3 12 10 30000 15000
4 6 12 40000 20000
我想将数据存储到另一个配置单元表中,如下所示:
dept_id metric_nm metric_val metric_val_prev-yr
1 emp_cnt 10 8
2 emp_cnt 15 9
3 emp_cnt 12 10
4 emp_cnt 6 12
1 sales_cnt 10000 5000
2 sales_cnt 20000 12000
3 sales_cnt 30000 15000
4 sales_cnt 40000 20000
到目前为止,我尝试的是:
SELECT dept_id,
metric_nm,
Substr(metrics,1,Locate('#',metrics,1)-1) AS metric_val,
Substr(metrics,Locate('#',metrics,1)+1) AS metric_val_prev_yr
FROM (
SELECT dept_id,
Map('emp_cnt', Concat(emp_cnt,'#',emp_cnt_prev_yr),'sales_cnt', Concat(sales_cnt,'#',sales_cnt_prev_yr)) AS metrc
FROM <TABLE>) a lateral VIEW explode(metric) ext AS metric_nm,
metrics;
使用UNION ALL将两个公制数据集合并为一个:
insert overwrite table table_name
select dept_id, 'emp_cnt' as metric_nm,
emp_cnt as metric_val, emp_cnt_prev_yr as metric_val_prev_yr
from your_table
UNION ALL
select dept_id, 'sales_cnt' as metric_nm,
sales_cnt as metric_val, sales_cnt_prev_yr as metric_val_prev_yr
from your_table;
还有一种方法与堆栈度量值_nm交叉连接以乘以行x度量值的数量,此交叉连接将作为映射连接执行:
--configuration
set hive.cli.print.header=true;
set hive.execution.engine=tez;
set hive.mapred.reduce.tasks.speculative.execution=false;
set mapred.reduce.tasks.speculative.execution=false;
set hive.exec.parallel=true;
set hive.exec.parallel.thread.number=36;
set hive.vectorized.execution.enabled=true;
set hive.vectorized.execution.reduce.enabled=true;
set hive.auto.convert.join=true; --this enables map-join
select dept_id, s.metric_nm,
case s.metric_nm when 'emp_cnt' then emp_cnt
when 'sales_cnt' then sales_cnt
--add more cases
end as metric_val,
case s.metric_nm when 'emp_cnt' then emp_cnt_prev_yr
when 'sales_cnt' then sales_cnt_prev_yr
--add more cases
end as metric_val_prev_yr
from your_table
cross join
(select stack (2, --number of values, add more
'sales_cnt',
'emp_cnt'
--add more values
) as metric_nm
)s
使用UNION ALL将两个公制数据集合并为一个:
insert overwrite table table_name
select dept_id, 'emp_cnt' as metric_nm,
emp_cnt as metric_val, emp_cnt_prev_yr as metric_val_prev_yr
from your_table
UNION ALL
select dept_id, 'sales_cnt' as metric_nm,
sales_cnt as metric_val, sales_cnt_prev_yr as metric_val_prev_yr
from your_table;
还有一种方法与堆栈度量值_nm交叉连接以乘以行x度量值的数量,此交叉连接将作为映射连接执行:
--configuration
set hive.cli.print.header=true;
set hive.execution.engine=tez;
set hive.mapred.reduce.tasks.speculative.execution=false;
set mapred.reduce.tasks.speculative.execution=false;
set hive.exec.parallel=true;
set hive.exec.parallel.thread.number=36;
set hive.vectorized.execution.enabled=true;
set hive.vectorized.execution.reduce.enabled=true;
set hive.auto.convert.join=true; --this enables map-join
select dept_id, s.metric_nm,
case s.metric_nm when 'emp_cnt' then emp_cnt
when 'sales_cnt' then sales_cnt
--add more cases
end as metric_val,
case s.metric_nm when 'emp_cnt' then emp_cnt_prev_yr
when 'sales_cnt' then sales_cnt_prev_yr
--add more cases
end as metric_val_prev_yr
from your_table
cross join
(select stack (2, --number of values, add more
'sales_cnt',
'emp_cnt'
--add more values
) as metric_nm
)s
欢迎来到SO!到目前为止你尝试了什么?请相应地编辑您的问题。从选择部门id、映射“emp\u cnt”、“CONCATemp\u cnt”、“emp\u cnt\u prev\u yr”、“sales\u cnt”、“CONCATsales\u cnt”、“sales\u cnt”、“sales\u prev\u yr”的横向视图中选择部门id、度量值、子度量值、1、1-1作为度量值、子度量值、定位、度量值、1+1作为度量值上一个度量值,韵律学;欢迎来到SO!到目前为止你尝试了什么?请相应地编辑您的问题。从选择部门id、映射“emp\u cnt”、“CONCATemp\u cnt”、“emp\u cnt\u prev\u yr”、“sales\u cnt”、“CONCATsales\u cnt”、“sales\u cnt”、“sales\u prev\u yr”的横向视图中选择部门id、度量值、子度量值、1、1-1作为度量值、子度量值、定位、度量值、1+1作为度量值上一个度量值,韵律学;谢谢你的回复。你们的解决方案运行良好,但若我们有多个指标,那个么我相信下面的解决方案将是最优的。选择部门id、度量值、子度量值、1、定位、度量值、1-1作为度量值、子度量值、定位、度量值、1+1作为度量值上一年,从选择部门id、映射“emp_cnt”、CONCATemp_cnt、emp_cnt_上一年、“sales_cnt”、CONCATsales_cnt、sales_cnt、sales_cnt、sales_cnt__上一年作为度量值,从侧面看,将度量值扩展为度量值、度量值@你可以查一下。在我看来,行的乘法是一种更便宜的方法。使用交叉连接和选择堆栈2、'sales\u cnt'、'emp\u cnt'sThanks进行回复。你们的解决方案运行良好,但若我们有多个指标,那个么我相信下面的解决方案将是最优的。选择部门id、度量值、子度量值、1、定位、度量值、1-1作为度量值、子度量值、定位、度量值、1+1作为度量值上一年,从选择部门id、映射“emp_cnt”、CONCATemp_cnt、emp_cnt_上一年、“sales_cnt”、CONCATsales_cnt、sales_cnt、sales_cnt、sales_cnt__上一年作为度量值,从侧面看,将度量值扩展为度量值、度量值@你可以查一下。在我看来,行的乘法是一种更便宜的方法。使用交叉连接选择堆栈2、'sales\u cnt'、'emp\u cnt's