从Vertica中的Flex表读取嵌套的json数据

时间:2017-11-20 19:45:40

标签: json vertica flextable

我有这个json文件。

        [
 {
"Modified": "2016-09-0",
"Id": 16,
"Name": "ABC",
   "Filters": [],
"ScoreComponents":[
  {
    "Id": 86,
    "Name": "Politeness",
     "Bins": [],
    "Ranges": [
      {
        "ComponentId": 86,                     
        "LastUser": "CDE\\John.Doe"
       },
      {
        "ComponentId": 86,
        "LastUser": "CDE\\John.Doe"
        }
      ],
    "Filters": []
  },
  {
    "Id": 87,
    "Name": "Empathy",
    "Bins": [],
    "Ranges": [
      {
        "ComponentId": 87,
        "LastUser": "CDE\\John.Doe"
         }
    ],
    "Filters": [
      {
        "ComponentID": -30356,
        "BucketID": 81
        }
    ]
  },
  {
    "Id": 88,
    "Name": "Ownership",
     "Bins": [],
    "Ranges": [
      {
        "ComponentId": 88,
        "User": "CDE\\John.Doe"

      }
    ],
    "Filters": []
  }]
 }
 ]

我已在Vertica flex表中加载此文件

     CREATE FLEX TABLE flex_test();
     copy events_stg.flex_test from LOCAL 'C:/test2.json' PARSER fjsonparser (flatten_maps= true, flatten_arrays = false)

我想读取ScoreComponents中的所有数据,包括嵌套数组。 我试过查询这个查询

select "Id" as scoreid,mapitems("ScoreComponents") OVER(PARTITION BY 
         "Id")  from flex_test

输出如:Outout of query

我只是不理解输出中的那些小方块。我是一名学生,这个vertica数据库和Flex表是新手。

我尝试使用flatten_arrays = true,但它给了我空的结果集。

1 个答案:

答案 0 :(得分:1)

您正在获得正方形,因为值字段包含二进制VMap。

这应该这样做:

create flex table so_flex();
create table so_score_components(
    id int,
    name varchar(100)
);
create table so_ranges(
    parent_id int,
    component_id int,
    last_user varchar(100)
);
create table so_filters(
    parent_id int,
    component_id int,
    bucket_id int
);

copy so_flex from local 'E:\Demos\so.json' 
parser fjsonparser(start_point='ScoreComponents', 
flatten_maps = false, flatten_arrays = false);

insert into so_score_components(id, name)
select id::int, name::varchar from so_flex;

insert into so_ranges(parent_id, component_id, last_user)
select id::int, values['ComponentId']::int, values['LastUser']::varchar
from (
    select id, mapitems(ranges) over (partition by id)  
    from so_flex
) t;

insert into so_filters(parent_id, component_id, bucket_id)
select id::int, values['ComponentID']::int, values['BucketID']::int
from (
    select id, mapitems(filters) over (partition by id)  
    from so_flex
) t;