如何根据 apache hive 中的键从 json 列表中提取 json 对象

Posted

技术标签:

【中文标题】如何根据 apache hive 中的键从 json 列表中提取 json 对象【英文标题】:how to extract a json object from list of json based on key in apache hive 【发布时间】: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 中实现上述方法。

【问题讨论】:

【参考方案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")

【讨论】:

以上是关于如何根据 apache hive 中的键从 json 列表中提取 json 对象的主要内容,如果未能解决你的问题,请参考以下文章

根据指定的键从集合中创建键值对集合

如何将tabpanel的键从视图传递到控制器asp mvc

如何根据ios中的特定键从数组中获取字典

Underscore.js:使用在对象中找到的键从对象列表中创建映射

根据codeigniter中的外键从两个表中获取json编码数据

Json - 扁平化 Hive 中的键和值