json - 通过配置单元中的 get_json_object 将一行分隔为具有唯一 ID 的几行
问题描述
我正在尝试通过 Hive 中的 get_json_object 将一行分成具有唯一问题引用的几行,我可以这样做吗?
我只是在 Hive 中触摸 get_json_object,尝试将一行分隔为具有唯一问题引用(来自一个 json 格式列)的几行,并包含其他列信息。
SELECT reference AS item_reference,
get_json_object(questions, '$.reference') AS question_reference,
get_json_object(questions, '$.type') AS question_type
from sandbox.exportitems limit 10;
例如,给定输入:
reference | questions
NP002_025 | [{"reference":"3dfc54c0","type":"clozeformula"}]
DP001_1_10 | [{"reference":"73879547","type":"imageclozeformula"},
{"reference":"466a5b88","type":"clozedropdown"}]
预期的输出是:
reference | questions_reference | questions_type
NP002_025 | 3dfc54c0 | clozeformula
DP001_1_10 | 73879547 | imageclozeformula
DP001_1_10 | 466a5b88 | clozedropdown
解决方案
好的,下面是一个例子:
with core as (
SELECT
'DP001_1_10' as reference,
explode(
split(
regexp_replace(
regexp_replace(
regexp_replace('[{"reference":"73879547","type":"imageclozeformula"},{"reference":"466a5b88","type":"clozedropdown"}]', '\\]','')
,'\\}\\,\\{','\\}\\;\\{')
,'\\[','')
,'\\;')
) as json_str
)
select
reference,
get_json_object(json_str,'$.reference') as questions_reference,
get_json_object(json_str,'$.type') as questions_type
from
core;
+-------------+----------------------+--------------------+--+
| reference | questions_reference | questions_type |
+-------------+----------------------+--------------------+--+
| DP001_1_10 | 73879547 | imageclozeformula |
| DP001_1_10 | 466a5b88 | clozedropdown |
+-------------+----------------------+--------------------+--+
您可以将示例字符串'DP001_1_10'
和替换'[{"reference":"73879547","type":"imageclozeformula"},{"reference":"466a5b88","type":"clozedropdown"}]'
为您的列名reference
and questions
。所以你想要的最终 hql 可能如下所示:
with core as (
select
reference,
explode(
split(
regexp_replace(
regexp_replace(
regexp_replace(questions, '\\]','')
,'\\}\\,\\{','\\}\\;\\{')
,'\\[','')
,'\\;')
) as json_str
from
sandbox.exportitems
)
select
reference,
get_json_object(json_str,'$.reference') as questions_reference,
get_json_object(json_str,'$.type') as questions_type
from
core;
推荐阅读
- perf - 解释 perf 脚本列
- javascript - 使用谷歌图表在水平栏中的垂直线注释
- mysql - 如何确保插入到表中的值存在于另一个表中?
- java - 来自 Http 请求的 400 错误请求使用 java
- apache-spark - Spark 常量 IndexOutOfBoundsException 警告
- python - Python:如何将 csv 文件中的空文件编码为数字升序
- django - Django中的ModelForm没有保存任何东西
- php - 为什么我们在 AJAX 调用中不需要 dataType: "json" (有条件地)?
- wordpress - 使用三层架构在 AWS 上托管 WordPress
- pine-script - Pine Script 无状态函数实现