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

Dictionary 表引擎

Dictionary 引擎将 dictionary 数据显示为 ClickHouse 表。

示例

例如,假设有一个名为 products 的字典,其配置如下:

<dictionaries>
    <dictionary>
        <name>products</name>
        <source>
            <odbc>
                <table>products</table>
                <connection_string>DSN=some-db-server</connection_string>
            </odbc>
        </source>
        <lifetime>
            <min>300</min>
            <max>360</max>
        </lifetime>
        <layout>
            <flat/>
        </layout>
        <structure>
            <id>
                <name>product_id</name>
            </id>
            <attribute>
                <name>title</name>
                <type>String</type>
                <null_value></null_value>
            </attribute>
        </structure>
    </dictionary>
</dictionaries>

查询字典中的数据:

SELECT
    name,
    type,
    key,
    attribute.names,
    attribute.types,
    bytes_allocated,
    element_count,
    source
FROM system.dictionaries
WHERE name = 'products'
┌─name─────┬─type─┬─key────┬─attribute.names─┬─attribute.types─┬─bytes_allocated─┬─element_count─┬─source──────────┐
│ products │ Flat │ UInt64 │ ['title']       │ ['String']      │        23065376 │        175032 │ ODBC: .products │
└──────────┴──────┴────────┴─────────────────┴─────────────────┴─────────────────┴───────────────┴─────────────────┘

你可以使用 dictGet* 函数以这种格式获取字典数据。

当你需要获取原始数据或执行 JOIN 操作时,此视图并不太有用。对于这些场景,你可以使用 Dictionary 引擎,它会以表格形式展示字典数据。

语法:

CREATE TABLE %table_name% (%fields%) engine = Dictionary(%dictionary_name%)`

用法示例:

CREATE TABLE products (product_id UInt64, title String) ENGINE = Dictionary(products);

好的

来看一下表里的内容。

SELECT * FROM products LIMIT 1;
┌────product_id─┬─title───────────┐
│        152689 │ 某个商品        │
└───────────────┴─────────────────┘

另请参阅