-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Support for executing DistSQL for ShardingSphere JDBC logical databases #37778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
linghengqian
wants to merge
1
commit into
apache:master
Choose a base branch
from
linghengqian:distsql-new
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
docs/document/content/user-manual/shardingsphere-jdbc/distsql/_index.cn.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| +++ | ||
| title = "DistSQL" | ||
| weight = 6 | ||
| chapter = true | ||
| +++ | ||
|
|
||
| # 背景信息 | ||
|
|
||
| 当前可以通过 ShardingSphere JDBC DataSource 执行 DistSQL,以动态修改 ShardingSphere 配置。 | ||
|
|
||
| # 配置示例 | ||
|
|
||
| ## 前提条件 | ||
|
|
||
| 在业务项目引入如下依赖, | ||
|
|
||
| ```xml | ||
| <dependency> | ||
| <groupId>org.apache.shardingsphere</groupId> | ||
| <artifactId>shardingsphere-jdbc</artifactId> | ||
| <version>${shardingsphere.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.shardingsphere</groupId> | ||
| <artifactId>shardingsphere-jdbc-dialect-mysql</artifactId> | ||
| <version>${shardingsphere.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.shardingsphere</groupId> | ||
| <artifactId>shardingsphere-infra-data-source-pool-hikari</artifactId> | ||
| <version>${shardingsphere.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.shardingsphere</groupId> | ||
| <artifactId>shardingsphere-infra-url-classpath</artifactId> | ||
| <version>${shardingsphere.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.shardingsphere</groupId> | ||
| <artifactId>shardingsphere-standalone-mode-repository-memory</artifactId> | ||
| <version>${shardingsphere.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.shardingsphere</groupId> | ||
| <artifactId>shardingsphere-sharding-core</artifactId> | ||
| <version>${shardingsphere.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.shardingsphere</groupId> | ||
| <artifactId>shardingsphere-authority-simple</artifactId> | ||
| <version>${shardingsphere.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.h2database</groupId> | ||
| <artifactId>h2</artifactId> | ||
| <version>2.2.224</version> | ||
| </dependency> | ||
| ``` | ||
|
|
||
| 在业务项目的 classpath 上编写 ShardingSphere 数据源的配置文件 `demo.yaml`, | ||
|
|
||
| ```yaml | ||
| props: | ||
| sql-show: false | ||
| ``` | ||
|
|
||
| ## 享受集成 | ||
|
|
||
| 创建 ShardingSphere 的数据源以享受集成, | ||
|
|
||
| ```java | ||
| import com.zaxxer.hikari.HikariConfig; | ||
| import com.zaxxer.hikari.HikariDataSource; | ||
| import java.sql.Connection; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
| public class ExampleUtils { | ||
| void test() throws SQLException { | ||
| HikariConfig config = new HikariConfig(); | ||
| config.setJdbcUrl("jdbc:shardingsphere:classpath:demo.yaml"); | ||
| config.setDriverClassName("org.apache.shardingsphere.driver.ShardingSphereDriver"); | ||
| try (HikariDataSource dataSource = new HikariDataSource(config); | ||
| Connection connection = dataSource.getConnection(); | ||
| Statement statement = connection.createStatement()) { | ||
| statement.execute("register storage unit if not exists ds_0 (url='jdbc:h2:mem:local_sharding_ds_0;MODE=MYSQL;IGNORECASE=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE',user='sa',password=''), ds_1 (url='jdbc:h2:mem:local_sharding_ds_1;MODE=MYSQL;IGNORECASE=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE',user='sa',password=''), ds_2 (url='jdbc:h2:mem:local_sharding_ds_2;MODE=MYSQL;IGNORECASE=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE',user='sa',password='')"); | ||
| statement.execute("create default sharding database strategy if not exists (type='standard', sharding_column=user_id,sharding_algorithm(type(name=class_based, properties('strategy'='STANDARD', 'algorithmClassName'='org.apache.shardingsphere.test.natived.commons.algorithm.ClassBasedInlineShardingAlgorithmFixture'))))"); | ||
| statement.execute("create sharding table rule if not exists t_order (datanodes('<LITERAL>ds_0.t_order, ds_1.t_order, ds_2.t_order'), key_generate_strategy(column=order_id,type(name='SNOWFLAKE'))), t_order_item (datanodes('<LITERAL>ds_0.t_order_item, ds_1.t_order_item, ds_2.t_order_item'), key_generate_strategy(column=order_item_id,type(name='SNOWFLAKE')))"); | ||
| statement.execute("create broadcast table rule if not exists t_address"); | ||
| statement.execute("CREATE TABLE IF NOT EXISTS t_order (order_id BIGINT NOT NULL AUTO_INCREMENT,order_type INT(11),user_id INT NOT NULL,address_id BIGINT NOT NULL,status VARCHAR(50),PRIMARY KEY (order_id))"); | ||
| statement.execute("TRUNCATE TABLE t_order"); | ||
| statement.execute("INSERT INTO t_order (user_id, order_type, address_id, status) VALUES (1, 1, 1, 'INSERT_TEST')"); | ||
| statement.execute("SELECT * FROM t_order"); | ||
| statement.execute("DELETE FROM t_order WHERE user_id=1"); | ||
| statement.execute("DROP TABLE IF EXISTS t_order"); | ||
| statement.execute("drop broadcast table rule if exists t_address"); | ||
| statement.execute("drop sharding table rule if exists t_order, t_order_item"); | ||
| statement.execute("drop default sharding database strategy if exists"); | ||
| statement.execute("unregister storage unit if exists ds_0, ds_1, ds_2"); | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| # 使用限制 | ||
|
|
||
| ## YAML 限制 | ||
|
|
||
| 使用 ShardingSphere JDBC Driver 前,总是需要创建 YAML 作为配置文件。 | ||
|
|
||
| ## 属性限制 | ||
|
|
||
| 如果需要定义 YAML 配置的 `props` 属性和 `databaseName` 属性,总是需要在 YAML 文件内配置,且无法修改。 | ||
| 可能的配置文件如下, | ||
|
|
||
| ```yaml | ||
| databaseName: logic_db | ||
| props: | ||
| sql-show: false | ||
| ``` | ||
|
|
||
| ## SQL 限制 | ||
|
|
||
| 若不在 YAML 配置文件内定义 `databaseName` 属性,则默认的逻辑数据库名为 `logic_db`。 | ||
| 此时,无法针对 ShardingSphere JDBC DataSource 执行 `CREATE DATABASE sharding_db` 或 `USE sharding_db` 这类创建新的逻辑数据库的 SQL。 | ||
125 changes: 125 additions & 0 deletions
125
docs/document/content/user-manual/shardingsphere-jdbc/distsql/_index.en.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| +++ | ||
| title = "DistSQL" | ||
| weight = 6 | ||
| chapter = true | ||
| +++ | ||
|
|
||
| # Background Information | ||
|
|
||
| Currently, DistSQL can be executed via ShardingSphere JDBC DataSource to dynamically modify ShardingSphere configuration. | ||
|
|
||
| # Configuration Example | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| Include the following dependencies in your business project: | ||
|
|
||
| ```xml | ||
| <dependency> | ||
| <groupId>org.apache.shardingsphere</groupId> | ||
| <artifactId>shardingsphere-jdbc</artifactId> | ||
| <version>${shardingsphere.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.shardingsphere</groupId> | ||
| <artifactId>shardingsphere-jdbc-dialect-mysql</artifactId> | ||
| <version>${shardingsphere.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.shardingsphere</groupId> | ||
| <artifactId>shardingsphere-infra-data-source-pool-hikari</artifactId> | ||
| <version>${shardingsphere.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.shardingsphere</groupId> | ||
| <artifactId>shardingsphere-infra-url-classpath</artifactId> | ||
| <version>${shardingsphere.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.shardingsphere</groupId> | ||
| <artifactId>shardingsphere-standalone-mode-repository-memory</artifactId> | ||
| <version>${shardingsphere.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.shardingsphere</groupId> | ||
| <artifactId>shardingsphere-sharding-core</artifactId> | ||
| <version>${shardingsphere.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.shardingsphere</groupId> | ||
| <artifactId>shardingsphere-authority-simple</artifactId> | ||
| <version>${shardingsphere.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.h2database</groupId> | ||
| <artifactId>h2</artifactId> | ||
| <version>2.2.224</version> | ||
| </dependency> | ||
| ``` | ||
|
|
||
| Write the ShardingSphere data source configuration file `demo.yaml` on the classpath of your business project. | ||
|
|
||
| ```yaml | ||
| props: | ||
| sql-show: false | ||
| ``` | ||
|
|
||
| ## Enjoy the Integration | ||
|
|
||
| Create a ShardingSphere data source to enjoy integration. | ||
|
|
||
| ```java | ||
| import com.zaxxer.hikari.HikariConfig; | ||
| import com.zaxxer.hikari.HikariDataSource; | ||
| import java.sql.Connection; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
| public class ExampleUtils { | ||
| void test() throws SQLException { | ||
linghengqian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| HikariConfig config = new HikariConfig(); | ||
| config.setJdbcUrl("jdbc:shardingsphere:classpath:demo.yaml"); | ||
| config.setDriverClassName("org.apache.shardingsphere.driver.ShardingSphereDriver"); | ||
| try (HikariDataSource dataSource = new HikariDataSource(config); | ||
| Connection connection = dataSource.getConnection(); | ||
| Statement statement = connection.createStatement()) { | ||
| statement.execute("register storage unit if not exists ds_0 (url='jdbc:h2:mem:local_sharding_ds_0;MODE=MYSQL;IGNORECASE=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE',user='sa',password=''), ds_1 (url='jdbc:h2:mem:local_sharding_ds_1;MODE=MYSQL;IGNORECASE=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE',user='sa',password=''), ds_2 (url='jdbc:h2:mem:local_sharding_ds_2;MODE=MYSQL;IGNORECASE=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE',user='sa',password='')"); | ||
| statement.execute("create default sharding database strategy if not exists (type='standard', sharding_column=user_id,sharding_algorithm(type(name=class_based, properties('strategy'='STANDARD', 'algorithmClassName'='org.apache.shardingsphere.test.natived.commons.algorithm.ClassBasedInlineShardingAlgorithmFixture'))))"); | ||
| statement.execute("create sharding table rule if not exists t_order (datanodes('<LITERAL>ds_0.t_order, ds_1.t_order, ds_2.t_order'), key_generate_strategy(column=order_id,type(name='SNOWFLAKE'))), t_order_item (datanodes('<LITERAL>ds_0.t_order_item, ds_1.t_order_item, ds_2.t_order_item'), key_generate_strategy(column=order_item_id,type(name='SNOWFLAKE')))"); | ||
| statement.execute("create broadcast table rule if not exists t_address"); | ||
| statement.execute("CREATE TABLE IF NOT EXISTS t_order (order_id BIGINT NOT NULL AUTO_INCREMENT,order_type INT(11),user_id INT NOT NULL,address_id BIGINT NOT NULL,status VARCHAR(50),PRIMARY KEY (order_id))"); | ||
| statement.execute("TRUNCATE TABLE t_order"); | ||
| statement.execute("INSERT INTO t_order (user_id, order_type, address_id, status) VALUES (1, 1, 1, 'INSERT_TEST')"); | ||
| statement.execute("SELECT * FROM t_order"); | ||
| statement.execute("DELETE FROM t_order WHERE user_id=1"); | ||
| statement.execute("DROP TABLE IF EXISTS t_order"); | ||
| statement.execute("drop broadcast table rule if exists t_address"); | ||
| statement.execute("drop sharding table rule if exists t_order, t_order_item"); | ||
| statement.execute("drop default sharding database strategy if exists"); | ||
| statement.execute("unregister storage unit if exists ds_0, ds_1, ds_2"); | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| # Usage Restrictions | ||
|
|
||
| ## YAML Restrictions | ||
|
|
||
| A YAML configuration file must always be created before using the ShardingSphere JDBC Driver. | ||
|
|
||
| ## Property Restrictions | ||
|
|
||
| If you need to define the `props` and `databaseName` properties in the YAML configuration, you must configure them within the YAML file and cannot modify them. | ||
| A possible configuration file is as follows: | ||
|
|
||
| ```yaml | ||
| databaseName: logic_db | ||
| props: | ||
| sql-show: false | ||
| ``` | ||
|
|
||
| ## SQL Restrictions | ||
|
|
||
| If the `databaseName` property is not defined in the YAML configuration file, | ||
| the default logical database name is `logic_db`. | ||
| In this case, you cannot execute SQL statements like `CREATE DATABASE sharding_db` or `USE sharding_db` to create a new logical database against the ShardingSphere JDBC DataSource. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.