访问 clickhouse 中 json 内的所有值

Access all the values inside the json in clickhouse

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

device
{"brand_name":'huawei,'brand_id':'1232',''country:'china'}
{"brand_name":'sony,'brand_id':'1ds232',''country:'japan'}

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

brand_name brand_id country
huawei 1232 china
sony 1ds232 japan

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

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

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

在标准中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   │
└───┴────────────┴──────────┴─────────┘