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

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

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

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

预期的表结构:

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

谢谢您

qvsjd97n

qvsjd97n1#

演示

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

with    t as 
        (
            select  stack
                    (
                        2
                       ,'USA WEST',array('NUMBER','Street','City'),array('135','Pacific','Irvine')
                       ,'USA WEST',array('NUMBER','Street','City'),array('1672','Madison','Denver')
                    ) as (c1,a1,a2)
        )

select  c1
       ,str_to_map
        (
            substring_index
            (
                regexp_replace
                (
                    concat_ws('%',a1,a2,'')
                   ,'(?<e1>.*?)%(?=((?<e2>.*?)%){3})'
                   ,'${e1}%${e2}&'
                )
               ,'&'
               ,size(a1)
            )
           ,'&'
           ,'%'
        )   as `map`

from    t
;
+----------+------------------------------------------------------+
|    c1    |                         map                          |
+----------+------------------------------------------------------+
| USA WEST | {"NUMBER":"135","Street":"Pacific","City":"Irvine"}  |
| USA WEST | {"NUMBER":"1672","Street":"Madison","City":"Denver"} |
+----------+------------------------------------------------------+

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

with    t as 
        (
            select  stack
                    (
                        2
                       ,'USA WEST',array('NUMBER','Street','City'),array('135','Pacific','Irvine')
                       ,'USA WEST',array('NUMBER','Street','City'),array('1672','Madison','Denver')
                    ) as (c1,a1,a2)
        )

select  c1
       ,str_to_map
        (
            substring_index
            (
                regexp_replace
                (
                    concat_ws(string(unhex(1)),a1,a2,'')
                   ,concat('(?<e1>.*?)',string(unhex(1)),'(?=((?<e2>.*?)',string(unhex(1)),'){3})')
                   ,concat('${e1}',string(unhex(1)),'${e2}',string(unhex(2)))
                )
               ,string(unhex(2))
               ,size(a1)
            )
           ,string(unhex(2))
           ,string(unhex(1))
        )   as `map`

from    t
;
+----------+------------------------------------------------------+
|    c1    |                         map                          |
+----------+------------------------------------------------------+
| USA WEST | {"NUMBER":"135","Street":"Pacific","City":"Irvine"}  |
| USA WEST | {"NUMBER":"1672","Street":"Madison","City":"Denver"} |
+----------+------------------------------------------------------+

相关问题