【问题标题】:How to extract nested JSON Object in Hive如何在 Hive 中提取嵌套的 JSON 对象
【发布时间】:2021-03-04 02:57:51
【问题描述】:

我有一个名为“影响”的列,其中包含嵌套 Json 格式的数据

输入: [{"internalid":"079","impactid":[{"position":"1","typeid":"NOEUD","value":"G1"},{"position":"2","typeid":"ID","value":"001"},{"position":"3","typeid":"CODE_CI","value":"14"}],"typeid":"BTS","cdrs":"X110","belong":"OF","impactclass":"R","count":"0","numberaccessimpacted":"0","impactcalculationrequest":null},{"internalid":"6381075","impactid":[{"position":"1","typeid":"NOEUD","value":"G3"},{"position":"2","typeid":"ID","value":"003"},{"position":"3","typeid":"CI","value":"58"}],"typeid":"BTS","cdrs":"X110","belong":"OF","impactclass":"R","count":"0","numberaccessimpacted":"0","impactcalculationrequest":null},{"internalid":"6381071","impactid":[{"position":"1","typeid":"NOEUD","value":"G2"},{"position":"2","typeid":"IDT","value":"002"},{"position":"3","typeid":"CI","value":"57"}],"typeid":"BTS","cdrs":"X110","belong":"OF","impactclass":"R","count":"0","numberaccessimpacted":"0","impactcalculationrequest":null}]

我使用下面的代码:

SELECT 
       get_json_object(single_json_table.identifiant, '$.position') AS impact_position,
       get_json_object(single_json_table.identifiant, '$.value')  AS impact_value
   FROM 
   (SELECT exp2.identifiant
  FROM  socle s
  lateral view explode(split(regexp_replace(substr(impact, 2, length(impact)-2),
          '},\\{"', '},,,,{"'), ',,,,')) exp2 as identifiant   
           )single_json_table 

这是结果,它跳过了第一个位置和值,有人知道我该如何解决吗?

impact_position  |  impact_value
(null)                (null)
2                     001
3                     14
(null)                (null)
2                     003
3                     58
(null)                (null)
2                     002
3                     57 

【问题讨论】:

    标签: arrays json hive hiveql json-extract


    【解决方案1】:

    您的输入是带有嵌套数组的 JSON。上层数组是整个输入,包含 struct > >, impactid 是一个嵌套数组,包含这样的结构元素:{"position":"1","typeid":"NOEUD","value":"G1"}

    您需要分解两个数组。先分解上层数组:改变分隔符,拆分,分解,然后对嵌套数组做同样的事情。

    演示:

    with socle as (
    select '[{"internalid":"079","impactid":[{"position":"1","typeid":"NOEUD","value":"G1"},{"position":"2","typeid":"ID","value":"001"},{"position":"3","typeid":"CODE_CI","value":"14"}],"typeid":"BTS","cdrs":"X110","belong":"OF","impactclass":"R","count":"0","numberaccessimpacted":"0","impactcalculationrequest":null},{"internalid":"6381075","impactid":[{"position":"1","typeid":"NOEUD","value":"G3"},{"position":"2","typeid":"ID","value":"003"},{"position":"3","typeid":"CI","value":"58"}],"typeid":"BTS","cdrs":"X110","belong":"OF","impactclass":"R","count":"0","numberaccessimpacted":"0","impactcalculationrequest":null},{"internalid":"6381071","impactid":[{"position":"1","typeid":"NOEUD","value":"G2"},{"position":"2","typeid":"IDT","value":"002"},{"position":"3","typeid":"CI","value":"57"}],"typeid":"BTS","cdrs":"X110","belong":"OF","impactclass":"R","count":"0","numberaccessimpacted":"0","impactcalculationrequest":null}]'
    as impact
    
    )
    
    select internalid,
           get_json_object(e.impact, '$.position')  as position,
           get_json_object(e.impact, '$.value')  as value
    from
    (
    select get_json_object(impacts, '$.internalid') internalid,
          --extract inner impact array, remove [], convert delimiters 
           regexp_replace(regexp_replace(get_json_object(impacts,'$.impactid'),'^\\[|\\]$',''),'\\},\\{','},,,,{') impact
    from 
    (
    SELECT --First we need to explode upper array. Since it is a string, 
           --we need to prepare delimiters to be able to explode it
           --remove first [ and last ], replace delimiters between inner structure with 4 commas
           regexp_replace(regexp_replace(s.impact,'^\\[|\\]$',''),'\\},\\{"internalid"','},,,,{"internalid"') upper_array_str 
      FROM  socle s
    )s lateral view explode (split(upper_array_str, ',,,,')) e as impacts --get upper array element
    )s lateral view explode (split(impact, ',,,,') ) e as impact
    

    结果:

    internalid  position    value
    079         1          G1
    079         2          001
    079         3          14
    6381075     1          G3
    6381075     2          003
    6381075     3          58
    6381071     1          G2
    6381071     2          002
    6381071     3          57
    

    【讨论】:

    • 很抱歉打扰你@leftjoin,我对那个解决方案有一点问题,所以我有一堆我需要获取的其他变量,以及 json 列中的内容,其中一个是id 和该列没有正确显示,缺少很多行。你能帮帮我吗?
    • @Stella 好的。详细描述你的问题。如果这不相关,最好提出新问题
    • @lefjoin 正如我所说,我需要获取一个字符串类型的列名“t_id”,以及 JSON 类型的列“影响”,所以如果我想检索t_id: "20W064" 在第一个选择中:SELECT t_id, regexp_replace(regexp_replace(s.impacts_impact,'^\\[|\\]$',''),'\\},\\{"internalid"','},,,,{"internalid"') upper_array_str FROM socle s where t_id = "20W064" 它可以工作,但是当我使用横向视图爆炸时,没有为该 ID 获取任何结果。我不知道问题是侧视图还是其他问题。你能帮忙吗?
    • @Stella 横向视图的工作方式类似于内部连接。如果横向视图没有返回分解的行,因为数组是空的,则没有从主表返回的行。使用LATERAL VIEW OUTER... 而不是LATERAL VIEW - 然后如果数组为空或不存在,它将从主表返回行(如左连接)。如果这对您的问题没有帮助,请准备一些有代表性的数据示例并查询
    • 非常感谢!!,其实只是侧视图,我不得不换成侧视图外。
    猜你喜欢
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    相关资源
    最近更新 更多