Sources of External Dictionaries

    If dictionary is configured using xml-file, the configuration looks like this:

    In case of DDL-query, equal configuration will looks like:

    1. ...
    2. SOURCE(SOURCE_TYPE(param1 val1 ... paramN valN)) -- Source configuration
    3. ...

    The source is configured in the source section.

    For source types , Executable file, , ClickHouse
    optional settings are available:

    1. <source>
    2. <file>
    3. <path>/opt/dictionaries/os.tsv</path>
    4. <format>TabSeparated</format>
    5. </file>
    6. <settings>
    7. <format_csv_allow_single_quotes>0</format_csv_allow_single_quotes>
    8. </settings>
    9. </source>

    or

    1. SOURCE(FILE(path '/opt/dictionaries/os.tsv' format 'TabSeparated'))
    2. SETTINGS(format_csv_allow_single_quotes = 0)

    Types of sources (source_type):

    Example of settings:

    1. <source>
    2. <file>
    3. <path>/opt/dictionaries/os.tsv</path>
    4. <format>TabSeparated</format>
    5. </file>
    6. </source>

    or

    1. SOURCE(FILE(path '/opt/dictionaries/os.tsv' format 'TabSeparated'))

    Setting fields:

    • path – The absolute path to the file.
    • format – The file format. All the formats described in “” are supported.

    Working with executable files depends on how the dictionary is stored in memory. If the dictionary is stored using cache and complex_key_cache, ClickHouse requests the necessary keys by sending a request to the executable file’s STDIN. Otherwise, ClickHouse starts executable file and treats its output as dictionary data.

    Example of settings:

    1. <source>
    2. <executable>
    3. <command>cat /opt/dictionaries/os.tsv</command>
    4. <format>TabSeparated</format>
    5. </executable>
    6. </source>

    or

    1. SOURCE(EXECUTABLE(command 'cat /opt/dictionaries/os.tsv' format 'TabSeparated'))

    Setting fields:

    • command – The absolute path to the executable file, or the file name (if the program directory is written to PATH).
    • format – The file format. All the formats described in “” are supported.

    Working with an HTTP(s) server depends on how the dictionary is stored in memory. If the dictionary is stored using cache and complex_key_cache, ClickHouse requests the necessary keys by sending a request via the POST method.

    Example of settings:

    1. <source>
    2. <http>
    3. <url>http://[::1]/os.tsv</url>
    4. <format>TabSeparated</format>
    5. <credentials>
    6. <user>user</user>
    7. <password>password</password>
    8. </credentials>
    9. <headers>
    10. <header>
    11. <name>API-KEY</name>
    12. <value>key</value>
    13. </header>
    14. </headers>
    15. </http>
    16. </source>

    or

    1. SOURCE(HTTP(
    2. url 'http://[::1]/os.tsv'
    3. format 'TabSeparated'
    4. credentials(user 'user' password 'password')
    5. headers(header(name 'API-KEY' value 'key'))
    6. ))

    In order for ClickHouse to access an HTTPS resource, you must in the server configuration.

    Setting fields:

    • url – The source URL.
    • format – The file format. All the formats described in “Formats” are supported.
    • credentials – Basic HTTP authentication. Optional parameter.
      • user – Username required for the authentication.
      • password – Password required for the authentication.
    • headers – All custom HTTP headers entries used for the HTTP request. Optional parameter.
      • header – Single HTTP header entry.
      • name – Identifiant name used for the header send on the request.
      • value – Value set for a specific identifiant name.

    You can use this method to connect any database that has an ODBC driver.

    Example of settings:

    1. <source>
    2. <odbc>
    3. <db>DatabaseName</db>
    4. <table>ShemaName.TableName</table>
    5. <connection_string>DSN=some_parameters</connection_string>
    6. <invalidate_query>SQL_QUERY</invalidate_query>
    7. </odbc>
    8. </source>

    or

    Setting fields:

    • db – Name of the database. Omit it if the database name is set in the <connection_string> parameters.
    • table – Name of the table and schema if exists.
    • connection_string – Connection string.
    • invalidate_query – Query for checking the dictionary status. Optional parameter. Read more in the section .

    If you have a problems with encodings when using Oracle, see the corresponding F.A.Q. item.

    Attention

    When connecting to the database through the ODBC driver connection parameter Servername can be substituted. In this case values of USERNAME and PASSWORD from odbc.ini are sent to the remote server and can be compromised.

    Example of insecure use

    Let’s configure unixODBC for PostgreSQL. Content of /etc/odbc.ini:

    1. [gregtest]
    2. Driver = /usr/lib/psqlodbca.so
    3. Servername = localhost
    4. PORT = 5432
    5. DATABASE = test_db
    6. #OPTION = 3
    7. USERNAME = test
    8. PASSWORD = test

    If you then make a query such as

    1. SELECT * FROM odbc('DSN=gregtest;Servername=some-server.com', 'test_db');

    ODBC driver will send values of USERNAME and PASSWORD from odbc.ini to some-server.com.

    Example of Connecting Postgresql

    Ubuntu OS.

    Installing unixODBC and the ODBC driver for PostgreSQL:

    1. $ sudo apt-get install -y unixodbc odbcinst odbc-postgresql

    Configuring /etc/odbc.ini (or ~/.odbc.ini if you signed in under a user that runs ClickHouse):

    1. [DEFAULT]
    2. Driver = myconnection
    3. [myconnection]
    4. Description = PostgreSQL connection to my_db
    5. Driver = PostgreSQL Unicode
    6. Database = my_db
    7. Servername = 127.0.0.1
    8. Password = password
    9. Port = 5432
    10. Protocol = 9.3
    11. ReadOnly = No
    12. RowVersioning = No
    13. ShowSystemTables = No
    14. ConnSettings =

    The dictionary configuration in ClickHouse:

    1. <yandex>
    2. <dictionary>
    3. <name>table_name</name>
    4. <source>
    5. <odbc>
    6. <!-- You can specify the following parameters in connection_string: -->
    7. <connection_string>DSN=myconnection</connection_string>
    8. <table>postgresql_table</table>
    9. </odbc>
    10. </source>
    11. <lifetime>
    12. <min>300</min>
    13. <max>360</max>
    14. </lifetime>
    15. <layout>
    16. <hashed/>
    17. </layout>
    18. <structure>
    19. <id>
    20. <name>id</name>
    21. </id>
    22. <attribute>
    23. <name>some_column</name>
    24. <type>UInt64</type>
    25. <null_value>0</null_value>
    26. </attribute>
    27. </structure>
    28. </dictionary>
    29. </yandex>

    or

    1. CREATE DICTIONARY table_name (
    2. id UInt64,
    3. some_column UInt64 DEFAULT 0
    4. )
    5. PRIMARY KEY id
    6. SOURCE(ODBC(connection_string 'DSN=myconnection' table 'postgresql_table'))
    7. LAYOUT(HASHED())
    8. LIFETIME(MIN 300 MAX 360)

    You may need to edit odbc.ini to specify the full path to the library with the driver DRIVER=/usr/local/lib/psqlodbcw.so.

    Ubuntu OS.

    Installing the ODBC driver for connecting to MS SQL:

    1. $ sudo apt-get install tdsodbc freetds-bin sqsh

    Configuring the driver:

    1. $ cat /etc/freetds/freetds.conf
    2. ...
    3. [MSSQL]
    4. host = 192.168.56.101
    5. port = 1433
    6. tds version = 7.0
    7. client charset = UTF-8
    8. # test TDS connection
    9. $ sqsh -S MSSQL -D database -U user -P password
    10. $ cat /etc/odbcinst.ini
    11. [FreeTDS]
    12. Description = FreeTDS
    13. Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
    14. Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
    15. FileUsage = 1
    16. UsageCount = 5
    17. $ cat /etc/odbc.ini
    18. # $ cat ~/.odbc.ini # if you signed in under a user that runs ClickHouse
    19. [MSSQL]
    20. Description = FreeTDS
    21. Driver = FreeTDS
    22. Servername = MSSQL
    23. Database = test
    24. UID = test
    25. PWD = test
    26. Port = 1433
    27. # (optional) test ODBC connection (to use isql-tool install the [unixodbc](https://packages.debian.org/sid/unixodbc)-package)
    28. $ isql -v MSSQL "user" "password"

    Remarks:
    - to determine the earliest TDS version that is supported by a particular SQL Server version, refer to the product documentation or look at MS-TDS Product Behavior

    Configuring the dictionary in ClickHouse:

    1. <yandex>
    2. <dictionary>
    3. <name>test</name>
    4. <source>
    5. <odbc>
    6. <table>dict</table>
    7. <connection_string>DSN=MSSQL;UID=test;PWD=test</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>k</name>
    20. </id>
    21. <attribute>
    22. <name>s</name>
    23. <type>String</type>
    24. <null_value></null_value>
    25. </attribute>
    26. </structure>
    27. </dictionary>
    28. </yandex>

    or

    1. CREATE DICTIONARY test (
    2. k UInt64,
    3. )
    4. PRIMARY KEY k
    5. SOURCE(ODBC(table 'dict' connection_string 'DSN=MSSQL;UID=test;PWD=test'))
    6. LAYOUT(FLAT())
    7. LIFETIME(MIN 300 MAX 360)

    Mysql

    Example of settings:

    or

    1. SOURCE(MYSQL(
    2. port 3306
    3. user 'clickhouse'
    4. password 'qwerty'
    5. replica(host 'example01-1' priority 1)
    6. replica(host 'example01-2' priority 1)
    7. db 'db_name'
    8. table 'table_name'
    9. where 'id=10'
    10. invalidate_query 'SQL_QUERY'
    11. ))

    Setting fields:

    • user – Name of the MySQL user. You can specify it for all replicas, or for each one individually (inside <replica>).

    • password – Password of the MySQL user. You can specify it for all replicas, or for each one individually (inside <replica>).

    • – Section of replica configurations. There can be multiple sections.

      1. - `replica/host` The MySQL host.
      2. - `replica/priority` The replica priority. When attempting to connect, ClickHouse traverses the replicas in order of priority. The lower the number, the higher the priority.
    • db – Name of the database.

    • table – Name of the table.

    • where – The selection criteria. The syntax for conditions is the same as for WHERE clause in MySQL, for example, id > 10 AND id < 20. Optional parameter.

    • invalidate_query – Query for checking the dictionary status. Optional parameter. Read more in the section Updating dictionaries.

    MySQL can be connected on a local host via sockets. To do this, set host and socket.

    Example of settings:

    1. <source>
    2. <mysql>
    3. <host>localhost</host>
    4. <socket>/path/to/socket/file.sock</socket>
    5. <user>clickhouse</user>
    6. <password>qwerty</password>
    7. <db>db_name</db>
    8. <table>table_name</table>
    9. <where>id=10</where>
    10. <invalidate_query>SQL_QUERY</invalidate_query>
    11. </mysql>
    12. </source>

    or

    1. SOURCE(MYSQL(
    2. host 'localhost'
    3. socket '/path/to/socket/file.sock'
    4. user 'clickhouse'
    5. password 'qwerty'
    6. db 'db_name'
    7. table 'table_name'
    8. where 'id=10'
    9. invalidate_query 'SQL_QUERY'
    10. ))

    Example of settings:

    1. <source>
    2. <clickhouse>
    3. <host>example01-01-1</host>
    4. <port>9000</port>
    5. <user>default</user>
    6. <password></password>
    7. <db>default</db>
    8. <table>ids</table>
    9. <where>id=10</where>
    10. </clickhouse>
    11. </source>

    or

    1. SOURCE(CLICKHOUSE(
    2. host 'example01-01-1'
    3. port 9000
    4. user 'default'
    5. password ''
    6. db 'default'
    7. table 'ids'
    8. where 'id=10'
    9. ))

    Setting fields:

    • host – The ClickHouse host. If it is a local host, the query is processed without any network activity. To improve fault tolerance, you can create a table and enter it in subsequent configurations.
    • port – The port on the ClickHouse server.
    • user – Name of the ClickHouse user.
    • password – Password of the ClickHouse user.
    • db – Name of the database.
    • table – Name of the table.
    • where – The selection criteria. May be omitted.
    • invalidate_query – Query for checking the dictionary status. Optional parameter. Read more in the section Updating dictionaries.

    Mongodb

    Example of settings:

    1. <source>
    2. <mongodb>
    3. <host>localhost</host>
    4. <port>27017</port>
    5. <user></user>
    6. <password></password>
    7. <db>test</db>
    8. <collection>dictionary_source</collection>
    9. </mongodb>
    10. </source>

    or

    1. SOURCE(MONGO(
    2. host 'localhost'
    3. port 27017
    4. user ''
    5. password ''
    6. db 'test'
    7. collection 'dictionary_source'
    8. ))

    Setting fields:

    • host – The MongoDB host.
    • port – The port on the MongoDB server.
    • user – Name of the MongoDB user.
    • password – Password of the MongoDB user.
    • db – Name of the database.
    • collection – Name of the collection.

    Example of settings:

    1. <source>
    2. <redis>
    3. <host>localhost</host>
    4. <port>6379</port>
    5. <storage_type>simple</storage_type>
    6. <db_index>0</db_index>
    7. </redis>
    8. </source>

    or

    1. SOURCE(REDIS(
    2. host 'localhost'
    3. port 6379
    4. storage_type 'simple'
    5. db_index 0
    6. ))

    Setting fields:

    • host – The Redis host.
    • port – The port on the Redis server.
    • db_index – The specific numeric index of Redis logical database. May be omitted, default value is 0.

    Cassandra

    Example of settings:

    Setting fields:
    - host – The Cassandra host or comma-separated list of hosts.
    - port – The port on the Cassandra servers. If not specified, default port is used.
    - user – Name of the Cassandra user.
    - password – Password of the Cassandra user.
    - keyspace – Name of the keyspace (database).
    - column_family – Name of the column family (table).
    - allow_filering – Flag to allow or not potentially expensive conditions on clustering key columns. Default value is 1.
    - partition_key_prefix – Number of partition key columns in primary key of the Cassandra table.
    Required for compose key dictionaries. Order of key columns in the dictionary definition must be the same as in Cassandra.
    Default value is 1 (the first key column is a partition key and other key columns are clustering key).
    - consistency – Consistency level. Possible values: One, Two, Three,
    All, EachQuorum, Quorum, LocalQuorum, LocalOne, Serial, LocalSerial. Default is One.
    - where – Optional selection criteria.
    - max_threads – The maximum number of threads to use for loading data from multiple partitions in compose key dictionaries.