> ## 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.

# ClickHouse 字典源

> 将 ClickHouse 表配置为字典源。

设置示例：

> [tab] DDL
>
> ```sql theme={null}
> SOURCE(CLICKHOUSE(
>     host 'example01-01-1'
>     port 9000
>     user 'dict_reader'
>     password 'dict_reader_password'
>     db 'default'
>     table 'ids'
>     where 'id=10'
>     secure 1
>     query 'SELECT id, value_1, value_2 FROM default.ids'
> ));
> ```

> [tab] 配置文件
>
> ```xml theme={null}
> <source>
>     <clickhouse>
>         <host>example01-01-1</host>
>         <port>9000</port>
>         <user>dict_reader</user>
>         <password>dict_reader_password</password>
>         <db>default</db>
>         <table>ids</table>
>         <where>id=10</where>
>         <secure>1</secure>
>         <query>SELECT id, value_1, value_2 FROM default.ids</query>
>     </clickhouse>
> </source>
> ```

设置字段：

| 设置 | 说明 |
| - | - |
| `host` | ClickHouse 主机。如果是本地主机，则查询会在没有任何网络通信的情况下处理。为提高容错能力，你可以创建一个 [Distributed](https://clickhouse.com/docs/zh/reference/engines/table-engines/special/distributed.md) 表，并在后续配置中填入该表。 |
| `port` | ClickHouse server 的端口。 |
| `user` | ClickHouse 用户名。 |
| `password` | ClickHouse 用户的密码。 |
| `db` | 数据库名称。 |
| `table` | 表名。 |
| `where` | 筛选条件。可选。 |
| `invalidate_query` | 用于检查字典状态的查询。可选。更多信息，请参见[使用 LIFETIME 刷新字典数据](https://clickhouse.com/docs/zh/reference/statements/create/dictionary/lifetime.md)一节。 |
| `secure` | 连接时使用 SSL。 |
| `query` | 自定义查询。可选。 |

> [!NOTE]
>
> `table` 或 `where` 字段不能与 `query` 字段同时使用。此外，必须声明 `table` 或 `query` 字段之一。

> [!NOTE]
>
> 在 ClickHouse Cloud 中，如果由 `default` 以外的用户创建字典，则必须在源中同时指定 `user` 和 `password`，且 `user` 不能为 `default`，否则 `CREATE DICTIONARY` 会失败并报 `BAD_ARGUMENTS` 错误。请使用对源表拥有 `SELECT` 权限的专用用户。
