diff --git a/sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/java/io/shardingsphere/example/hint/raw/jdbc/HintType.java b/sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/java/io/shardingsphere/example/hint/raw/jdbc/HintType.java new file mode 100644 index 0000000000000000000000000000000000000000..dff45b6221514fbbb4756b1d55bb4eeb4cc7d0fd --- /dev/null +++ b/sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/java/io/shardingsphere/example/hint/raw/jdbc/HintType.java @@ -0,0 +1,23 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.shardingsphere.example.hint.raw.jdbc; + +public enum HintType { + + DATABASE_ONLY, DATABASE_TABLES, MASTER_ONLY +} diff --git a/sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/java/io/shardingsphere/example/hint/raw/jdbc/YamlConfigurationExample.java b/sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/java/io/shardingsphere/example/hint/raw/jdbc/YamlConfigurationExample.java index e37cbc32414f1c7d77d4b224bc199b8c501a9427..4c3d945856e7298ed2be7075245aca4a64cc9c6a 100644 --- a/sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/java/io/shardingsphere/example/hint/raw/jdbc/YamlConfigurationExample.java +++ b/sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/java/io/shardingsphere/example/hint/raw/jdbc/YamlConfigurationExample.java @@ -36,8 +36,11 @@ import java.sql.Statement; */ public class YamlConfigurationExample { +// private static final HintType TYPE = HintType.DATABASE_ONLY; + private static final HintType TYPE = HintType.DATABASE_TABLES; + public static void main(final String[] args) throws SQLException, IOException { - DataSource dataSource = YamlShardingDataSourceFactory.createDataSource(getFile("/META-INF/hint-databases.yaml")); + DataSource dataSource = YamlShardingDataSourceFactory.createDataSource(getFile()); CommonService commonService = getCommonService(dataSource); commonService.initEnvironment(); try (HintManager hintManager = HintManager.getInstance()) { @@ -46,8 +49,17 @@ public class YamlConfigurationExample { commonService.cleanEnvironment(); } - private static File getFile(final String fileName) { - return new File(Thread.currentThread().getClass().getResource(fileName).getFile()); + private static File getFile() { + switch (TYPE) { + case DATABASE_ONLY: + return new File(Thread.currentThread().getClass().getResource("/META-INF/hint-databases-only.yaml").getFile()); + case DATABASE_TABLES: + return new File(Thread.currentThread().getClass().getResource("/META-INF/hint-databases-tables.yaml").getFile()); + case MASTER_ONLY: + return new File(Thread.currentThread().getClass().getResource("/META-INF/hint-databases-only.yaml").getFile()); + default: + throw new UnsupportedOperationException("unsupported type"); + } } private static CommonService getCommonService(final DataSource dataSource) { @@ -55,12 +67,27 @@ public class YamlConfigurationExample { } private static void processWithHintValue(final DataSource dataSource, final HintManager hintManager) throws SQLException { - hintManager.addDatabaseShardingValue("t_order", 1L); - hintManager.addTableShardingValue("t_order", 1L); + setHintValue(hintManager); try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { statement.execute("select * from t_order"); + statement.execute("SELECT i.* FROM t_order o, t_order_item i WHERE o.order_id = i.order_id"); statement.execute("select * from t_order_item"); + statement.execute("INSERT INTO t_order (user_id, status) VALUES (1, 'init')"); + } + } + + private static void setHintValue(final HintManager hintManager) { + switch (TYPE) { + case DATABASE_ONLY: + hintManager.setDatabaseShardingValue(1L); + return; + case DATABASE_TABLES: + hintManager.addDatabaseShardingValue("t_order", 1L); + hintManager.addTableShardingValue("t_order", 1L); + return; + default: + throw new UnsupportedOperationException("unsupported type"); } } } diff --git a/sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/resources/META-INF/hint-databases-only.yaml b/sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/resources/META-INF/hint-databases-only.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1eb6971673c5c6628587f5c44e6becc3d677524b --- /dev/null +++ b/sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/resources/META-INF/hint-databases-only.yaml @@ -0,0 +1,29 @@ +dataSources: + ds_0: !!com.zaxxer.hikari.HikariDataSource + driverClassName: com.mysql.jdbc.Driver + jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0 + username: root + password: + ds_1: !!com.zaxxer.hikari.HikariDataSource + driverClassName: com.mysql.jdbc.Driver + jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1 + username: root + password: + +shardingRule: + tables: + t_order: + actualDataNodes: ds_${0..1}.t_order + t_order_item: + actualDataNodes: ds_${0..1}.t_order_item + bindingTables: + - t_order,t_order_item + + defaultDatabaseStrategy: + hint: + algorithmClassName: io.shardingsphere.example.hint.raw.jdbc.ModuloHintShardingAlgorithm + defaultTableStrategy: + none: + +props: + sql.show: true diff --git a/sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/resources/META-INF/hint-databases.yaml b/sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/resources/META-INF/hint-databases-tables.yaml similarity index 77% rename from sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/resources/META-INF/hint-databases.yaml rename to sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/resources/META-INF/hint-databases-tables.yaml index beb8e4ea4fd343d51e95e493c07a2a6034d9743f..f6da0d6e28b8f61d1050d0b939b69fc13a1e73b9 100644 --- a/sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/resources/META-INF/hint-databases.yaml +++ b/sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/resources/META-INF/hint-databases-tables.yaml @@ -13,15 +13,18 @@ dataSources: shardingRule: tables: t_order: - actualDataNodes: ds_${0..1}.t_order + actualDataNodes: ds_${0..1}.t_order_${0..1} databaseStrategy: hint: algorithmClassName: io.shardingsphere.example.hint.raw.jdbc.ModuloHintShardingAlgorithm + tableStrategy: + hint: + algorithmClassName: io.shardingsphere.example.hint.raw.jdbc.ModuloHintShardingAlgorithm keyGenerator: type: SNOWFLAKE column: order_id t_order_item: - actualDataNodes: ds_${0..1}.t_order_item + actualDataNodes: ds_${0..1}.t_order_item_${0..1} bindingTables: - t_order,t_order_item