如何从配置单元表中的json字符串中提取数组元素?
Posted
技术标签:
【中文标题】如何从配置单元表中的json字符串中提取数组元素?【英文标题】:How to extract array elements from json string in hive table? 【发布时间】:2021-06-28 17:23:19 【问题描述】:我有一个包含 json 字符串的配置单元表:
column |
---|
"id": 123, "ShortName":"test tax","LongName":"test","Description":"test","aFactor":"true","bFactor":"true","cFactor":"false" |
"id": 456, "ShortName":"test tax","LongName":"test","Description":"test","aFactor":"false","bFactor":"true","cFactor":"true" |
"id": 678, "ShortName":"test tax","LongName":"test","Description":"test","aFactor":"false","cFactor":"true" |
我想从这里创建一个数组,只要 aFactor、bFactor 或 cFactor 为真:
id | factors |
---|---|
123 | aFactor,bFactor |
456 | bFactor,cFactor |
678 | cFactor |
为 false 或 null 是等价的。
【问题讨论】:
【参考方案1】:您可以使用以下步骤执行此操作 -
-
首先确定 *Factor 字段的位置。
如果它们为真则使用,然后将它们连接起来
整个查询应如下所示 -
select get_json_object(str,'$.id') as id,
if(get_json_object(str,'$.aFactor')='true','aFactor','') ||if(get_json_object(str,'$.bFactor')='true',',bFactor','')||if(get_json_object(str,'$.cFactor')='true',',cFactor','') factors
from mytable
示例结果在屏幕截图中。 测试查询
with tab as ( select '"id": 123, "ShortName":"test tax","LongName":"test","Description":"test","aFactor":"true","bFactor":"true","cFactor":"false"' str)
select get_json_object(str,'$.id') as id,
if(get_json_object(str,'$.aFactor')='true','aFactor','') ||
if(get_json_object(str,'$.bFactor')='true',',bFactor','') ||
if(get_json_object(str,'$.cFactor')='true',',cFactor','')
factors
【讨论】:
以上是关于如何从配置单元表中的json字符串中提取数组元素?的主要内容,如果未能解决你的问题,请参考以下文章
从 postgres 表中提取 json 数组给出错误:无法从标量中提取元素