【问题标题】:regex_replace function skips anything coming after a NULL valueregex_replace 函数跳过 NULL 值之后的任何内容
【发布时间】:2021-02-27 20:30:59
【问题描述】:

在我的配置单元表“ticket_full”中,我有一个名为“service_id”的 json 类型列,我想在 3 列中提取它,就像这样

[{"position":"1","typeid":"ROUTNAME","value":"PWAW13197"},{"position":"2","typeid":"CDCNAME","value":null},{"position":"3","typeid":"SVCNAME","value":"Business"},{"position":"4","typeid":"USID","value":"FI021MLQE4"}]

[{"position":"1","typeid":"ROUTNAME","value":"KHLA30076"},{"position":"2","typeid":"CDCNAME","value":"eff-e-rjh-sw-cs2"},{"position":"3","typeid":"SVCNAME","value":"Managed LAN"},{"position":"4","typeid":"USID","value":"SA00BNGH0E"}]

[{"position":"1","typeid":"NUMLIAPTT","value":"0492212984"},{"position":"2","typeid":null,"value":null},{"position":"3","typeid":null,"value":null},{"position":"4","typeid":null,"value":null}]

我使用了下面的代码:

SELECT get_json_object(single_json_table.identifiant_produit, '$.position') AS position,
  get_json_object(single_json_table.identifiant_produit, '$.typeid') AS typeid,
  get_json_object(single_json_table.identifiant_produit, '$.value') AS value
  FROM   
(SELECT explode(split(regexp_replace(substr(serviceid, 2, length(serviceid)-2),
            '"},\\{"', '"},,,,{"'), ',,,,')  ) as identifiant_produit 
  FROM ticket_full) single_json_table

它可以工作,但每次有一个值为 NULL 时,它都会忽略后面的内容并转到下一个字段: 示例:

请问有人知道怎么解决吗?

【问题讨论】:

    标签: json hive hiveql regexp-replace json-extract


    【解决方案1】:

    这是因为 null 没有双引号,而您将这个 '"},\\{"' 替换为这个 '"},,,,{"'

    尝试在正则表达式模式中删除 } 之前的双引号并相应地替换字符串,然后它也适用于带引号的值和空值:

    split(regexp_replace(substr(serviceid, 2, length(serviceid)-2),
                '},\\{"', '},,,,{"'), ',,,,')
    

    【讨论】:

      猜你喜欢
      • 2021-11-23
      • 1970-01-01
      • 2018-04-11
      • 1970-01-01
      • 2019-05-24
      • 2020-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多