sql - Hive/SQL 查询以从提交“详细信息”的表结构中提取关键字“swid”的值
问题描述
Hive/SQL 查询以从提交“详细信息”的表结构中提取关键字“swid”的值。
栏——“ Details
”
价值 -"id":123;"name":"Alex";"depID":100;"swid":5456213
期望的输出:
swid
5456213
解决方案
使用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;
推荐阅读
- java - 石英和 mysql 的自动模式创建失败并出现 MySQLSyntaxException
- python - 如果子消息没有字段,如何在 protobuf 消息上分配 oneof 字段?
- linux - 停止没有端口号的 SSH 访问
- html - 出现在菜单前面的图像
- python - 使用字典遍历 json 列表
- qt - 创建项目时隐藏 QTreeView 的项目
- php - 代码中的 OOP 关联暗示不违反封装
- jquery - Angular 6 ReferenceError: $ is not defined error with JQuery
- ios - 使所有子类符合特定协议
- azure - 尝试从 azure VM 连接到 cosmosDB 时连接超时