Hive 在蜂箱中解组/分解
可以在配置单元中解组数据集吗?我不相信横向视图可以分解成整数 当前表格:Hive 在蜂箱中解组/分解,hive,group-by,aggregate,Hive,Group By,Aggregate,可以在配置单元中解组数据集吗?我不相信横向视图可以分解成整数 当前表格: event count A 3 B 2 结果表: event count A 1 A 1 A 1 B 1 B 1 计数列在结果中显然不是非常重要。一个选项是创建一个数字表并使用它进行分解 --create numbers table create table if not exists dbname.numbers location 'some_hdfs_
event count
A 3
B 2
结果表:
event count
A 1
A 1
A 1
B 1
B 1
计数列在结果中显然不是非常重要。一个选项是创建一个数字表并使用它进行分解
--create numbers table
create table if not exists dbname.numbers
location 'some_hdfs_location' as
select stack(5,1,2,3,4,5) t as num --increase the number of values as needed
--Disaggregation
select a.event,n.num --or a.cnt
from dbname.agg_table a
join dbname.numbers n on true
where a.cnt >= n.num and a.cnt <= n.num
——创建数字表
如果不存在,则创建表dbname.numbers
位置“一些hdfs位置”为
选择堆栈(5,1,2,3,4,5)t作为num——根据需要增加值的数量
--分解
选择a.event、n.num--或a.cnt
从dbname.agg_表a
在true上连接dbname.n
其中a.cnt>=n.num和a.cnt如果要分解聚合的记录数很高,并且您不想硬编码它
创建一个将返回序号的自定义项
[prjai@lnx0689 py_ws]$ cat prime_num.py
import sys
try:
for line in sys.stdin:
num = int(line)
for i in range(1, num+1):
#print u"i".encode('utf-8')
print u"%i".encode('utf-8') %(i)
except:
print sys.exc_info()
将python脚本添加到配置单元环境
hive> add FILE /home/prjai/prvys/py_ws/prime_num.py
为上述脚本创建临时表
hive> create temporary table t1 as with t1 as (select transform(10) using 'python prime_num.py' as num1) select * from t1;
你的问题是-
hive> with t11 as (select 'A' as event, 3 as count) select t11.event, t11.count from t11, t1 where t11.count>=t1.num1;
希望这有帮助。使用space()
函数,您可以将count
转换为长度为count-1的空格字符串,然后使用split()
将其转换为数组,并使用横向视图将其分解()
生成行。
只需将我的演示中的a
子查询替换为您的表即可
演示:
select a.event,
1 as count --calculate count somehow if necessary
from
(select stack(2,'A',3,'B',2) as (event, count)) a --Replace this subquery with your table name
lateral view explode(split(space(a.count-1),' ')) s
;
结果:
OK
A 1
A 1
A 1
B 1
B 1
Time taken: 0.814 seconds, Fetched: 5 row(s)