跳到主要内容
跳到主要内容

cluster, clusterAllReplicas

允许访问集群(在 remote_servers 部分配置)的所有分片,而无需创建 Distributed 表。仅查询每个分片的一个副本。

clusterAllReplicas 函数 — 与 cluster 相同,但查询所有副本。集群中的每个副本都用作单独的分片/连接。

注意

所有可用的集群都列在 system.clusters 表中。

语法

cluster(['cluster_name', db.table, sharding_key])
cluster(['cluster_name', db, table, sharding_key])
clusterAllReplicas(['cluster_name', db.table, sharding_key])
clusterAllReplicas(['cluster_name', db, table, sharding_key])

参数

  • cluster_name – 用于构建地址集和到远程和本地服务器的连接参数的集群名称,如果未指定,则设置为 default
  • db.tabledb, table - 数据库和表的名称。
  • sharding_key - 分片键。可选。如果集群有多个分片,则需要指定。

返回值

来自集群的数据集。

使用宏

cluster_name 可以包含宏 — 花括号中的替换。替换值取自服务器配置文件的 macros 部分。

示例

SELECT * FROM cluster('{cluster}', default.example_table);

用法和建议

使用 clusterclusterAllReplicas 表函数不如创建 Distributed 表高效,因为在这种情况下,服务器连接会为每个请求重新建立。当处理大量查询时,请始终提前创建 Distributed 表,而不要使用 clusterclusterAllReplicas 表函数。

clusterclusterAllReplicas 表函数在以下情况下可能很有用

  • 访问特定集群以进行数据比较、调试和测试。
  • 为了研究目的查询各种 ClickHouse 集群和副本。
  • 手动进行的非频繁分布式请求。

连接设置(如 hostportuserpasswordcompressionsecure)取自 <remote_servers> 配置部分。有关详细信息,请参阅 Distributed 引擎

另请参阅