在配置单元中取消分组/分解

zf9nrax1  于 2021-06-27  发布在  Hive
关注(0)|答案(3)|浏览(435)

是否可以在配置单元中解组数据集?我不相信你能把一个整数分解。
当前表格:

  1. event count
  2. A 3
  3. B 2

结果表:

  1. event count
  2. A 1
  3. A 1
  4. A 1
  5. B 1
  6. B 1

计数列在结果中显然不是超重要的。

uidvcgyl

uidvcgyl1#

如果要聚合的记录数很高,并且您不想硬编码它。
创建一个将返回数字序列的自定义项

  1. [prjai@lnx0689 py_ws]$ cat prime_num.py
  2. import sys
  3. try:
  4. for line in sys.stdin:
  5. num = int(line)
  6. for i in range(1, num+1):
  7. #print u"i".encode('utf-8')
  8. print u"%i".encode('utf-8') %(i)
  9. except:
  10. print sys.exc_info()

将python脚本添加到配置单元环境

  1. hive> add FILE /home/prjai/prvys/py_ws/prime_num.py

为上述脚本创建临时表

  1. hive> create temporary table t1 as with t1 as (select transform(10) using 'python prime_num.py' as num1) select * from t1;

你的问题是-

  1. hive> with t11 as (select 'A' as event, 3 as count) select t11.event, t11.count from t11, t1 where t11.count>=t1.num1;

希望这有帮助。

展开查看全部
mi7gmzs6

mi7gmzs62#

一种方法是创建一个数字表并使用它进行分解。

  1. --create numbers table
  2. create table if not exists dbname.numbers
  3. location 'some_hdfs_location' as
  4. select stack(5,1,2,3,4,5) t as num --increase the number of values as needed
  5. --Disaggregation
  6. select a.event,n.num --or a.cnt
  7. from dbname.agg_table a
  8. join dbname.numbers n on true
  9. where a.cnt >= n.num and a.cnt <= n.num
ztigrdn8

ztigrdn83#

使用 space() 可以转换的函数 count 要创建长度为count-1的空格字符串,请使用 split() 将其转换为数组和 explode()lateral view 生成行。只需更换 a 在我的演示子查询与您的表。
演示:

  1. select a.event,
  2. 1 as count --calculate count somehow if necessary
  3. from
  4. (select stack(2,'A',3,'B',2) as (event, count)) a --Replace this subquery with your table name
  5. lateral view explode(split(space(a.count-1),' ')) s
  6. ;

结果:

  1. OK
  2. A 1
  3. A 1
  4. A 1
  5. B 1
  6. B 1
  7. Time taken: 0.814 seconds, Fetched: 5 row(s)
展开查看全部

相关问题