hive查询按位置Map3个数组列

tzdcorbm  于 2021-05-27  发布在  Hadoop
关注(0)|答案(2)|浏览(603)
  1. i/p:
  2. c1 c2 c3
  3. [[1,2,3],[4],[5,6]] ['v1','v2','v3'] [['sam'], ['tam'], ['bam']]
  4. o/p:
  5. c1 c2 c3
  6. [1,2,3] 'v1' ['sam']
  7. [4] 'v2' ['tam']
  8. [5,6] 'v3' ['bam']

有人能建议我如何写一个关于上述问题的查询吗?

o0lyfsai

o0lyfsai1#

使用 explode :

  1. select explode(c1) as c1 from tab;

或与一起使用 lateral view 如果您的用例更复杂:

  1. select
  2. c1_exploded,
  3. a,b,c
  4. from
  5. tab t
  6. lateral view explode(t.c1) tf as c1_exploded
  7. ;

裁判:https://cwiki.apache.org/confluence/display/hive/languagemanual+udf

0ve6wy6x

0ve6wy6x2#

使用posexplode():

  1. with your_data as (
  2. select array(array(1,2,3),array(4),array(5,6)) c1, array('v1','v2','v3') c2, array(array('sam'), array('tam'), array('bam')) c3
  3. --returns [[1,2,3],[4],[5,6]] ["v1","v2","v3"] [["sam"],["tam"],["bam"]]
  4. )
  5. select a1.c1, a2.c2, a3.c3
  6. from your_data d
  7. lateral view posexplode(d.c1) a1 as p1, c1
  8. lateral view posexplode(d.c2) a2 as p2, c2
  9. lateral view posexplode(d.c3) a3 as p3, c3
  10. where a1.p1=a2.p2 and a1.p1=a3.p3 --match positions in exploded arrays
  11. --without this where condition
  12. --lateral views will produce cartesian product
  13. --alternatively you can explode arrays in subqueries and join them
  14. --using positions, in such way you can do left-join, not only inner
  15. ;

结果:

  1. OK
  2. c1 c2 c3
  3. [1,2,3] v1 ["sam"]
  4. [4] v2 ["tam"]
  5. [5,6] v3 ["bam"]
  6. Time taken: 0.078 seconds, Fetched: 3 row(s)

简化版,感谢@grzegorzskibinski的建议:

  1. with your_data as (
  2. select array(array(1,2,3),array(4),array(5,6)) c1, array('v1','v2','v3') c2, array(array('sam'), array('tam'), array('bam')) c3
  3. --returns [[1,2,3],[4],[5,6]] ["v1","v2","v3"] [["sam"],["tam"],["bam"]]
  4. )
  5. select a1.c1, d.c2[a1.p1] as c2, d.c3[a1.p1] as c3
  6. from your_data d
  7. lateral view posexplode(d.c1) a1 as p1, c1
  8. ;
展开查看全部

相关问题