ClickHouse/docs/zh/engines/table-engines/integrations/jdbc.md

93 lines
2.5 KiB
Markdown
Raw Normal View History

2020-04-03 13:23:32 +00:00
---
toc_priority: 34
toc_title: JDBC表引擎
2020-04-03 13:23:32 +00:00
---
# JDBC {#table-engine-jdbc}
允许CH通过 [JDBC](https://en.wikipedia.org/wiki/Java_Database_Connectivity) 连接到外部数据库。
2020-04-03 13:23:32 +00:00
要实现JDBC连接CH需要使用以后台进程运行的程序 [clickhouse-jdbc-bridge](https://github.com/alex-krash/clickhouse-jdbc-bridge)。
2020-04-03 13:23:32 +00:00
该引擎支持 [Nullable](../../../sql-reference/data-types/nullable.md) 数据类型。
## 建表 {#creating-a-table}
2020-04-03 13:23:32 +00:00
``` sql
CREATE TABLE [IF NOT EXISTS] [db.]table_name
(
columns list...
)
ENGINE = JDBC(dbms_uri, external_database, external_table)
```
**引擎参数**
2020-04-03 13:23:32 +00:00
- `dbms_uri` — 外部DBMS的uri.
2020-04-03 13:23:32 +00:00
格式: `jdbc:<driver_name>://<host_name>:<port>/?user=<username>&password=<password>`.
MySQL示例: `jdbc:mysql://localhost:3306/?user=root&password=root`.
2020-04-03 13:23:32 +00:00
- `external_database` — 外部DBMS的数据库名.
2020-04-03 13:23:32 +00:00
- `external_table``external_database`中的外部表名.
2020-04-03 13:23:32 +00:00
## 用法示例 {#usage-example}
2020-04-03 13:23:32 +00:00
通过mysql控制台客户端来创建表
Creating a table in MySQL server by connecting directly with its console client:
2020-04-03 13:23:32 +00:00
``` text
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;
+------+----------+-----+----------+
2020-04-03 13:23:32 +00:00
| int_id | int_nullable | float | float_nullable |
+------+----------+-----+----------+
2020-04-03 13:23:32 +00:00
| 1 | NULL | 2 | NULL |
+------+----------+-----+----------+
2020-04-03 13:23:32 +00:00
1 row in set (0,00 sec)
```
在CH服务端创建表并从中查询数据
2020-04-03 13:23:32 +00:00
``` sql
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')
```
``` sql
SELECT *
FROM jdbc_table
```
``` text
┌─int_id─┬─int_nullable─┬─float─┬─float_nullable─┐
│ 1 │ ᴺᵁᴸᴸ │ 2 │ ᴺᵁᴸᴸ │
└────────┴──────────────┴───────┴────────────────┘
```
## 参见 {#see-also}
2020-04-03 13:23:32 +00:00
- [JDBC表函数](../../../sql-reference/table-functions/jdbc.md).
2020-04-03 13:23:32 +00:00
[原始文档](https://clickhouse.tech/docs/en/operations/table_engines/jdbc/) <!--hide-->