lushaorong commented on code in PR #20341: URL: https://github.com/apache/shardingsphere/pull/20341#discussion_r953303624
########## examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/spi-based-sharding-algorithm-example/spi-based-sharding-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/extension/spibased/sharding/raw/jdbc/config/SPIBasedShardingRawJavaConfiguration.java: ########## @@ -0,0 +1,73 @@ +/* + * 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 org.apache.shardingsphere.example.extension.spibased.sharding.raw.jdbc.config; + +import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory; +import org.apache.shardingsphere.example.config.ExampleConfiguration; +import org.apache.shardingsphere.example.core.api.DataSourceUtil; +import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; +import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; +import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration; + +import javax.sql.DataSource; +import java.sql.SQLException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +public final class SPIBasedShardingRawJavaConfiguration implements ExampleConfiguration { + + @Override + public DataSource getDataSource() throws SQLException { + return ShardingSphereDataSourceFactory.createDataSource(createDataSourceMap(), Collections.singletonList(createShardingRuleConfiguration()), new Properties()); + } + + private Map<String, DataSource> createDataSourceMap() { + Map<String, DataSource> result = new HashMap<>(); + result.put("demo_ds_0", DataSourceUtil.createDataSource("demo_ds_0")); + result.put("demo_ds_1", DataSourceUtil.createDataSource("demo_ds_1")); + return result; + } + + private ShardingRuleConfiguration createShardingRuleConfiguration() { + ShardingRuleConfiguration result = new ShardingRuleConfiguration(); + result.getTables().add(getOrderTableRuleConfiguration()); + result.getTables().add(getOrderItemTableRuleConfiguration()); + result.getBindingTableGroups().add("t_order, t_order_item"); + result.getBroadcastTables().add("t_address"); + result.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "spi_based")); + result.getShardingAlgorithms().put("spi_based", new AlgorithmConfiguration("SPI_BASED", new Properties())); + result.getKeyGenerators().put("snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties())); + return result; + } + + private ShardingTableRuleConfiguration getOrderTableRuleConfiguration() { + ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_order", "demo_ds_${0..1}.t_order_${[0, 1]}"); Review Comment: You're right. I'll change it to SHARDING_DATABASES_AND_TABLES -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
