regex_replace 函数跳过 NULL 值之后的任何内容

Posted

技术标签:

【中文标题】regex_replace 函数跳过 NULL 值之后的任何内容【英文标题】:regex_replace function skips anything coming after a NULL value 【发布时间】: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 时,它都会忽略后面的内容并转到下一个字段: 示例:

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

【问题讨论】:

【参考方案1】:

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

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

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

【讨论】:

以上是关于regex_replace 函数跳过 NULL 值之后的任何内容的主要内容,如果未能解决你的问题,请参考以下文章

为啥函数返回的值总是 null 或 undefined?

Oracle regex_replace 不排除某些值

future.then 块在返回类型整数的函数内跳过导致返回 null

oracle 查询条件中判断两个字段相等时 一个字段为空则跳过

Hive Regex_replace 给出了无法编译正则表达式模式:错误

有啥函数,啥可以获取当天的第一个值并在 Python 中跳过同一天的值?