是否可以在 Hive 中分组后连接字符串字段

Is it possible to concat a string field after group by in Hive

我正在评估 Hive,需要在分组依据之后进行一些字符串字段连接。我找到了一个名为 "concat_ws" 的函数,但看起来我必须明确列出所有要连接的值。我想知道我是否可以在 Hive 中使用 concat_ws 做这样的事情。这是一个例子。所以我有一个名为 "my_table" 的 table,它有两个名为国家和城市的字段。我想每个国家只有一条记录,每条记录都有两个字段 - 国家和城市:

select country, concat_ws(city, "|") as cities
from my_table
group by country

这在 Hive 中可行吗?我现在正在使用来自 CDH5 的 Hive 0.11

In database management an aggregate function is a function where the values of multiple rows are grouped together as input on certain criteria to form a single value of more significant meaning or measurement such as a set, a bag or a list.

来源:Aggregate function - Wikipedia

Hive 的开箱即用 聚合函数 在以下网页上列出:
Built-in Aggregate Functions (UDAF - user defined aggregation function)

因此,唯一的内置选项(对于 Hive 0.11;对于 Hive 0.13 及更高版本,您有 collect_list)是:
array collect_set(col)

如果每个 country 没有重复的 city 记录(returns 一组消除了重复元素的对象),这将回答您的请求。否则创建您自己的 UDAF 或在 Hive 之外聚合。

编写UDAF的参考资料: