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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

98 lines
2.7 KiB
Markdown
Raw Normal View History

2020-04-03 13:23:32 +00:00
---
2022-08-26 19:07:59 +00:00
slug: /zh/engines/table-engines/integrations/jdbc
2022-04-10 23:08:18 +00:00
sidebar_position: 34
sidebar_label: 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
2021-06-11 09:37:38 +00:00
要实现JDBC连接CH需要使用以后台进程运行的程序 [clickhouse-jdbc-bridge](https://github.com/ClickHouse/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...
)
2021-06-11 09:37:38 +00:00
ENGINE = JDBC(datasource_uri, external_database, external_table)
2020-04-03 13:23:32 +00:00
```
**引擎参数**
2020-04-03 13:23:32 +00:00
2021-06-11 09:37:38 +00:00
- `datasource_uri` — 外部DBMS的URI或名字.
2020-04-03 13:23:32 +00:00
2021-06-11 09:37:38 +00:00
URI格式: `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
2021-06-11 09:37:38 +00:00
- `external_table``external_database`中的外部表名或类似`select * from table1 where column1=1`的查询语句.
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 │ ᴺᵁᴸᴸ │
└────────┴──────────────┴───────┴────────────────┘
```
2021-06-11 09:37:38 +00:00
``` sql
INSERT INTO jdbc_table(`int_id`, `float`)
SELECT toInt32(number), toFloat32(number * 1.0)
FROM system.numbers
```
## 参见 {#see-also}
2020-04-03 13:23:32 +00:00
- [JDBC表函数](../../../sql-reference/table-functions/jdbc.md).