从Clickhouse的JSON列中提取所有可能的键

发布于 2025-02-12 03:43:10 字数 329 浏览 1 评论 0原文

我在Clickhouse中有一个具有属性列的表。此列是 JSON类型列,并存储一个具有多个键和值的对象。没有数组,只有一个具有多个键和值的对象。

我要做的是构建列中存在的所有唯一密钥名称的列表。

因此,如果该列具有此对象:

{ age: '25'}

以及此对象:

{ genre: 'pop'}

我想检索age类型作为此查询的结果。

有什么办法可以做到吗?

I have a table in Clickhouse that has a properties column. This column is a JSON type column and stores an object with several keys and values. No arrays, just a single object with multiple keys and values.

What I am trying to do is to build a list of all the unique key names present in the column.

So, if the column has this object:

{ age: '25'}

And also this object:

{ genre: 'pop'}

I'd like to retrieve age and genre as results for this query.

Any way this can be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南汐寒笙箫 2025-02-19 03:43:10

您可以使用“ draction命令condict_extend_object_types = 1;

例如:

DESCRIBE TABLE json_test
SETTINGS describe_extend_object_types = 1

Query id: bfa406b0-948c-460f-9e05-6981b5f52270

┌─name──┬─type───────────────────────────┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ key   │ UInt32                         │              │                    │         │                  │                │
│ value │ Tuple(json_key Int8, value String) │              │                    │         │                  │                │
└───────┴────────────────────────────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘

JSON_TEST表中的“值”列实际上是JSON类型,但是它当前存储两个键,JSON_KEY是INT8,value ,这是字符串类型。

我相信目前,您必须分析描述表查询的结果,以在另一个查询中使用这些列名/键。

You can retrieve all of the inferred columns/keys from a ClickHouse JSON column using the DESCRIBE command with the setting describe_extend_object_types=1;

For example:

DESCRIBE TABLE json_test
SETTINGS describe_extend_object_types = 1

Query id: bfa406b0-948c-460f-9e05-6981b5f52270

┌─name──┬─type───────────────────────────┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ key   │ UInt32                         │              │                    │         │                  │                │
│ value │ Tuple(json_key Int8, value String) │              │                    │         │                  │                │
└───────┴────────────────────────────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘

The value column in the json_test table is actually a JSON type, but it currently stores two keys, json_key which is an Int8, and value, which is a String type.

I believe at the moment you'd have to parse the results of the DESCRIBE TABLE query to use those column names/keys in another query.

超可爱的懒熊 2025-02-19 03:43:10

我最终意识到我的用例更适合地图列类型。

将其更改为地图后,做我想做的事很容易:

SELECT DISTINCT arrayJoin(mapKeys(properties)) AS keys
FROM groups

I ended up realizing that my use case is better suited for a MAP column type.

After changing it to Map, doing what I wanted was very easy:

SELECT DISTINCT arrayJoin(mapKeys(properties)) AS keys
FROM groups
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文