JDBC

Allows ClickHouse to connect to external databases via JDBC.

To implement the JDBC connection, ClickHouse uses the separate program clickhouse-jdbc-bridge that should run as a daemon.

This engine supports the Nullable data type.

Creating a Table

  1. CREATE TABLE [IF NOT EXISTS] [db.]table_name
  2. ENGINE = JDBC(dbms_uri, external_database, external_table)

Engine Parameters

  • dbms_uri — URI of an external DBMS.

    Format: jdbc:<driver_name>://<host_name>:<port>/?user=<username>&password=<password>.
    Example for MySQL: jdbc:mysql://localhost:3306/?user=root&password=root.

  • external_database — Database in an external DBMS.

  • external_table — Name of the table in external_database.

Usage Example

Creating a table in MySQL server by connecting directly with it’s console client:

  1. mysql> CREATE TABLE `test`.`test` (
  2. -> `int_id` INT NOT NULL AUTO_INCREMENT,
  3. -> `int_nullable` INT NULL DEFAULT NULL,
  4. -> `float` FLOAT NOT NULL,
  5. -> `float_nullable` FLOAT NULL DEFAULT NULL,
  6. -> PRIMARY KEY (`int_id`));
  7. Query OK, 0 rows affected (0,09 sec)
  8. mysql> insert into test (`int_id`, `float`) VALUES (1,2);
  9. Query OK, 1 row affected (0,00 sec)
  10. mysql> select * from test;
  11. +--------+--------------+-------+----------------+
  12. | int_id | int_nullable | float | float_nullable |
  13. +--------+--------------+-------+----------------+
  14. | 1 | NULL | 2 | NULL |
  15. +--------+--------------+-------+----------------+
  16. 1 row in set (0,00 sec)

Creating a table in ClickHouse server and selecting data from it:

  1. CREATE TABLE jdbc_table ENGINE JDBC('jdbc:mysql://localhost:3306/?user=root&password=root', 'test', 'test')
  2. Ok.
  3. DESCRIBE TABLE jdbc_table
  4. ┌─name───────────────┬─type───────────────┬─default_type─┬─default_expression─┐
  5. int_id Int32
  6. int_nullable Nullable(Int32)
  7. float Float32
  8. float_nullable Nullable(Float32)
  9. └────────────────────┴────────────────────┴──────────────┴────────────────────┘
  10. 10 rows in set. Elapsed: 0.031 sec.
  11. SELECT *
  12. FROM jdbc_table
  13. ┌─int_id─┬─int_nullable─┬─float─┬─float_nullable─┐
  14. 1 ᴺᵁᴸᴸ 2 ᴺᵁᴸᴸ
  15. └────────┴──────────────┴───────┴────────────────┘
  16. 1 rows in set. Elapsed: 0.055 sec.

See Also

Original article