在 hive 中使用 group by 对数组类型进行collect_set

Rah*_*rma 3 hadoop hive

我有下表,其中包含 id 上的重复项以及每个 id 的值数组,我想找出每个 id 的唯一值,该怎么做?

CREATE TABLE test(
id string,
values array<string>)
Run Code Online (Sandbox Code Playgroud)

当我运行下面的命令时,它会抛出错误,因为collect_set仅支持原始类型值。

select id, collect_set(values) from ts group by id;
Run Code Online (Sandbox Code Playgroud)

错误:

FAILED: UDFArgumentTypeException 仅接受原始类型参数,但数组作为参数 1 传递。

Amb*_*ish 5

正如错误消息所示Only primitive type arguments are accepted but array was passed as parameter 1.,您需要在使用之前将数组转换为 String。

使用即可实现相同的explode()功能。就像是:

select 
  id, 
  collect_set(tokens) 
FROM
  ts LATERAL VIEW explode(values) x AS tokens
group by
  id;
Run Code Online (Sandbox Code Playgroud)