Loading sharding-jdbc-example/hint-example/hint-raw-jdbc-example/pom.xml 0 → 100644 +42 −0 Original line number Diff line number Diff line <?xml version="1.0" encoding="UTF-8"?> <!-- ~ 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. --> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>io.shardingsphere</groupId> <artifactId>hint-example</artifactId> <version>4.0.0-RC1-SNAPSHOT</version> </parent> <artifactId>hint-raw-jdbc-example</artifactId> <dependencies> <dependency> <groupId>io.shardingsphere</groupId> <artifactId>repository-jdbc</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-core</artifactId> </dependency> </dependencies> </project> sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/java/io/shardingsphere/example/hint/raw/jdbc/HintType.java 0 → 100644 +23 −0 Original line number Diff line number Diff line /* * 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 } sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/java/io/shardingsphere/example/hint/raw/jdbc/ModuloHintShardingAlgorithm.java 0 → 100644 +37 −0 Original line number Diff line number Diff line /* * 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; import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm; import org.apache.shardingsphere.api.sharding.hint.HintShardingValue; import java.util.Collection; import java.util.Collections; public final class ModuloHintShardingAlgorithm implements HintShardingAlgorithm<Long> { @Override public Collection<String> doSharding(final Collection<String> availableTargetNames, final HintShardingValue<Long> shardingValue) { for (String each : availableTargetNames) { if (each.endsWith(String.valueOf(shardingValue.getValues().iterator().next() % 2))) { return Collections.singletonList(each); } } throw new UnsupportedOperationException(); } } sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/java/io/shardingsphere/example/hint/raw/jdbc/YamlConfigurationExample.java 0 → 100644 +99 −0 Original line number Diff line number Diff line /* * 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; import io.shardingsphere.example.common.jdbc.repository.OrderItemRepositoryImpl; import io.shardingsphere.example.common.jdbc.repository.OrderRepositoryImpl; import io.shardingsphere.example.common.jdbc.service.CommonServiceImpl; import io.shardingsphere.example.common.service.CommonService; import org.apache.shardingsphere.api.hint.HintManager; import org.apache.shardingsphere.shardingjdbc.api.yaml.YamlMasterSlaveDataSourceFactory; import org.apache.shardingsphere.shardingjdbc.api.yaml.YamlShardingDataSourceFactory; import javax.sql.DataSource; import java.io.File; import java.io.IOException; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; public class YamlConfigurationExample { private static final HintType TYPE = HintType.DATABASE_TABLES; // private static final HintType TYPE = HintType.DATABASE_ONLY; // private static final HintType TYPE = HintType.MASTER_ONLY; public static void main(final String[] args) throws SQLException, IOException { DataSource dataSource = getDataSource(); CommonService commonService = getCommonService(dataSource); commonService.initEnvironment(); processWithHintValue(dataSource); commonService.cleanEnvironment(); } private static DataSource getDataSource() throws IOException, SQLException { switch (TYPE) { case DATABASE_TABLES: return YamlShardingDataSourceFactory.createDataSource(getFile("/META-INF/hint-databases-tables.yaml")); case DATABASE_ONLY: return YamlShardingDataSourceFactory.createDataSource(getFile("/META-INF/hint-databases-only.yaml")); case MASTER_ONLY: return YamlMasterSlaveDataSourceFactory.createDataSource(getFile("/META-INF/hint-master-only.yaml")); default: throw new UnsupportedOperationException("unsupported type"); } } private static File getFile(final String configFile) { return new File(Thread.currentThread().getClass().getResource(configFile).getFile()); } private static CommonService getCommonService(final DataSource dataSource) { return new CommonServiceImpl(new OrderRepositoryImpl(dataSource), new OrderItemRepositoryImpl(dataSource)); } private static void processWithHintValue(final DataSource dataSource) throws SQLException { try (HintManager hintManager = HintManager.getInstance(); Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { setHintValue(hintManager); 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_TABLES: hintManager.addDatabaseShardingValue("t_order", 1L); hintManager.addTableShardingValue("t_order", 1L); return; case DATABASE_ONLY: hintManager.setDatabaseShardingValue(1L); return; case MASTER_ONLY: hintManager.setMasterRouteOnly(); return; default: throw new UnsupportedOperationException("unsupported type"); } } } sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/resources/META-INF/hint-databases-only.yaml 0 → 100644 +29 −0 Original line number Diff line number Diff line 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 Loading
sharding-jdbc-example/hint-example/hint-raw-jdbc-example/pom.xml 0 → 100644 +42 −0 Original line number Diff line number Diff line <?xml version="1.0" encoding="UTF-8"?> <!-- ~ 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. --> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>io.shardingsphere</groupId> <artifactId>hint-example</artifactId> <version>4.0.0-RC1-SNAPSHOT</version> </parent> <artifactId>hint-raw-jdbc-example</artifactId> <dependencies> <dependency> <groupId>io.shardingsphere</groupId> <artifactId>repository-jdbc</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-core</artifactId> </dependency> </dependencies> </project>
sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/java/io/shardingsphere/example/hint/raw/jdbc/HintType.java 0 → 100644 +23 −0 Original line number Diff line number Diff line /* * 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 }
sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/java/io/shardingsphere/example/hint/raw/jdbc/ModuloHintShardingAlgorithm.java 0 → 100644 +37 −0 Original line number Diff line number Diff line /* * 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; import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm; import org.apache.shardingsphere.api.sharding.hint.HintShardingValue; import java.util.Collection; import java.util.Collections; public final class ModuloHintShardingAlgorithm implements HintShardingAlgorithm<Long> { @Override public Collection<String> doSharding(final Collection<String> availableTargetNames, final HintShardingValue<Long> shardingValue) { for (String each : availableTargetNames) { if (each.endsWith(String.valueOf(shardingValue.getValues().iterator().next() % 2))) { return Collections.singletonList(each); } } throw new UnsupportedOperationException(); } }
sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/java/io/shardingsphere/example/hint/raw/jdbc/YamlConfigurationExample.java 0 → 100644 +99 −0 Original line number Diff line number Diff line /* * 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; import io.shardingsphere.example.common.jdbc.repository.OrderItemRepositoryImpl; import io.shardingsphere.example.common.jdbc.repository.OrderRepositoryImpl; import io.shardingsphere.example.common.jdbc.service.CommonServiceImpl; import io.shardingsphere.example.common.service.CommonService; import org.apache.shardingsphere.api.hint.HintManager; import org.apache.shardingsphere.shardingjdbc.api.yaml.YamlMasterSlaveDataSourceFactory; import org.apache.shardingsphere.shardingjdbc.api.yaml.YamlShardingDataSourceFactory; import javax.sql.DataSource; import java.io.File; import java.io.IOException; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; public class YamlConfigurationExample { private static final HintType TYPE = HintType.DATABASE_TABLES; // private static final HintType TYPE = HintType.DATABASE_ONLY; // private static final HintType TYPE = HintType.MASTER_ONLY; public static void main(final String[] args) throws SQLException, IOException { DataSource dataSource = getDataSource(); CommonService commonService = getCommonService(dataSource); commonService.initEnvironment(); processWithHintValue(dataSource); commonService.cleanEnvironment(); } private static DataSource getDataSource() throws IOException, SQLException { switch (TYPE) { case DATABASE_TABLES: return YamlShardingDataSourceFactory.createDataSource(getFile("/META-INF/hint-databases-tables.yaml")); case DATABASE_ONLY: return YamlShardingDataSourceFactory.createDataSource(getFile("/META-INF/hint-databases-only.yaml")); case MASTER_ONLY: return YamlMasterSlaveDataSourceFactory.createDataSource(getFile("/META-INF/hint-master-only.yaml")); default: throw new UnsupportedOperationException("unsupported type"); } } private static File getFile(final String configFile) { return new File(Thread.currentThread().getClass().getResource(configFile).getFile()); } private static CommonService getCommonService(final DataSource dataSource) { return new CommonServiceImpl(new OrderRepositoryImpl(dataSource), new OrderItemRepositoryImpl(dataSource)); } private static void processWithHintValue(final DataSource dataSource) throws SQLException { try (HintManager hintManager = HintManager.getInstance(); Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) { setHintValue(hintManager); 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_TABLES: hintManager.addDatabaseShardingValue("t_order", 1L); hintManager.addTableShardingValue("t_order", 1L); return; case DATABASE_ONLY: hintManager.setDatabaseShardingValue(1L); return; case MASTER_ONLY: hintManager.setMasterRouteOnly(); return; default: throw new UnsupportedOperationException("unsupported type"); } } }
sharding-jdbc-example/hint-example/hint-raw-jdbc-example/src/main/resources/META-INF/hint-databases-only.yaml 0 → 100644 +29 −0 Original line number Diff line number Diff line 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