在clickhouse中访问json中的所有值

Jb-99

我有一列“设备”,其中包含这样的行 json 值

设备
{"brand_name":'huawei,'brand_id':'1232',''国家:'china'}
{"brand_name":'sony,'brand_id':'1ds232',''国家:'日本'}

我想像这样为json中的每个元素创建一个列

品牌 品牌标识 国家
华为 1232 中国
索尼 1ds232 日本

在标准 SQL 中,我已经这样做了,

Select
device.brand_name
device.brand_id
device.country
From table

我想在clickhouse中执行此操作,在这种情况下JSON只有三个值(brand_name,brand_id,country)但是如果JSON有n个值怎么办,所以我想要做的是而不是通过设备访问JSON中的每个值.brand_name,device.brand_id....等,我想循环其中的所有值并将其作为一列

在标准 SQL 中,我已经实现了这个

Select
device.*
From table

,有没有办法在clickhouse中做到这一点?, 谢谢

丹尼起重机

{brand_name:'huawei,'brand_id':'1232',''country:'china'}

不是有效的 JSON。


新的 JSON (22.3) 功能https://github.com/ClickHouse/ClickHouse/issues/23516

set allow_experimental_object_type=1;
create table testj( A Int64, device JSON ) Engine=MergeTree order by A;

insert into testj (device) format TSV {"brand_name":"huawei","brand_id":"1232","country":"china"}


select A, device.brand_name, device.brand_id, device.country from testj;
┌─A─┬─device.brand_name─┬─device.brand_id─┬─device.country─┐
│ 0 │ huawei            │ 1232            │ china          │
└───┴───────────────────┴─────────────────┴────────────────┘


SELECT * FROM testj;
┌─A─┬─device────────────────────┐
│ 0 │ ('1232','huawei','china') │
└───┴───────────────────────────┘

SELECT toJSONString(device) FROM testj
┌─toJSONString(device)────────────────────────────────────────┐
│ {"brand_id":"1232","brand_name":"huawei","country":"china"} │
└─────────────────────────────────────────────────────────────┘

https://kb.altinity.com/altinity-kb-queries-and-syntax/jsonextract-to-parse-many-attributes-at-a-time/

https://kb.altinity.com/altinity-kb-schema-design/altinity-kb-jsonasstring-and-mat.-view-as-json-parser/

https://clickhouse.com/docs/en/sql-reference/functions/json-functions/#jsonextractjson-indices-or-keys-return-type

create table testj( A Int64, device String, 
     brand_name String default JSONExtractString(device,'brand_name'), 
     brand_id String  default JSONExtractString(device,'brand_id'), 
     country String  default JSONExtractString(device,'country') ) 
Engine=MergeTree order by A;

insert into testj (device) format TSV {"brand_name":"huawei","brand_id":"1232","country":"china"}
                               ;

select A, brand_name, brand_id, country from testj;
┌─A─┬─brand_name─┬─brand_id─┬─country─┐
│ 0 │ huawei     │ 1232     │ china   │
└───┴────────────┴──────────┴─────────┘

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章