【问题标题】:How to find the same elements in tow arrays of two different tables in HIVE?如何在 HIVE 中的两个不同表的两个数组中找到相同的元素?
【发布时间】:2020-05-28 10:31:23
【问题描述】:

我有两个如下表:

表1:

id               sid
1   |   ['101', '102', '103']
2   |   ['102', '101', '103']
3   |   ['103', '101', '102']

表2:

id  |            sid
1   |   ['101', '102', '103']
3   |   ['102', '103']

我希望得到下表:

id               sid
1   |   ['101', '102', '103']
2   |   ['102', '101', '103']
3   |   ['103', '102']

说明: 我希望在table1中以相同的顺序选择table1.sid和table2.sid中相同的元素。另外,如果table1 中的id 在table2 中不存在,则将sid 保持在table1 中。我该怎么办?

【问题讨论】:

    标签: sql hive hiveql


    【解决方案1】:

    您可以使用posexplode() 基本上做您想做的事。当然,所有这些数组内容在 Hive 中比在其他数据库中更复杂,尤其是按照您想要的顺序获取结果:

    select t1.id, collect_list(sid2)
    from (select t1.id, t2.sid2, t1.pos1
          from (table1 t1 lateral view
                posexplode(t1.sid) as pos1, sid1
               ) left join
               (table2 t2 lateral view
                posexplode(t2.sid) as pos2, sid2
               )
               on t2.id = t1.id and t2.sid2 = t1.sid1
          distribute by t1.id
          order by t1.id, t1.pos1
         ) t
    

    类似这样的:

    用 t 作为 ( 选择 t1.id,collect_list(sid2) 作为 sid 从(选择 t1.id、t2.sid2、t1.pos1 从 (table1 t1 侧视图 posexplode(t1.sid) 作为 pos1, sid1 ) 左连接 (table2 t2 侧视图 posexplode(t2.sid) 作为 pos2, sid2 ) 在 t2.id = t1.id 和 t2.sid2 = t1.sid1 按 t1.id 分发 按 t1.id、t1.pos1 排序 ) 吨 选择 t1.id, (大小(t.sid)= 0 然后 t1.sid 否则 t.sid 结束的情况) 从 t1 左加入 吨 在 t1.id = t.id

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-27
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多