跳至主要内容
跳至主要内容

remote, remoteSecure 表函数

表函数 remote 允许实时访问远程服务器,即无需创建 分布式 表。表函数 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

类型:字符串
db数据库名称。类型:字符串
table表名称。类型:字符串
用户用户名。如果未指定,则使用 default。类型:字符串
password用户密码。如果未指定,则使用空密码。类型:字符串
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 设置限制。

    © . This site is unofficial and not affiliated with ClickHouse, Inc.