Получение данных из столбца JSON в таблице SQL - PullRequest
0 голосов
/ 30 августа 2018

Интересно, как получить данные из столбца JSON в SQL. regexp_substr может работать или этот как его в таблице Vertica. Я довольно новичок и был бы очень благодарен за любую помощь, которая может помочь мне понять, как, например:

  • найти Prem_amount для конкретного OFFICE_ID

Должен ли я разделить всю структуру ниже на более мелкие кусочки как здесь , чтобы понять структуру?

[{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["1"],"intervals":[{"end_offset":464400,"start_at":null,"start_offset":457200,"end_at":null}],"id":"1","OFFICE_ID":"1","content_hash_id":"1","inc_params":{"is_commissionable":false,"prem_amount":25.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["2"],"intervals":[{"end_offset":550800,"start_at":null,"start_offset":543600,"end_at":null}],"id":"2","OFFICE_ID":"2","content_hash_id":"2","inc_params":{"is_commissionable":false,"prem_amount":25.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1.5"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["3"],"intervals":[{"end_offset":471600,"start_at":null,"start_offset":464400,"end_at":null}],"id":"3","OFFICE_ID":"3","content_hash_id":"3","inc_params":{"is_commissionable":false,"prem_amount":25.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1.5"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["4"],"intervals":[{"end_offset":558000,"start_at":null,"start_offset":550800,"end_at":null}],"id":"4","OFFICE_ID":"4","content_hash_id":"4","inc_params":{"is_commissionable":false,"prem_amount":25.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["5"],"intervals":[{"end_offset":478800,"start_at":null,"start_offset":471600,"end_at":null}],"id":"5","OFFICE_ID":"5","content_hash_id":"5","inc_params":{"is_commissionable":false,"prem_amount":20.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["6"],"intervals":[{"end_offset":565200,"start_at":null,"start_offset":558000,"end_at":null}],"id":"6","OFFICE_ID":"6","content_hash_id":"6","inc_params":{"is_commissionable":false,"prem_amount":20.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1.5"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["7"],"intervals":[{"end_offset":486000,"start_at":null,"start_offset":478800,"end_at":null}],"id":"7","OFFICE_ID":"7","content_hash_id":"7","inc_params":{"is_commissionable":false,"prem_amount":25.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1.5"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["8"],"intervals":[{"end_offset":572400,"start_at":null,"start_offset":565200,"end_at":null}],"id":"8","OFFICE_ID":"8","content_hash_id":"8","inc_params":{"is_commissionable":false,"prem_amount":25.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1.5"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["9"],"intervals":[{"end_offset":493200,"start_at":null,"start_offset":486000,"end_at":null}],"id":"9","OFFICE_ID":"9","content_hash_id":"9","inc_params":{"is_commissionable":false,"prem_amount":25.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1.5"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["10"],"intervals":[{"end_offset":579600,"start_at":null,"start_offset":572400,"end_at":null}],"id":"10","OFFICE_ID":"10","content_hash_id":"10","inc_params":{"is_commissionable":false,"prem_amount":25.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["11"],"intervals":[{"end_offset":500400,"start_at":null,"start_offset":493200,"end_at":null}],"id":"11","OFFICE_ID":"11","content_hash_id":"11","inc_params":{"is_commissionable":false,"prem_amount":20.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}},{"Constraint_a":{"match_all":false,"children":[{"match_all":true,"constraints":[{"operator":"GREATER_OR_EQUAL","property":"RATE","value":"0.850"},{"operator":"GREATER_OR_EQUAL","property":"CALLS_PER_HOUR","value":"1"},{"operator":"GREATER_OR_EQUAL","property":"IN_RATE","value":"1.000"}]}],"constraints":[]},"constraint_ids":["12"],"intervals":[{"end_offset":586800,"start_at":null,"start_offset":579600,"end_at":null}],"id":"12","OFFICE_ID":"12","content_hash_id":"12","inc_params":{"is_commissionable":false,"prem_amount":20.0,"fee":0.1},"filters":[],"visible_office_name":"site_trix","legacy_params":{}}]

Спасибо за помощь!

Ответы [ 2 ]

0 голосов
/ 30 августа 2018

Как вы получили данные JSON в Vertica? Вы кладете это в VARCHAR? Это не идеально, потому что вы не можете легко проверить его (как показывает этот ответ ) или извлечь значения из него. Вместо этого вы можете загрузить данные JSON, используя FJSONParser . Например:

vsql> create table super(age int, name varchar);

vsql> copy super from stdin parser fjsonparser();
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> {"age": 5, "name": "Tim"}
>>  {"age": 3}
>>  {"name": "Fred"}
>>  {"name": "Bob", "age": 10}
>> \.

vsql> select * from super;
 age | name
-----+------
     | Fred
  10 | Bob
   5 | Tim
   3 |
(4 rows)

Очевидно, что вы не будете вводить свой JSON на STDIN; это просто пример в документации. Вы можете загрузить файл JSON следующим образом:

vsql> COPY SE.PostLinks FROM '"$loaddir"/PostLinks.json' PARSER fjsonparser();

(Угадайте, с какими данными я экспериментировал. :-))

0 голосов
/ 30 августа 2018

JSON, который вы указали здесь, искажен. Вы можете быстро проверить, используя онлайн-валидатор JSON. Есть несколько сайтов, которые вы можете использовать. Например: JSON Formatter

Более подробную информацию о запросе данных JSON можно найти здесь: Данные JSON в SQL Server

...