> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 複数のリモートクラスターに対してクエリを実行できるテーブルを作成する方法

> 複数のリモートクラスターに対してクエリを実行できるテーブルを作成する方法

{frontMatter.description}

***

<div id="question">
  ## 質問
</div>

他のクラスターやインスタンスに対してクエリを実行できるテーブルを作成するにはどうすればよいですか？

<div id="answer">
  ## 回答
</div>

以下は機能をテストするための簡単な例です。

この例ではClickHouse Cloudを使用していますが、セルフホストのクラスターでも同様に動作します。
ターゲットには、対象ノードまたはロードバランサーのURL・ホスト・DNSを指定してください。

クラスターAにて：

```
./clickhouse client --host clusterA.us-west-2.aws.clickhouse.cloud --secure --password 'Password123!'
```

データベースを作成します：

```
create database db1;
```

テーブルを作成します：

```
CREATE TABLE db1.table1_remote1
(
    `id` UInt32,
    `timestamp_column` DateTime,
    `string_column` String
)
ENGINE = MergeTree()
ORDER BY id;
```

サンプルの行をいくつか挿入します。

```
insert into db1.table1_remote1
values
(1, '2023-09-29 00:01:00', 'a'),
(2, '2023-09-29 00:02:00', 'b'),
(3, '2023-09-29 00:03:00', 'c');
```

クラスターBでは：

```
./clickhouse client --host clusterB.us-east-2.aws.clickhouse.cloud --secure --password 'Password123!'
```

データベースを作成します。

```
create database db1;
```

テーブルを作成します：

```
CREATE TABLE db1.table1_remote2
(
    `id` UInt32,
    `timestamp_column` DateTime,
    `string_column` String
)
ENGINE = MergeTree()
ORDER BY id;
```

サンプル行を挿入します：

```
insert into db1.table1_remote1
values
(4, '2023-09-29 00:04:00', 'x'),
(5, '2023-09-29 00:05:00', 'y'),
(6, '2023-09-29 00:06:00', 'z');
```

クラスターCでは:
\*このクラスターは他の2つのクラスターからデータを収集するために使用されますが、ソースとしても使用できます。

```
./clickhouse client --host clusterC.us-west-2.aws.clickhouse.cloud --secure --password 'Password123!'
```

データベースを作成します：

```
create database db1;
```

remoteSecure() を使用してリモートテーブルを作成し、他のクラスターに接続します。
リモートクラスター A のテーブル定義：

```
CREATE TABLE db1.table1_remote1_main
(
    `id` UInt32,
    `timestamp_column` DateTime,
    `string_column` String
) AS remoteSecure('clusterA.us-west-2.aws.clickhouse.cloud:9440', 'db1.table1_remote1', 'default', 'Password123!');
```

リモートクラスター B のテーブルの定義：

```
CREATE TABLE db1.table1_remote2_main
(
    `id` UInt32,
    `timestamp_column` DateTime,
    `string_column` String
) AS remoteSecure('clusterB.us-east-2.aws.clickhouse.cloud:9440', 'db1.table1_remote2', 'default', 'Password123!')
```

結果を集約するために使用するmergeテーブルを作成します：

```
create table db1.table1_merge_remote
(
  id UInt32,
  timestamp_column DateTime,
  string_column String
)
engine = Merge('db1', 'table.\_main');
```

結果を確認します：

```
clickhouse-cloud :) select * from db1.table1_merge_remote;

SELECT *
FROM db1.table1_merge_remote

Query id: 46b6e741-bbd1-47ed-b40e-69ddb6e0c364

┌─id─┬────timestamp_column─┬─string_column─┐
│  1 │ 2023-09-29 00:01:00 │ a             │
│  2 │ 2023-09-29 00:02:00 │ b             │
│  3 │ 2023-09-29 00:03:00 │ c             │
└────┴─────────────────────┴───────────────┘
┌─id─┬────timestamp_column─┬─string_column─┐
│  4 │ 2023-09-29 00:04:00 │ x             │
│  5 │ 2023-09-29 00:05:00 │ y             │
│  6 │ 2023-09-29 00:06:00 │ z             │
└────┴─────────────────────┴───────────────┘

6 rows in set. Elapsed: 0.275 sec.
```

詳しくは：

* [Remote table function](/docs/ja/reference/functions/table-functions/remote)
* [Merge テーブルエンジン](/docs/ja/reference/engines/table-engines/special/merge)
