hive从两个数组创建Map或键/值对

jdg4fx2g  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(696)

我有两个数组,它们的值是1:1。我需要从这两个数组中创建一个键/值对或Map(key,value)。任何想法或建议都会有帮助。
当前表结构:

  1. USA WEST [NUMBER,Street,City] [135,Pacific,Irvine]
  2. USA WEST [NUMBER,Street,City] [1672,Madison,Denver]

预期的表结构:

  1. USA WEST [NUMBER:135,Street:Pacific,City:Irvine]
  2. USA WEST [NUMBER:1672,Street:Madison,City:Denver]

谢谢您

qvsjd97n

qvsjd97n1#

演示

(with子句仅用于演示)
假设角色 % 以及 & 不要出现在文本中

  1. with t as
  2. (
  3. select stack
  4. (
  5. 2
  6. ,'USA WEST',array('NUMBER','Street','City'),array('135','Pacific','Irvine')
  7. ,'USA WEST',array('NUMBER','Street','City'),array('1672','Madison','Denver')
  8. ) as (c1,a1,a2)
  9. )
  10. select c1
  11. ,str_to_map
  12. (
  13. substring_index
  14. (
  15. regexp_replace
  16. (
  17. concat_ws('%',a1,a2,'')
  18. ,'(?<e1>.*?)%(?=((?<e2>.*?)%){3})'
  19. ,'${e1}%${e2}&'
  20. )
  21. ,'&'
  22. ,size(a1)
  23. )
  24. ,'&'
  25. ,'%'
  26. ) as `map`
  27. from t
  28. ;
  1. +----------+------------------------------------------------------+
  2. | c1 | map |
  3. +----------+------------------------------------------------------+
  4. | USA WEST | {"NUMBER":"135","Street":"Pacific","City":"Irvine"} |
  5. | USA WEST | {"NUMBER":"1672","Street":"Madison","City":"Denver"} |
  6. +----------+------------------------------------------------------+

使用ascii值为1和2的字符也是一样的。

  1. with t as
  2. (
  3. select stack
  4. (
  5. 2
  6. ,'USA WEST',array('NUMBER','Street','City'),array('135','Pacific','Irvine')
  7. ,'USA WEST',array('NUMBER','Street','City'),array('1672','Madison','Denver')
  8. ) as (c1,a1,a2)
  9. )
  10. select c1
  11. ,str_to_map
  12. (
  13. substring_index
  14. (
  15. regexp_replace
  16. (
  17. concat_ws(string(unhex(1)),a1,a2,'')
  18. ,concat('(?<e1>.*?)',string(unhex(1)),'(?=((?<e2>.*?)',string(unhex(1)),'){3})')
  19. ,concat('${e1}',string(unhex(1)),'${e2}',string(unhex(2)))
  20. )
  21. ,string(unhex(2))
  22. ,size(a1)
  23. )
  24. ,string(unhex(2))
  25. ,string(unhex(1))
  26. ) as `map`
  27. from t
  28. ;
  1. +----------+------------------------------------------------------+
  2. | c1 | map |
  3. +----------+------------------------------------------------------+
  4. | USA WEST | {"NUMBER":"135","Street":"Pacific","City":"Irvine"} |
  5. | USA WEST | {"NUMBER":"1672","Street":"Madison","City":"Denver"} |
  6. +----------+------------------------------------------------------+
展开查看全部

相关问题