【问题标题】:how to extract a json object from list of json based on key in apache hive如何根据 apache hive 中的键从 json 列表中提取 json 对象
【发布时间】:2019-12-03 05:15:19
【问题描述】:

我需要根据 id 字段值从下面的 json 列表中提取一个 json 并将键显示为列

[{"id":"123","name":"ABC","age":"18","subject":"Maths","score":20},
{"id":"124","name":"ABCD","age":"20","subject":"History","score":40},
{"id":"213","name":"XYZ","age":"28","subject":"Economics","score":35}]

我希望根据以下键显示最终的 json : 123

id|name|age|subject|score
123|ABC|18|Maths|20

请建议在 hive 中实现上述方法。

【问题讨论】:

    标签: arrays json hive hiveql


    【解决方案1】:

    分解数组并使用 get_json_object 提取 JSON 元素。

    例子:

    with your_data as (
         select stack(1, array(
         '{"id":"123","name":"ABC","age":"18","subject":"Maths","score":20}',
         '{"id":"124","name":"ABCD","age":"20","subject":"History","score":40}',
         '{"id":"213","name":"XYZ","age":"28","subject":"Economics","score":35}')
         ) as json_array
    )
    
    select --t.json_array as initial_data,
           --a.json, 
           get_json_object(a.json, '$.id')      id,
           get_json_object(a.json, '$.name')    name,
           get_json_object(a.json, '$.age')     age,
           get_json_object(a.json, '$.subject') subject,
           get_json_object(a.json, '$.score')   score    
      from your_data t 
           lateral view outer explode(json_array) a as json
      where get_json_object(a.json, '$.id') = 123  ;
    

    结果:

    id      name    age     subject score
    123     ABC     18      Maths   20
    

    【讨论】:

      【解决方案2】:
      # Header 1 #   
       Writing a function to get the index of the object by mapping it with the key
      
      #Code#
      
      
      let obj = [{"id":"123","name":"ABC","age":"18","subject":"Maths","score":20},{"id":"124","name":"ABCD","age":"20","subject":"History","score":40},{"id":"213","name":"XYZ","age":"28","subject":"Economics","score":35}]
          function getColoumn(_key){
              for(i in obj){
               if(obj[i].id ===_key){
                  return obj[i];
                  }
              }
          }
          getColoumn("123")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-04
        • 2021-07-12
        • 1970-01-01
        • 1970-01-01
        • 2019-10-20
        • 2019-02-05
        • 2016-12-24
        相关资源
        最近更新 更多