是否可以在配置单元中解组数据集?我不相信你能把一个整数分解。当前表格:
event countA 3B 2
event count
A 3
B 2
结果表:
event countA 1A 1A 1B 1B 1
A 1
B 1
计数列在结果中显然不是超重要的。
uidvcgyl1#
如果要聚合的记录数很高,并且您不想硬编码它。创建一个将返回数字序列的自定义项
[prjai@lnx0689 py_ws]$ cat prime_num.pyimport 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()
[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;
希望这有帮助。
mi7gmzs62#
一种方法是创建一个数字表并使用它进行分解。
--create numbers tablecreate 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--Disaggregationselect a.event,n.num --or a.cntfrom dbname.agg_table a join dbname.numbers n on truewhere a.cnt >= n.num and a.cnt <= n.num
--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
ztigrdn83#
使用 space() 可以转换的函数 count 要创建长度为count-1的空格字符串,请使用 split() 将其转换为数组和 explode() 与 lateral view 生成行。只需更换 a 在我的演示子查询与您的表。演示:
space()
count
split()
explode()
lateral view
a
select a.event, 1 as count --calculate count somehow if necessaryfrom (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;
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
;
结果:
OKA 1A 1A 1B 1B 1Time taken: 0.814 seconds, Fetched: 5 row(s)
OK
Time taken: 0.814 seconds, Fetched: 5 row(s)
3条答案
按热度按时间uidvcgyl1#
如果要聚合的记录数很高,并且您不想硬编码它。
创建一个将返回数字序列的自定义项
将python脚本添加到配置单元环境
为上述脚本创建临时表
你的问题是-
希望这有帮助。
mi7gmzs62#
一种方法是创建一个数字表并使用它进行分解。
ztigrdn83#
使用
space()
可以转换的函数count
要创建长度为count-1的空格字符串,请使用split()
将其转换为数组和explode()
与lateral view
生成行。只需更换a
在我的演示子查询与您的表。演示:
结果: