配置单元-从json提取数组

ccgok5k5  于 2021-06-26  发布在  Hive
关注(0)|答案(2)|浏览(408)

我有一个包含一些值数组的表。

create external table apidetails
(
  inputdetails sting
)
Location 'XXXX'
``` `select inputdetails from apidetails` ```
{ "Name": "api-server1", "ID": "api-1", "tags": ["tag-1","tag-2"] }

我需要结果
|id |标签|
|-------|

j1dl9f46

j1dl9f461#

api-1标签-1、标签-2

我尝试选择json提取标量(inputdetails,“$tags”),但它返回错误。

igsr9ssn

igsr9ssn2#

以下是一些选项:

选项1:json

select  json_extract_scalar(inputdetails ,'$.ID') as ID
       ,json_extract(inputdetails ,'$.tags')      as tags

from    apidetails
;
ID   |       tags
-------+-------------------
 api-1 | ["tag-1","tag-2"]

选项2:数组(varchar)

select  json_extract_scalar(inputdetails ,'$.ID')                       as ID
       ,cast(json_extract(inputdetails ,'$.tags') as array(varchar))    as tags

from    apidetails
;
ID   |      tags
-------+----------------
 api-1 | [tag-1, tag-2]

选项3:分隔字符串

select  json_extract_scalar(inputdetails ,'$.ID')                                       as ID
       ,array_join(cast(json_extract(inputdetails ,'$.tags') as array(varchar)),',')    as tags

from    apidetails
;
ID   |    tags
-------+-------------
 api-1 | tag-1,tag-2

相关问题