将N个数组合并为一个?
Merge N arrays into one?
我有一个名为 table 的连接,它存储 property_ids 及其现有的 API 连接。一个 属性 可以在同一个 connection_id
上有多个 apis
。一个 属性 也可以有多个 connection_id
。 apis
的重要性顺序为升序,因此 API 1 的重要性高于 API 14.
考虑到上述情况,我正在尝试 select 单个 connection_id per 属性 per 天。给定以下数据:
+------------+-------------+---------------+----------------+
| yyyy_mm_dd | property_id | connection_id | apis |
+------------+-------------+---------------+----------------+
| 2019-10-01 | 100 | 123 | ['8'] |
| 2019-10-01 | 100 | 200 | ['16'] |
| 2019-10-01 | 100 | 5 | ['1','2','14'] |
+------------+-------------+---------------+----------------+
我想要返回以下内容(因为 connection_id
5 拥有最低的 API 连接):
+------------+-------------+---------------+
| yyyy_mm_dd | property_id | connection_id |
+------------+-------------+---------------+
| 2019-10-01 | 100 | 5 |
+------------+-------------+---------------+
我想实现这一点,我可以合并数组,然后在 select 索引 0 处的项目之前对它们进行升序排序。但是,我觉得这可能过于复杂了。
在集合函数下我没有看到任何合并函数 - https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-CollectionFunctions。也许这可以在不合并的情况下实现?
如果您需要 connection_id 最低的 api,那么您可以对数组进行排序,然后记录最低的 api[0]
select yyyy_mm_dd, property_id, connection_id
from
(
select yyyy_mm_dd, property_id, connection_id, apis,
row_number() over(partition by yyyy_mm_dd, property_id order by api0 ) rn
from
(
select yyyy_mm_dd, property_id, connection_id, apis, sort_array(apis)[0] as api0
from mytable
)s
)s
where rn=1;
如果数组是字符串,而不是整数,那么它不能与排序一起使用,您可以分解数组,将其转换为 int 并获取最低的记录 API:
select yyyy_mm_dd, property_id, connection_id
from
(
select yyyy_mm_dd, property_id, connection_id, apis,
row_number() over(partition by yyyy_mm_dd, property_id order by api ) rn
from
(
select t.yyyy_mm_dd, t.property_id, t.connection_id, t.apis, cast(e.api as int) as api
from mytable t
lateral view explode(apis) e as api
)s
)s
where rn=1;
我有一个名为 table 的连接,它存储 property_ids 及其现有的 API 连接。一个 属性 可以在同一个 connection_id
上有多个 apis
。一个 属性 也可以有多个 connection_id
。 apis
的重要性顺序为升序,因此 API 1 的重要性高于 API 14.
考虑到上述情况,我正在尝试 select 单个 connection_id per 属性 per 天。给定以下数据:
+------------+-------------+---------------+----------------+
| yyyy_mm_dd | property_id | connection_id | apis |
+------------+-------------+---------------+----------------+
| 2019-10-01 | 100 | 123 | ['8'] |
| 2019-10-01 | 100 | 200 | ['16'] |
| 2019-10-01 | 100 | 5 | ['1','2','14'] |
+------------+-------------+---------------+----------------+
我想要返回以下内容(因为 connection_id
5 拥有最低的 API 连接):
+------------+-------------+---------------+
| yyyy_mm_dd | property_id | connection_id |
+------------+-------------+---------------+
| 2019-10-01 | 100 | 5 |
+------------+-------------+---------------+
我想实现这一点,我可以合并数组,然后在 select 索引 0 处的项目之前对它们进行升序排序。但是,我觉得这可能过于复杂了。
在集合函数下我没有看到任何合并函数 - https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-CollectionFunctions。也许这可以在不合并的情况下实现?
如果您需要 connection_id 最低的 api,那么您可以对数组进行排序,然后记录最低的 api[0]
select yyyy_mm_dd, property_id, connection_id
from
(
select yyyy_mm_dd, property_id, connection_id, apis,
row_number() over(partition by yyyy_mm_dd, property_id order by api0 ) rn
from
(
select yyyy_mm_dd, property_id, connection_id, apis, sort_array(apis)[0] as api0
from mytable
)s
)s
where rn=1;
如果数组是字符串,而不是整数,那么它不能与排序一起使用,您可以分解数组,将其转换为 int 并获取最低的记录 API:
select yyyy_mm_dd, property_id, connection_id
from
(
select yyyy_mm_dd, property_id, connection_id, apis,
row_number() over(partition by yyyy_mm_dd, property_id order by api ) rn
from
(
select t.yyyy_mm_dd, t.property_id, t.connection_id, t.apis, cast(e.api as int) as api
from mytable t
lateral view explode(apis) e as api
)s
)s
where rn=1;