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

remote, remoteSecure

表函数 remote 允许即时访问远程服务器,即无需创建 Distributed 表。表函数 remoteSecureremote 相同,但通过安全连接。

这两个函数都可以在 SELECTINSERT 查询中使用。

语法

remote(addresses_expr, [db, table, user [, password], sharding_key])
remote(addresses_expr, [db.table, user [, password], sharding_key])
remote(named_collection[, option=value [,..]])
remoteSecure(addresses_expr, [db, table, user [, password], sharding_key])
remoteSecure(addresses_expr, [db.table, user [, password], sharding_key])
remoteSecure(named_collection[, option=value [,..]])

参数

  • addresses_expr — 远程服务器地址或生成多个远程服务器地址的表达式。格式:hosthost:port

    host 可以指定为服务器名称,或 IPv4 或 IPv6 地址。IPv6 地址必须用方括号括起来。

    port 是远程服务器上的 TCP 端口。如果省略端口,则表函数 remote 使用服务器配置文件中的 tcp_port(默认为 9000),表函数 remoteSecure 使用 tcp_port_secure(默认为 9440)。

    对于 IPv6 地址,端口是必需的。

    如果仅指定参数 addresses_expr,则 dbtable 默认将使用 system.one

    类型:String

  • db — 数据库名称。类型:String

  • table — 表名称。类型:String

  • user — 用户名。如果未指定,则使用 default。类型:String

  • password — 用户密码。如果未指定,则使用空密码。类型:String

  • sharding_key — 分片键,用于支持跨节点分发数据。例如:insert into remote('127.0.0.1:9000,127.0.0.2', db, table, 'default', rand())。类型:UInt32

参数也可以使用 命名集合 传递。

返回值

位于远程服务器上的表。

用法

由于表函数 remoteremoteSecure 为每个请求重新建立连接,因此建议改用 Distributed 表。此外,如果设置了主机名,则会解析名称,并且在使用各种副本时不会计算错误。在处理大量查询时,始终提前创建 Distributed 表,而不要使用 remote 表函数。

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

  • 从一个系统到另一个系统的一次性数据迁移
  • 访问特定服务器以进行数据比较、调试和测试,即即席连接。
  • 在各种 ClickHouse 集群之间进行查询以进行研究。
  • 手动进行的非频繁分布式请求。
  • 每次重新定义服务器集的分布式请求。

地址

example01-01-1
example01-01-1:9440
example01-01-1:9000
localhost
127.0.0.1
[::]:9440
[::]:9000
[2a02:6b8:0:1111::11]:9000

多个地址可以用逗号分隔。在这种情况下,ClickHouse 将使用分布式处理并将查询发送到所有指定的地址(如具有不同数据的分片)。示例

example01-01-1,example01-02-1

示例

从远程服务器选择数据:

SELECT * FROM remote('127.0.0.1', db.remote_engine_table) LIMIT 3;

或使用 命名集合

CREATE NAMED COLLECTION creds AS
host = '127.0.0.1',
database = 'db';
SELECT * FROM remote(creds, table='remote_engine_table') LIMIT 3;

将数据插入到远程服务器上的表中:

CREATE TABLE remote_table (name String, value UInt32) ENGINE=Memory;
INSERT INTO FUNCTION remote('127.0.0.1', currentDatabase(), 'remote_table') VALUES ('test', 42);
SELECT * FROM remote_table;

将表从一个系统迁移到另一个系统:

此示例使用来自示例数据集中的一个表。数据库是 imdb,表是 actors

在源 ClickHouse 系统上(当前托管数据的系统)

  • 验证源数据库和表名 (imdb.actors)

    show databases
    show tables in imdb
  • 从源获取 CREATE TABLE 语句

    select create_table_query
    from system.tables
    where database = 'imdb' and table = 'actors'

    响应

    CREATE TABLE imdb.actors (`id` UInt32,
    `first_name` String,
    `last_name` String,
    `gender` FixedString(1))
    ENGINE = MergeTree
    ORDER BY (id, first_name, last_name, gender);

在目标 ClickHouse 系统上:

  • 创建目标数据库

    CREATE DATABASE imdb
  • 使用来自源的 CREATE TABLE 语句,创建目标

    CREATE TABLE imdb.actors (`id` UInt32,
    `first_name` String,
    `last_name` String,
    `gender` FixedString(1))
    ENGINE = MergeTree
    ORDER BY (id, first_name, last_name, gender);

返回到源部署:

插入到远程系统上创建的新数据库和表中。您将需要主机、端口、用户名、密码、目标数据库和目标表。

INSERT INTO FUNCTION
remoteSecure('remote.clickhouse.cloud:9440', 'imdb.actors', 'USER', 'PASSWORD')
SELECT * from imdb.actors

Globbing

花括号 { } 中的模式用于生成一组分片并指定副本。如果有多个花括号对,则生成相应集合的直接乘积。

支持以下模式类型。

  • {a,b,c} - 表示字符串 abc 中的任何一个。该模式在第一个分片地址中替换为 a,在第二个分片地址中替换为 b,依此类推。例如,example0{1,2}-1 生成地址 example01-1example02-1
  • {N..M} - 数字范围。此模式生成分片地址,索引从 N 递增到(包括)M。例如,example0{1..2}-1 生成 example01-1example02-1
  • {0n..0m} - 带有前导零的数字范围。此模式保留索引中的前导零。例如,example{01..03}-1 生成 example01-1example02-1example03-1
  • {a|b} - 由 | 分隔的任意数量的变体。该模式指定副本。例如,example01-{1|2} 生成副本 example01-1example01-2

查询将发送到第一个健康的副本。但是,对于 remote,副本按照当前在 load_balancing 设置中设置的顺序进行迭代。生成的地址数受 table_function_remote_max_addresses 设置的限制。