【问题标题】:Aggregation On Struct columns Hive结构列 Hive 上的聚合
【发布时间】:2017-06-21 21:22:00
【问题描述】:

我有一个结构数组,我正在尝试查找结构列的计数、总和、不同值。

create table temp (regionkey smallint, name string, comment string, nations array<struct<n_nationkey:smallint,n_name:string,n_comment:string>>) 
ROW FORMAT DELIMITED 
FIELDS TERMINATED BY '|' 
MAP KEYS TERMINATED BY ',';

当我尝试运行查询时

select name, 
count(nations.n_nationkey) as count, 
sum(nations.n_nationkey) as sum, 
ndv(nations.n_nationkey) as distinct_val 
from temp 
group by name 
order by name;

我得到了错误

FAILED: UDFArgumentTypeException Only primitive type arguments are accepted but array<smallint> is passed.

我要做的是找到 n_nationkey 的计数、总和和不同的值。

任何帮助将不胜感激。

【问题讨论】:

  • ndv? ......
  • 抱歉忘记在 hive 中编码了。

标签: arrays hadoop struct hive bigdata


【解决方案1】:
select      t.name 
           ,count   (e.col.n_nationkey)             as count 
           ,sum     (e.col.n_nationkey)             as sum
           ,count   (distinct e.col.n_nationkey)    as distinct_val 

from        temp t lateral view explode (t.nations) e

group by    t.name 

order by    t.name
;

对于 OP

具有别名的相同解决方案。
nations 不是结构。它是结构的数组
它没有n_nationkey 属性。它具有具有n_nationkey 属性的结构元素。
explode 函数采用结构数组 (nations) 并在单独的行中返回每个结构 (nation)。

select      t.name 
           ,count   (e.nation.n_nationkey)             as count 
           ,sum     (e.nation.n_nationkey)             as sum
           ,count   (distinct e.nation.n_nationkey)    as distinct_val 

from        temp t lateral view explode (t.nations) e as nation

group by    t.name 

order by    t.name
;

【讨论】:

  • e.col.n_nation,我理解 e 和 column_name,col 是关键字还是我们可以只做 e.n_nationkey?因为当我运行上面的代码时,我收到了这个错误消息'FAILED: SemanticException [Error 10025]: Line 1:20 Expression not in GROUP BY key'col'
  • 德里克,检查更新的答案。您在这里苦苦挣扎的原因是您没有提供数据样本。如果您想了解数据问题,您应该查看数据,而不是代码。
  • 嘟嘟,非常感谢,这对我帮助很大,我只是有点困惑,不是每个结构都已经有了别名吗?再次感谢。
  • e 是表函数的别名,同样t 是表的别名。
猜你喜欢
  • 2021-07-26
  • 2018-11-07
  • 1970-01-01
  • 2022-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-01
  • 1970-01-01
相关资源
最近更新 更多