如何从两个蜂巢数组中获取不匹配的元素
Posted
技术标签:
【中文标题】如何从两个蜂巢数组中获取不匹配的元素【英文标题】:How to get unmatched elements from two hive arrays 【发布时间】:2021-03-31 02:16:31 【问题描述】:我正在寻找一个 hive 函数和查询以从 Hive 中的两个数组中获取不匹配的元素。假设数组是
A = ["Hello", "earth"]
B = ["Hello", "mars"]
Expected output is ["earth", "mars"] or ["mars", "earth"]
另一个例子
A = ["Hello", "world"]
B = ["Hello", "world", "!"]
Expected output is ["!"]
【问题讨论】:
【参考方案1】:在子查询中分解两个数组,FULL JOIN子查询并过滤不匹配的记录,然后使用collect_set聚合它们得到数组。
演示:
with your_data as (
select array ("Hello", "earth") A,
array("Hello", "mars") B
)
select collect_set(coalesce(A.element_A,B.element_B)) as result
from
(select element_A
from your_data d lateral view explode(A) e as element_A
)A
FULL JOIN
(select element_B
from your_data d lateral view explode(B) e as element_B
)B
ON A.element_A=B.element_B
WHERE A.element_A is NULL OR B.element_B is NULL
结果:
["earth","mars"]
【讨论】:
以上是关于如何从两个蜂巢数组中获取不匹配的元素的主要内容,如果未能解决你的问题,请参考以下文章
如果与提供的 id 匹配,如何从对象数组中获取 slug 值?