【问题标题】:Hive Explode and extract a value from a StringHive 爆炸并从字符串中提取值
【发布时间】:2020-01-18 03:58:40
【问题描述】:

伙计们,我正在尝试从配置单元中的字符串(列名:人)下方提取“状态”的值。问题是,该列既不是完整的 JSON,也不是存储为数组。

我试图通过将 '=' 替换为 ':' 来使其看起来像 JSON,但没有帮助。

[{name=abc, org=true, self=true, status=accepted, email=abc@gmail.com}, {name=cab abc, org=false, self=false, status=needsAction, email=cab@google.com}]

以下是我使用的查询:

SELECT 
  str.name,
  str.org, 
  str.status 
FROM table 
LATERAL VIEW EXPLODE (TRANSLATE(people,'=',':')) exploded as str;

但我遇到以下错误:

FAILED: UDFArgumentException explode() takes an array or a map as a parameter

需要这样的输出:

name    | org   | status
-------- ------- ------------
abc     | true  | accepted
cab abc | false | needsAction

注意:已经有一个表,数据类型是字符串,我 无法更改表架构。

【问题讨论】:

    标签: sql hadoop hive apache-spark-sql hiveql


    【解决方案1】:

    Hive 解决方案。它可能可以优化。阅读代码中的cmets:

    with your_table as ( --your data example, you select from your table instead
     select "[{name=abc, org=true, self=true, status=accepted, email=abc@gmail.com}, {name=cab abc, org=false, self=false, status=needsAction, email=cab@google.com}]" str
    )
    
     select --get map values
            m['org']    as org    , 
            m['name']   as name   , 
            m['self']   as self   , 
            m['status'] as status , 
            m['email']  as email 
      from
     (--remove spaces after commas, convert to map     
      select str_to_map(regexp_replace(a.s,', +',','),',','=') m --map
        from your_table t --replace w your table
             lateral view explode(split(regexp_replace(str,'\\[|\\{|]',''),'}, *')) a as s --remove extra characters: '[' or '{' or ']', split and explode
     )s;
    

    结果:

    OK
    true    abc     true    accepted        abc@gmail.com
    false   cab abc false   needsAction     cab@google.com
    Time taken: 1.001 seconds, Fetched: 2 row(s) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      • 2011-12-10
      • 2020-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多