ADB ClickHouse Connector configuration

To send data from ADB to ClickHouse via ADB ClickHouse Connector, you should first create the following objects on the ADB cluster side:

  1. Server — encapsulates connection information that a foreign data wrapper uses to access an external data source.

  2. User mapping — provides user authentication to an external data source.

  3. Foreign table — a table in ADB that defines the remote data structure. A foreign table has no storage in ADB, but can be used in queries just like a normal table.

To see how to use the listed objects to send data from ADB to ADQM, refer to ADB ClickHouse Connector usage examples.

IMPORTANT
  • For each ClickHouse cluster to which you plan to send queries from ADB, it is sufficient to create one server and one user mapping. However, there may be several foreign tables if you need to send queries against various ClickHouse tables.

  • All options that are listed in the tables below (Server options, User mapping options, Foreign table options) should be defined in the text format (i.e. in single quotes '). The Type column in the tables shows what data type will be used when validating input values.

Server

To create a server, use the CREATE SERVER command. The basic command syntax is listed below:

CREATE SERVER <server_name> [ TYPE '<server_type>' ] [ VERSION '<server_version>' ]
    FOREIGN DATA WRAPPER <fdw_name>
    [ OPTIONS ( [ <option> '<value>' [, ... ]] ) ]

where:

  • <server_name> — a server name in ADB. Should be unique within the current ADB database.

  • <server_type> — an optional server type.

  • <server_version> — an optional server version.

  • <fdw_name> — a foreign data wrapper name. You should use the tkh_fdw foreign data wrapper, which is automatically created after the connector installation (see step 8 in ADB ClickHouse Connector installation).

  • <option> — server options that define the connection details. The options that are available for ADB ClickHouse Connector are listed in the Server options table. Note that these options can be defined both at the server level and at the foreign table level. Options marked as required should be specified at least in one definition (server or table). Table-level options take precedence.

  • <value> — option values.

NOTE
  • To create a server, you need the USAGE privilege on the tkh_fdw foreign data wrapper. The user who creates the server becomes its owner.

  • The full description of the CREATE SERVER command syntax is available in the Greengage DB documentation.

  • To edit a server definition, use the ALTER SERVER command. To delete a server, use DROP SERVER.

Server options
Name Type Description Default Required

database

TEXT

A name of the ClickHouse database where the target table is located.

Should match the following regular expression: ^[a-zA-Z_][0-9a-zA-Z_]*$

 — 

Yes

hosts

TEXT

A list of ClickHouse hosts where the target table is located. A ClickHouse cluster does not have a single entry point (master node), so the requests can be sent to any host in the cluster. To reduce the load, data batches are sent to hosts from the connector according to the random or the round-robin distribution strategy (see distribution_type below).

Validation rules for the hosts option are listed below

 — 

Yes

lines_batch_size

INT

The maximum number of rows in one batch sent to ClickHouse by the connector. Used for INSERT operations only.

0 disables row-based batching. If bytes_batch_size_mb is also 0, data is not split into batches, and the entire data stream is sent as a single INSERT per segment.

Ignored if bytes_batch_size_mb is greater than 0

100000

No

bytes_batch_size_mb

INT

The maximum size of one batch sent to ClickHouse by the connector (in MB). Used for INSERT operations only.

0 disables size-based batching. If lines_batch_size is also 0, data is not split into batches, and the entire data stream is sent as a single INSERT per segment.

Takes precedence over lines_batch_size

0

No

send_compressed

BOOL

The flag that indicates whether to use compression on the connector side before sending data to ClickHouse.

Possible values:

  • true — use data compression.

  • false — do not use data compression.

false

No

send_delay

INT

A delay between connector requests to ClickHouse (in milliseconds).

Positive integers are allowed

300

No

insert_distributed_sync

BOOL

Enables or disables synchronous data load to distributed tables.

Possible values:

  • true — data is inserted synchronously. The INSERT query is considered successful after all data is added to all shards (at least to one replica per chard if internal_replication = true in ClickHouse).

  • false — data is inserted asynchronously.

true

No

distribution_type

TEXT

Defines the strategy of load distribution between ClickHouse hosts (see hosts above) when performing data load operations from the connector side.

Possible values:

  • random — hosts are selected randomly.

  • round-robin — hosts are selected based on the round-robin algorithm. This option value is preferable in terms of the uniform load distribution.

random

No

use_staging

BOOL

The flag that indicates whether to create staging tables in ClickHouse before inserting data into the target table. Used to emulate transactions that are not present in ClickHouse. If a failure occurs, the ADB transaction containing the INSERT command is rolled back, and the target table remains unchanged. However, if the transaction is rolled back after a partition has already been attached in ClickHouse, the connector cannot remove the added rows. Atomicity is guaranteed when inserting into a single partition of a local table.

Staging tables are supported only for the following ClickHouse engine families: MergeTree (including Replicated* engines) and Distributed. For other engines, an error will be returned, since not all engine types support partitioning in ClickHouse.

Possible values:

  • true — use staging tables.

  • false — do not use staging tables. In this mode, there are certain risks, as the consistency of data insertions can be disturbed. However, the mode is suitable for single insertions into empty target tables.

 — 

Yes

staging_table_name_format

TEXT

Defines the staging table name format (see use_staging above). Example: '$_tmp_$'. The first placeholder ($) is replaced with the name of the target table in ClickHouse (see resource in Foreign table options). The second placeholder is replaced with the integer identifier that is automatically generated on the master node of ADB

 — 

Yes if use_staging = true

clickhouse_properties

TEXT

A list of ClickHouse custom options in the <custom_option>=<value>;[…​] format, where <custom_option> is an option name and <value> is an option value.

For SELECT operations, only SSL options apply.

For INSERT operations, see the ClickHouse Java client configuration. If an option name matches a ClickHouse setting (see system.settings), it is passed to ClickHouse as a query-level setting

 — 

No

fdw_startup_cost

DOUBLE

The cost estimate for obtaining the first tuple from the data source.

Positive double values are allowed.

Can be used only in SELECT queries to ADQM/ClickHouse

1000.0

No

fdw_tuple_cost

DOUBLE

The cost estimate for obtaining one tuple from the data source.

Positive double values are allowed.

Can be used only in SELECT queries to ADQM/ClickHouse

0.01

No

fdw_tuples_count

DOUBLE

The estimate for the number of all rows in the data source. Does not take into account the query selectivity, LIMIT clauses, etc.

Positive double values >= 1 are allowed.

Can be used only in SELECT queries to ADQM/ClickHouse

100000.0

No

client_version

TEXT

JDBC driver for the ClickHouse client to be used for INSERT operations:

v2

No

NOTE

The legacy clickhouse4j driver (client_version 'blynk') does not support compression algorithms used by the latest versions of ClickHouse Server. If you want to use this driver with the latest versions of ClickHouse Server, specify clickhouse_properties 'compress=false' at the server or table level.

Validation rules for the hosts option
  • The list of ClickHouse hosts should be specified in the following format: <hostname>:<port> [, …​], where <hostname> is a name of each host and <port> is an HTTP port number to connect to the corresponding host.

  • The maximum length of the <hostname> value is 255 characters.

  • You can use in the <hostname> value digits 0-9, ASCII characters a-zA-Z, hyphens -, underscores _, and dots ..

  • The presence of the <port> substring for each <hostname> is required. Only digits 0-9 are allowed in the <port> value. The maximum length of the <port> value is 5 characters.

  • The maximum length of the hosts value: (255 (the <hostname> limit) + 1 (:) + 5 (the <port> limit) + 1 (,)) x 127 (number of hosts) = 33274 characters.

User mapping

To create a user mapping, use the CREATE USER MAPPING command. The basic command syntax is listed below:

CREATE USER MAPPING FOR { <username> | USER | CURRENT_USER | PUBLIC }
    SERVER <server_name>
    [ OPTIONS ( <option> '<value>' [, ... ] ) ]

where:

  • <username> — a user name in ADB. The following constants are allowed:

    • USER or CURRENT_USER — the current user who established the ADB connection.

    • PUBLIC — all present and future ADB users.

  • <server_name> — a server name.

  • <option> — user mapping options, which typically include a user name and a password to connect to an external data source. The options that are available for ADB ClickHouse Connector are listed in the User mapping options table.

  • <value> — option values.

NOTE
User mapping options
Name Type Description Default Required

clickhouse_username

TEXT

A user name in ClickHouse

 — 

Yes

clickhouse_password

TEXT

A user password in ClickHouse. If there is no password (for example, in the test environment), an empty string is specified

 — 

Yes

Foreign table

To create a foreign table, use the CREATE FOREIGN TABLE command. The basic command syntax is listed below:

CREATE FOREIGN TABLE [ IF NOT EXISTS ] <table_name> ( [
    <column_name> <data_type> [ COLLATE <collation> ] [ <column_constraint> [ ... ] ]
      [, ... ]
] )
    SERVER <server_name>
  [ OPTIONS ( <option> '<value>' [, ... ] ) ]

where:

  • <table_name> — a foreign table name in ADB.

  • <column_name> — a column name.

  • <data_type> — a column data type.

  • <collation> — a column collation.

  • <column_constraint> — a constraint defined at the column level. The name <constraint_name> is given optionally. The syntax:

    [ CONSTRAINT <constraint_name> ]
    { NOT NULL |
      NULL |
      DEFAULT <default_expr> }

    Possible constraints:

    • NOT NULL — specifies that the column is not allowed to contain null values.

    • NULL — specifies that the column is allowed to contain null values. It is the default behavior (unless NOT NULL is specified).

    • DEFAULT — sets the default column value equal to <default_expr>.

  • <server_name> — a server name.

  • <option> — foreign table options. For ADB ClickHouse Connector, all options defined at the server level can be overwritten at the foreign table level (partially or fully). When you specify an option for a server and a foreign table at the same time, the table level takes precedence. Additionally, at the foreign table level, two more options are available (see Foreign table options below).

  • <value> — option values.

NOTE
  • To create a foreign table, you need the USAGE privilege on the corresponding server and on all column data types that are used in the table definition. The user who creates the foreign table becomes its owner.

  • The full description of the CREATE FOREIGN TABLE command syntax is available in the Greengage DB documentation.

  • To edit a foreign table definition, use the ALTER FOREIGN TABLE command. To drop a foreign table, use DROP FOREIGN TABLE.

Foreign table options
Name Type Description Default Required

resource

TEXT

A table name in ClickHouse.

If you need your own strategy for distributing data across hosts (sharding), you should insert data into the distributed ClickHouse table with the sharding key configured. To do this, specify the distributed table name in the resource option. When writing data to a distributed table, remember that physical (local) tables should be present on all hosts in the ClickHouse cluster, otherwise the connector will return an error.

The option value should match the following regular expression: ^[a-zA-Z_][0-9a-zA-Z_]*$

 — 

Yes

cluster

TEXT

A cluster name in ClickHouse. For more information, see Typical cluster in the ADQM documentation.

If you specify the cluster option, the list of ClickHouse hosts should be defined in the hosts option at the foreign table level and consist of one element <hostname>:<port>, even if it has been already declared at the server level. Otherwise, there may be an ambiguous situation when there is a list of hosts at the server level, but the work is actually done with hosts received from the ClickHouse cluster metadata.

The cluster option provides the convenience of users. With that option, it is enough to specify a cluster name and one entry point, the full list of hosts will be received from ClickHouse. The load between hosts will be still distributed according to the selected distribution_type value (see Server options).

The maximum cluster length is 128 characters

 — 

No

SSL options

NOTE
  • With the official ClickHouse JDBC driver (default since ADB 6.32.0), the sslmode option is currently not supported and the certificate validation cannot be disabled (via sslmode=none) for INSERT operations. If you used sslmode=none before upgrading to ADB 6.32.0, do one of the following:

    • Provide a valid CA certificate file in sslrootcert (recommended).

    • Set client_version 'blynk' on the foreign server or foreign table to keep the legacy behavior.

  • Prior to ADB 6.30.0, SSL was only supported for INSERT operations in ADB ClickHouse Connector. Starting with 6.30.0, you can also use SSL with SELECT.

SSL parameters that the connector uses to connect to ClickHouse are configured via the clickhouse_properties option. For example:

OPTIONS (
    clickhouse_properties 'ssl=true;sslrootcert=/home/gpadmin/tkh_fdw/certs/ca-cert.pem'
)

For a more detailed example, refer to Usage examples.

This option can be used at the foreign server or foreign table level. The following parameters are recognized inside the clickhouse_properties value (format: key=value;key=value;…​).

Name Type Description Default

ssl

BOOL

Enables SSL for connections to ClickHouse. Possible values:

  • true — connection is established over HTTPS;

  • false — connection is established over HTTP (no encryption).

false

sslmode

TEXT

Defines the server certificate verification mode. Possible values:

  • strict — mandatory certificate verification;

  • none — certificate verification is disabled.

For INSERT operations, the option can be used with the legacy clickhouse4j driver only (see client_version above). With the official ClickHouse driver (used by default for INSERT), the option is not supported

strict

sslrootcert

TEXT

Absolute path to the root CA certificate file used to verify the server certificate. The file must be placed on the specified path on all ADB hosts (master and segment hosts)

none

SSL for ADB to PXF

To send data to ClickHouse, the connector establishes a connection between the ADB segment and PXF, which is HTTP by default. If SSL is enabled for PXF in the PXF service parameters, you must instruct the connector to use HTTPS for the ADB to PXF connection (otherwise, an INSERT operation to ClickHouse will fail). To do so, set the pxf_protocol option to 'https'. It can be defined at the following levels (in ascending order of precedence): foreign table, foreign server, and foreign data wrapper.

For example, to set this option at the highest level, add it to the foreign data wrapper:

ALTER FOREIGN DATA WRAPPER tkh_fdw
    OPTIONS (ADD pxf_protocol 'https');

To remove the option from the foreign data wrapper level:

ALTER FOREIGN DATA WRAPPER tkh_fdw
    OPTIONS (DROP pxf_protocol);

Note that the option does not enable SSL for PXF → ClickHouse connections (this is configured in clickhouse_properties) and does not affect SELECT, which reads ClickHouse directly without involving PXF.

Parser options

The connector uses univocity-parsers to parse data before sending it to ClickHouse. You can change the parser parameters using clickhouse_properties at the foreign server or foreign table level.

Option in connector Parser Javadoc Default

parser_auto_closing_enabled

setAutoClosingEnabled

true

parser_column_reordering_enabled

setColumnReorderingEnabled

true

parser_header_extraction_enabled

setHeaderExtractionEnabled

false

parser_ignore_leading_whitespaces

setIgnoreLeadingWhitespaces

true

parser_ignore_trailing_whitespaces

setIgnoreTrailingWhitespaces

true

parser_input_buffer_size

setInputBufferSize

1048576

parser_input_reading_on_separate_thread

setReadInputOnSeparateThread

true

parser_line_separator_detection_enabled

setLineSeparatorDetectionEnabled

false

parser_maximum_number_of_characters_per_column

setMaxCharsPerColumn

4096

parser_maximum_number_of_columns

setMaxColumns

512

parser_null_value

setNullValue

null

parser_number_of_records_to_read

setNumberOfRecordsToRead

-1 (no limit)

parser_error_content_length

setErrorContentLength

-1 (no limit)

parser_skip_bits_as_whitespace

setSkipBitsAsWhitespace

true

parser_skip_empty_lines

setSkipEmptyLines

true

parser_comment_character

setComment

'\0' (disabled)

parser_escape_character

setEscapeChar

'\'

parser_escaped_tab_char

setEscapedTabChar

't'

parser_line_separator

setLineSeparator

system line separator

For example, to override the default limit of 512 columns and insert rows that contain up to 1000 columns, create a foreign table with the parser_maximum_number_of_columns option:

CREATE FOREIGN TABLE wide_table_ft (
    id      int,
    -- ... other columns --
    column_999 text
)
SERVER clickhouse_server
OPTIONS (
    resource 'wide_table',
    clickhouse_properties 'parser_maximum_number_of_columns=1000'
);
Found a mistake? Seleсt text and press Ctrl+Enter to report it