|
| 1 | ++++ |
| 2 | +title = "DistSQL" |
| 3 | +weight = 6 |
| 4 | +chapter = true |
| 5 | ++++ |
| 6 | + |
| 7 | +# Background Information |
| 8 | + |
| 9 | +Currently, DistSQL can be executed via ShardingSphere JDBC DataSource to dynamically modify ShardingSphere configuration. |
| 10 | + |
| 11 | +# Configuration Example |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +Include the following dependencies in your business project: |
| 16 | + |
| 17 | +```xml |
| 18 | +<dependency> |
| 19 | + <groupId>org.apache.shardingsphere</groupId> |
| 20 | + <artifactId>shardingsphere-jdbc</artifactId> |
| 21 | + <version>${shardingsphere.version}</version> |
| 22 | +</dependency> |
| 23 | +<dependency> |
| 24 | + <groupId>org.apache.shardingsphere</groupId> |
| 25 | + <artifactId>shardingsphere-jdbc-dialect-mysql</artifactId> |
| 26 | + <version>${shardingsphere.version}</version> |
| 27 | +</dependency> |
| 28 | +<dependency> |
| 29 | + <groupId>org.apache.shardingsphere</groupId> |
| 30 | + <artifactId>shardingsphere-infra-data-source-pool-hikari</artifactId> |
| 31 | + <version>${shardingsphere.version}</version> |
| 32 | +</dependency> |
| 33 | +<dependency> |
| 34 | + <groupId>org.apache.shardingsphere</groupId> |
| 35 | + <artifactId>shardingsphere-infra-url-classpath</artifactId> |
| 36 | + <version>${shardingsphere.version}</version> |
| 37 | +</dependency> |
| 38 | +<dependency> |
| 39 | + <groupId>org.apache.shardingsphere</groupId> |
| 40 | + <artifactId>shardingsphere-standalone-mode-repository-memory</artifactId> |
| 41 | + <version>${shardingsphere.version}</version> |
| 42 | +</dependency> |
| 43 | +<dependency> |
| 44 | + <groupId>org.apache.shardingsphere</groupId> |
| 45 | + <artifactId>shardingsphere-sharding-core</artifactId> |
| 46 | + <version>${shardingsphere.version}</version> |
| 47 | +</dependency> |
| 48 | +<dependency> |
| 49 | + <groupId>org.apache.shardingsphere</groupId> |
| 50 | + <artifactId>shardingsphere-authority-simple</artifactId> |
| 51 | + <version>${shardingsphere.version}</version> |
| 52 | +</dependency> |
| 53 | +<dependency> |
| 54 | + <groupId>com.h2database</groupId> |
| 55 | + <artifactId>h2</artifactId> |
| 56 | + <version>2.2.224</version> |
| 57 | +</dependency> |
| 58 | +``` |
| 59 | + |
| 60 | +Write the ShardingSphere data source configuration file `demo.yaml` on the classpath of your business project. |
| 61 | + |
| 62 | +```yaml |
| 63 | +props: |
| 64 | + sql-show: false |
| 65 | +``` |
| 66 | +
|
| 67 | +## Enjoy the Integration |
| 68 | +
|
| 69 | +Create a ShardingSphere data source to enjoy integration. |
| 70 | +
|
| 71 | +```java |
| 72 | +import com.zaxxer.hikari.HikariConfig; |
| 73 | +import com.zaxxer.hikari.HikariDataSource; |
| 74 | +import java.sql.Connection; |
| 75 | +import java.sql.SQLException; |
| 76 | +import java.sql.Statement; |
| 77 | +public class ExampleUtils { |
| 78 | + void test() throws SQLException { |
| 79 | + HikariConfig config = new HikariConfig(); |
| 80 | + config.setJdbcUrl("jdbc:shardingsphere:classpath:demo.yaml"); |
| 81 | + config.setDriverClassName("org.apache.shardingsphere.driver.ShardingSphereDriver"); |
| 82 | + try (HikariDataSource dataSource = new HikariDataSource(config); |
| 83 | + Connection connection = dataSource.getConnection(); |
| 84 | + Statement statement = connection.createStatement()) { |
| 85 | + 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='')"); |
| 86 | + 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'))))"); |
| 87 | + 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')))"); |
| 88 | + statement.execute("create broadcast table rule if not exists t_address"); |
| 89 | + 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))"); |
| 90 | + statement.execute("TRUNCATE TABLE t_order"); |
| 91 | + statement.execute("INSERT INTO t_order (user_id, order_type, address_id, status) VALUES (1, 1, 1, 'INSERT_TEST')"); |
| 92 | + statement.execute("SELECT * FROM t_order"); |
| 93 | + statement.execute("DELETE FROM t_order WHERE user_id=1"); |
| 94 | + statement.execute("DROP TABLE IF EXISTS t_order"); |
| 95 | + statement.execute("drop broadcast table rule if exists t_address"); |
| 96 | + statement.execute("drop sharding table rule if exists t_order, t_order_item"); |
| 97 | + statement.execute("drop default sharding database strategy if exists"); |
| 98 | + statement.execute("unregister storage unit if exists ds_0, ds_1, ds_2"); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +# Usage Restrictions |
| 105 | + |
| 106 | +## YAML Restrictions |
| 107 | + |
| 108 | +A YAML configuration file must always be created before using the ShardingSphere JDBC Driver. |
| 109 | + |
| 110 | +## Property Restrictions |
| 111 | + |
| 112 | +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. |
| 113 | +A possible configuration file is as follows: |
| 114 | + |
| 115 | +```yaml |
| 116 | +databaseName: logic_db |
| 117 | +props: |
| 118 | + sql-show: false |
| 119 | +``` |
| 120 | +
|
| 121 | +## SQL Restrictions |
| 122 | +
|
| 123 | +If the `databaseName` property is not defined in the YAML configuration file, |
| 124 | +the default logical database name is `logic_db`. |
| 125 | +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. |
0 commit comments