有什么方法可以更新JSON列?

发布于 2025-01-26 05:23:37 字数 213 浏览 1 评论 0原文

ALTER TABLE
managers
UPDATE
JSONExtractString(managers.extra_data, 'name') = JSONExtractString(other_table.extra_data, 'name')
WHERE 1

上面的查询不起作用,也找不到类似于jsonb_set函数的东西

ALTER TABLE
managers
UPDATE
JSONExtractString(managers.extra_data, 'name') = JSONExtractString(other_table.extra_data, 'name')
WHERE 1

Query above does not work, also couldn't find something similar to jsonb_set function like in psql

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

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

发布评论

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

评论(2

人生百味 2025-02-02 05:23:37

您可以用ether_table作为源创建字典,然后使用
jsonextractString(dictget('my_dict',managers.key),'name')从dict读取每个特定键的数据。
就您而言,无法理解如何在这两个表中加入行。

You can create dictionary with other_table as source and then use
JSONExtractString(dictGet('my_dict', managers.key), 'name') to read data from dict for each specific key.
In your case there is no way to understand how to join rows in this two tables.

靑春怀旧 2025-02-02 05:23:37

clickhouse 23.10 给我们新功能 jsonmergepatch ,这绝对可以帮助更新JSON字段。

更新一个密钥的值更新

alter table 
managers
update 
extra_data = jsonMergePatch(extra_data, '{"surname": "test"}')
where id = 1

一行的许多键

alter table 
managers
update 
extra_data = jsonMergePatch(extra_data, '{"surname": "test", "name": "test2"}')
where id = 1

更新许多行,使用第二个表

注意:在这里您可以找到,如何更新非json字段。

让我们假设,您需要为所有经理更新名称。例如,您拥有具有这样的结构id,new_value的CSV文件。然后,您需要:

  1. 使用主键创建表和new_value的名称
create table names_data_temp (`id` Int32,`new_value` String) ENGINE = Join(ANY, LEFT, id) as
  select
    id
    new_value
  from file('/path/to/file', CSVWithNames);

​​注意:文件必须在clickhouse-server目录中,名为user_files

  1. 更新带有新名称的管理员数据,保存已经现有的数据
alter table 
managers 
update 
extra_data = jsonMergePatch(extra_data, concat('{"name":"', joinGet(names_data_temp, 'new_value', id), '"}'))
where true;

注意:不建议更新Clickhouse中的所有表! 位置的过滤

  1. 您应该有关闭
drop table names_data_temp;

Clickhouse 23.10 give us new function jsonMergePatch, that definitely can help updating json fields.

Updating value for one key

alter table 
managers
update 
extra_data = jsonMergePatch(extra_data, '{"surname": "test"}')
where id = 1

Updating many keys for one row

alter table 
managers
update 
extra_data = jsonMergePatch(extra_data, '{"surname": "test", "name": "test2"}')
where id = 1

Updating many rows, using second table

NOTE: Here you can find, how to update non-json fields.

Let's assume, you need to update names for all managers. For example, you have csv-file with such structure id,new_value. Then, you need to:

  1. Create table with primary key and new_value for your names
create table names_data_temp (`id` Int32,`new_value` String) ENGINE = Join(ANY, LEFT, id) as
  select
    id
    new_value
  from file('/path/to/file', CSVWithNames);

NOTE: File must be in clickhouse-server directory, named user_files

  1. Update managers data with new names, saving already existing data
alter table 
managers 
update 
extra_data = jsonMergePatch(extra_data, concat('{"name":"', joinGet(names_data_temp, 'new_value', id), '"}'))
where true;

NOTE: it's not recommended to update all table in clickhouse! You should have filter for where-closure.

  1. Delete no-more-needed table
drop table names_data_temp;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文