HIVE:如何访问MAP对象中的元素

问题描述 投票:0回答:1

在我的HIVE表MYTABLE中,我有一列“ MYCOL”包含以下内容:

{"id": "a651b57f", 

 "items": {
    "ITEM1": {
        "code": "CODE1", 
        "name": "NAME1"}, 
    "ITEM2": {
        "code": "CODE2", 
        "name": "NAME2"}}, 

 "myinfo": {
    "c7daf1a9": {
        "id": "c7daf1a9", 
        "name": "newname", 
        "type": "newtype", 
        "appliedto": ["ITEM1", "ITEM2"]}}, 

 "info2": 12}

我想将元素访问到“ myinfo”中,并且尝试了类似的操作:

select  GET_JSON_OBJECT(t.MYCOL,'$.myinfo') FROM MYTABLE

但是它不起作用。...

有人可以帮我吗?

谢谢

dictionary object hive access
1个回答
0
投票

确保HDFS文件中的数据具有one lineeach json row(一行没有多行)。

  • 如果json行有多行新行,那么在存储到HDFS中之前,我们需要替换每行的所有新行。

示例:

HDFS file data:

{"id": "a651b57f","items": {"ITEM1": {"code": "CODE1","name": "NAME1"},"ITEM2": {"code": "CODE2","name": "NAME2"}},"myinfo": {"c7daf1a9": {"id": "c7daf1a9","name": "newname","type": "newtype","appliedto": ["ITEM1", "ITEM2"]}},"info2": 12}

Hive:

with cte as (select string('{"id": "a651b57f","items": {"ITEM1": {"code": "CODE1","name": "NAME1"},"ITEM2": {"code": "CODE2","name": "NAME2"}},"myinfo": {"c7daf1a9": {"id": "c7daf1a9","name": "newname","type": "newtype","appliedto": ["ITEM1", "ITEM2"]}},"info2": 12}')my_col) --sample data
select get_json_object(my_col,'$.myinfo')jsn from cte;

Output:

{"c7daf1a9":{"id":"c7daf1a9","name":"newname","type":"newtype","appliedto":["ITEM1","ITEM2"]}}
© www.soinside.com 2019 - 2024. All rights reserved.