【问题标题】:hive create map or key/value pair from two arrayshive 从两个数组创建映射或键/值对
【发布时间】:2017-09-14 03:37:48
【问题描述】:

我有两个具有相同数量值的数组,它们映射为 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]

谢谢

【问题讨论】:

  • 总是 3 个元素?总是 [NUMBER,Street,City]?
  • 元素的数量根据不同的消息不断变化。
  • 在您的帖子中添加列名

标签: arrays hadoop dictionary hive key-value


【解决方案1】:

演示

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-15
    • 2019-10-27
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 2022-07-22
    相关资源
    最近更新 更多