JDBC
允许CH通过 JDBC 连接到外部数据库。
要实现JDBC连接,CH需要使用以后台进程运行的程序 clickhouse-jdbc-bridge。
该引擎支持 Nullable 数据类型。
建表
CREATE TABLE [IF NOT EXISTS] [db.]table_name(columns list...)ENGINE = JDBC(datasource_uri, external_database, external_table)
引擎参数
datasource_uri— 外部DBMS的URI或名字.URI格式:
jdbc:<driver_name>://<host_name>:<port>/?user=<username>&password=<password>. MySQL示例:jdbc:mysql://localhost:3306/?user=root&password=root.external_database— 外部DBMS的数据库名.external_table—external_database中的外部表名或类似select * from table1 where column1=1的查询语句.
用法示例
通过mysql控制台客户端来创建表
Creating a table in MySQL server by connecting directly with it’s console client:
mysql> CREATE TABLE `test`.`test` (-> `int_id` INT NOT NULL AUTO_INCREMENT,-> `int_nullable` INT NULL DEFAULT NULL,-> `float` FLOAT NOT NULL,-> `float_nullable` FLOAT NULL DEFAULT NULL,-> PRIMARY KEY (`int_id`));Query OK, 0 rows affected (0,09 sec)mysql> insert into test (`int_id`, `float`) VALUES (1,2);Query OK, 1 row affected (0,00 sec)mysql> select * from test;+------+----------+-----+----------+| int_id | int_nullable | float | float_nullable |+------+----------+-----+----------+| 1 | NULL | 2 | NULL |+------+----------+-----+----------+1 row in set (0,00 sec)
在CH服务端创建表,并从中查询数据:
CREATE TABLE jdbc_table(`int_id` Int32,`int_nullable` Nullable(Int32),`float` Float32,`float_nullable` Nullable(Float32))ENGINE JDBC('jdbc:mysql://localhost:3306/?user=root&password=root', 'test', 'test')
SELECT *FROM jdbc_table
┌─int_id─┬─int_nullable─┬─float─┬─float_nullable─┐│ 1 │ ᴺᵁᴸᴸ │ 2 │ ᴺᵁᴸᴸ │└────────┴──────────────┴───────┴────────────────┘
INSERT INTO jdbc_table(`int_id`, `float`)SELECT toInt32(number), toFloat32(number * 1.0)FROM system.numbers
