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

时间:2017-09-12 21:49:51

标签: arrays hadoop dictionary hive key-value

我有两个数字相同的数组,它们映射1:1。我需要从这两个数组创建一个键/值对或映射(键,值)。任何想法或提示都会有所帮助。

当前表格结构:

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]

谢谢

1 个答案:

答案 0 :(得分:0)

演示

(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"} |
+----------+------------------------------------------------------+