Dictionary Table Engine

The Dictionary engine displays the dictionary data as a ClickHouse table.

Example

As an example, consider a dictionary of products with the following configuration:

  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>

Query the dictionary data:

  1. SELECT
  2. name,
  3. type,
  4. key,
  5. attribute.names,
  6. attribute.types,
  7. bytes_allocated,
  8. element_count,
  9. source
  10. FROM system.dictionaries
  11. 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. └──────────┴──────┴────────┴─────────────────┴─────────────────┴─────────────────┴───────────────┴─────────────────┘

You can use the dictGet* function to get the dictionary data in this format.

This view isn’t helpful when you need to get raw data, or when performing a JOIN operation. For these cases, you can use the Dictionary engine, which displays the dictionary data in a table.

Syntax:

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

Usage example:

  1. create table products (product_id UInt64, title String) Engine = Dictionary(products);
  1. Ok

Take a look at what’s in the table.

  1. select * from products limit 1;
  1. ┌────product_id─┬─title───────────┐
  2. 152689 Some item
  3. └───────────────┴─────────────────┘

Original article