Dictionary

Dictionary 引擎将字典数据展示为一个ClickHouse的表。

例如,考虑使用一个具有以下配置的 products 字典:

  1. <dictionaries>
  2. <dictionary>
  3. <name>products</name>
  4. <source>
  5. <odbc>
  6. <table>products</table>
  7. <connection_string>DSN=some-db-server</connection_string>
  8. </odbc>
  9. </source>
  10. <lifetime>
  11. <min>300</min>
  12. <max>360</max>
  13. </lifetime>
  14. <layout>
  15. <flat/>
  16. </layout>
  17. <structure>
  18. <id>
  19. <name>product_id</name>
  20. </id>
  21. <attribute>
  22. <name>title</name>
  23. <type>String</type>
  24. <null_value></null_value>
  25. </attribute>
  26. </structure>
  27. </dictionary>
  28. </dictionaries>

查询字典中的数据:

  1. select name, type, key, attribute.names, attribute.types, bytes_allocated, element_count,source from system.dictionaries where name = 'products';
  2. SELECT
  3. name,
  4. type,
  5. key,
  6. attribute.names,
  7. attribute.types,
  8. bytes_allocated,
  9. element_count,
  10. source
  11. FROM system.dictionaries
  12. WHERE name = 'products'
  1. ┌─name─────┬─type─┬─key────┬─attribute.names─┬─attribute.types─┬─bytes_allocated─┬─element_count─┬─source──────────┐
  2. products Flat UInt64 ['title'] ['String'] 23065376 175032 ODBC: .products
  3. └──────────┴──────┴────────┴─────────────────┴─────────────────┴─────────────────┴───────────────┴─────────────────┘

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

当你需要获取原始数据,或者是想要使用 JOIN 操作的时候,这种视图并没有什么帮助。对于这些情况,你可以使用 Dictionary 引擎,它可以将字典数据展示在表中。

语法:

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

示例:

  1. create table products (product_id UInt64, title String) Engine = Dictionary(products);
  2. CREATE TABLE products
  3. (
  4. product_id UInt64,
  5. title String,
  6. )
  7. ENGINE = Dictionary(products)
  1. Ok.
  2. 0 rows in set. Elapsed: 0.004 sec.

看一看表中的内容。

  1. select * from products limit 1;
  2. SELECT *
  3. FROM products
  4. LIMIT 1
  1. ┌────product_id─┬─title───────────┐
  2. 152689 Some item
  3. └───────────────┴─────────────────┘
  4. 1 rows in set. Elapsed: 0.006 sec.

来源文章