首页 > 解决方案 > Hive/SQL 查询以从提交“详细信息”的表结构中提取关键字“swid”的值

问题描述

Hive/SQL 查询以从提交“详细信息”的表结构中提取关键字“swid”的值。

栏——“ Details

价值 -"id":123;"name":"Alex";"depID":100;"swid":5456213

期望的输出:

swid
5456213

标签: sqlhivehiveql

解决方案


使用sts_to_map函数:

 with test_data as (select '"id":123\\;"name":"Alex"\\;"depID":100\\;"swid":5456213' as str)

 select str, str_to_map(regexp_replace(str,'\\"',''),'\\;',':')['swid'] as swid from test_data
;

结果:

OK
str                                                     swid
"id":123;"name":"Alex";"depID":100;"swid":5456213       5456213
Time taken: 0.971 seconds, Fetched: 1 row(s)

另一种解决方案是转换为有效的 JSON(用逗号替换分号),然后使用 get_json_object 提取元素:

with test_data as (select '"id":123\\;"name":"Alex"\\;"depID":100\\;"swid":5456213' as str)

select str, get_json_object(concat('{',regexp_replace(str,'\\;',','),'}'),'$.swid')  as swid from test_data;
OK
str                                                     swid
"id":123;"name":"Alex";"depID":100;"swid":5456213       5456213
Time taken: 6.54 seconds, Fetched: 1 row(s)

使用regexp_extract

select str, regexp_extract(str,'\\"swid\\":(\\d+)',1) as swid from test_data;

推荐阅读