使用 HIVE 查询解析 json 数组

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

我将 json 数组存储在表 (jt) 中,如下所示:

[{"col": 1, "row": 1, "code": "INVESTMENT_PERIOD", "name": "Thời gian đầu tư", "unit": "Năm", "order": 1, "value": 10, "dataType": "INTEGER", "htmlType": "INPUT", "targetId": 1, "attributeId": 5}, {"col": 2, "row": 1, "code": "RETIREMENT_FUND", "name": "Số tiền cần có tại thời điểm nghỉ hưu", "unit": "VND", "order": 2, "value": 10000000, "dataType": "FLOAT", "htmlType": "INPUT", "targetId": 1, "attributeId": 6}, {"col": 1, "row": 2, "code": "INIT_INVESTMENT", "name": "Số tiền đầu tư ban đầu", "unit": "VND", "order": 3, "value": 10000, "dataType": "FLOAT", "htmlType": "INPUT", "targetId": 1, "attributeId": 4}, {"col": 2, "row": 2, "code": "MONTHLY_INVEST", "name": "Số tiền đầu tư định kỳ", "unit": "VND", "order": 4, "value": 10000, "dataType": "FLOAT", "htmlType": "INPUT", "targetId": 1, "attributeId": 1}, {"col": 1, "row": 3, "code": "DEPOSIT_TERM", "name": "Ngày nộp tiền định kỳ", "unit": "", "order": 5, "value": 15, "dataType": "INTEGER", "htmlType": "SELECT", "targetId": 1, "attributeId": 3}]

每个数组都是一条记录。我想解析这个表以获得一个包含多个字段的新表:

  • monthly_invest:attributeId = 1 -> value
  • execute_date:attributeId = 2 -> value
  • deposit_term:attributeId = 3 -> value
  • init_investment:attributeId = 4 -> value
  • investment_period:attributeId = 5 -> value
  • 退休基金:attributeId = 6 -> value
  • investment_fund:attributeId = 7 -> value
arrays json hive
1个回答
0
投票

我已经解决了以下问题。

SELECT
    id
    , MAX(monthly_invest)
    , MAX(execute_date)
    , MAX(deposit_term)
    , MAX(init_investment)
    , MAX(investment_period)
    , MAX(retirement_fund)
    , MAX(investment_fund)
FROM
(
SELECT
    'id_test' id
    , CASE WHEN get_json_object(b.new_column, '$.attributeId') = 1 THEN get_json_object(b.new_column, '$.value') END monthly_invest
    , CASE WHEN get_json_object(b.new_column, '$.attributeId') = 2 THEN get_json_object(b.new_column, '$.value') END execute_date
    , CASE WHEN get_json_object(b.new_column, '$.attributeId') = 3 THEN get_json_object(b.new_column, '$.value') END deposit_term
    , CASE WHEN get_json_object(b.new_column, '$.attributeId') = 4 THEN get_json_object(b.new_column, '$.value') END init_investment
    , CASE WHEN get_json_object(b.new_column, '$.attributeId') = 5 THEN get_json_object(b.new_column, '$.value') END investment_period
    , CASE WHEN get_json_object(b.new_column, '$.attributeId') = 6 THEN get_json_object(b.new_column, '$.value') END retirement_fund
    , CASE WHEN get_json_object(b.new_column, '$.attributeId') = 7 THEN get_json_object(b.new_column, '$.value') END investment_fund
FROM 
(
    SELECT 
        split(regexp_replace('[{"col": 1, "row": 1, "code": "INVESTMENT_PERIOD", "name": "Thời gian đầu tư", "unit": "Năm", "order": 1, "value": 10, "dataType": "INTEGER", "htmlType": "INPUT", "targetId": 1, "attributeId": 5}, {"col": 2, "row": 1, "code": "RETIREMENT_FUND", "name": "Số tiền cần có tại thời điểm nghỉ hưu", "unit": "VND", "order": 2, "value": 10000000, "dataType": "FLOAT", "htmlType": "INPUT", "targetId": 1, "attributeId": 6}, {"col": 1, "row": 2, "code": "INIT_INVESTMENT", "name": "Số tiền đầu tư ban đầu", "unit": "VND", "order": 3, "value": 10000, "dataType": "FLOAT", "htmlType": "INPUT", "targetId": 1, "attributeId": 4}, {"col": 2, "row": 2, "code": "MONTHLY_INVEST", "name": "Số tiền đầu tư định kỳ", "unit": "VND", "order": 4, "value": 10000, "dataType": "FLOAT", "htmlType": "INPUT", "targetId": 1, "attributeId": 1}, {"col": 1, "row": 3, "code": "DEPOSIT_TERM", "name": "Ngày nộp tiền định kỳ", "unit": "", "order": 5, "value": 15, "dataType": "INTEGER", "htmlType": "SELECT", "targetId": 1, "attributeId": 3}]', '\\[|\\]', ''),'(?<=\\})\\s*,\\s*(?=\\{)') a
) b
LATERAL view explode(b.a) b as new_column
) c
GROUP BY id;

© www.soinside.com 2019 - 2024. All rights reserved.