メインコンテンツへスキップ
メインコンテンツへスキップ

Dictionary テーブルエンジン

Dictionary エンジンは、Dictionary のデータを ClickHouse のテーブルとして利用できるようにします。

例として、次のように構成された products の Dictionary を考えます。

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

Dictionary データをクエリします:

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* 関数を使用して、この形式で Dictionary データを取得できます。

このビューは、生データを取得したい場合や JOIN 演算を行う場合にはあまり役に立ちません。こうしたケースでは、Dictionary データをテーブルとして表示する 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 │ Some item       │
└───────────────┴─────────────────┘

関連項目