This is an automated email from the ASF dual-hosted git repository.

jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 207cfeb3021 Add raw java example to 
`class-based-sharding-algorithm-example` (#20759)
207cfeb3021 is described below

commit 207cfeb3021634b0fbc7b9e5a0c91d3d8c0bb8a2
Author: lushaorong <[email protected]>
AuthorDate: Tue Sep 6 10:09:25 2022 +0800

    Add raw java example to `class-based-sharding-algorithm-example` (#20759)
    
    * Add raw java example to `class-based-sharding-algorithm-example`
    
    Signed-off-by: lushaorong <[email protected]>
    
    * Add raw java example to `class-based-sharding-algorithm-example`
    
    Signed-off-by: lushaorong <[email protected]>
    
    Signed-off-by: lushaorong <[email protected]>
---
 ...ssBasedShardingRawJavaConfigurationExample.java | 40 +++++++++++
 .../ClassBasedShardingRawJavaConfiguration.java    | 77 ++++++++++++++++++++++
 .../raw/jdbc/factory/DataSourceFactory.java        | 37 +++++++++++
 3 files changed, 154 insertions(+)

diff --git 
a/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/class-based-sharding-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/extension/classbased/sharding/raw/jdbc/ClassBasedShardingRawJavaConfigurationExample.java
 
b/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/
 [...]
new file mode 100644
index 00000000000..ee0698ace2a
--- /dev/null
+++ 
b/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/class-based-sharding-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/extension/classbased/sharding/raw/jdbc/ClassBasedShardingRawJavaConfigurationExample.java
@@ -0,0 +1,40 @@
+/*
+ * 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.classbased.sharding.raw.jdbc;
+
+import org.apache.shardingsphere.example.core.api.ExampleExecuteTemplate;
+import org.apache.shardingsphere.example.core.api.service.ExampleService;
+import org.apache.shardingsphere.example.core.jdbc.service.OrderServiceImpl;
+import 
org.apache.shardingsphere.example.extension.classbased.sharding.raw.jdbc.factory.DataSourceFactory;
+import org.apache.shardingsphere.example.type.ShardingType;
+
+import javax.sql.DataSource;
+import java.io.IOException;
+import java.sql.SQLException;
+
+public final class ClassBasedShardingRawJavaConfigurationExample {
+
+    public static void main(final String[] args) throws SQLException, 
IOException {
+        DataSource dataSource = 
DataSourceFactory.newInstance(ShardingType.SHARDING_DATABASES);
+        ExampleExecuteTemplate.run(getExampleService(dataSource));
+    }
+    
+    private static ExampleService getExampleService(final DataSource 
dataSource) {
+        return new OrderServiceImpl(dataSource);
+    }
+}
diff --git 
a/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/class-based-sharding-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/extension/classbased/sharding/raw/jdbc/config/ClassBasedShardingRawJavaConfiguration.java
 
b/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/
 [...]
new file mode 100644
index 00000000000..b500cf24f61
--- /dev/null
+++ 
b/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/class-based-sharding-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/extension/classbased/sharding/raw/jdbc/config/ClassBasedShardingRawJavaConfiguration.java
@@ -0,0 +1,77 @@
+/*
+ * 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.classbased.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 ClassBasedShardingRawJavaConfiguration 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", "database-classbased"));
+        Properties props = new Properties();
+        props.setProperty("strategy", "standard");
+        props.setProperty("algorithmClassName", 
"org.apache.shardingsphere.example.extension.classbased.sharding.raw.jdbc.fixture.ClassBasedStandardShardingAlgorithmFixture");
+        props.setProperty("sharding-count", "2");
+        result.getShardingAlgorithms().put("database-classbased", new 
AlgorithmConfiguration("CLASS_BASED", props));
+        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");
+        result.setKeyGenerateStrategy(new 
KeyGenerateStrategyConfiguration("order_id", "snowflake"));
+        return result;
+    }
+    
+    private ShardingTableRuleConfiguration 
getOrderItemTableRuleConfiguration() {
+        ShardingTableRuleConfiguration result = new 
ShardingTableRuleConfiguration("t_order_item", "demo_ds_${0..1}.t_order_item");
+        result.setKeyGenerateStrategy(new 
KeyGenerateStrategyConfiguration("order_item_id", "snowflake"));
+        return result;
+    }
+}
diff --git 
a/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/class-based-sharding-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/extension/classbased/sharding/raw/jdbc/factory/DataSourceFactory.java
 
b/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/class-based-sharding
 [...]
new file mode 100644
index 00000000000..e65fbd9cbd5
--- /dev/null
+++ 
b/examples/shardingsphere-jdbc-example/single-feature-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/class-based-sharding-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/extension/classbased/sharding/raw/jdbc/factory/DataSourceFactory.java
@@ -0,0 +1,37 @@
+/*
+ * 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.classbased.sharding.raw.jdbc.factory;
+
+import 
org.apache.shardingsphere.example.extension.classbased.sharding.raw.jdbc.config.ClassBasedShardingRawJavaConfiguration;
+import org.apache.shardingsphere.example.type.ShardingType;
+
+import javax.sql.DataSource;
+import java.io.IOException;
+import java.sql.SQLException;
+
+public final class DataSourceFactory {
+    
+    public static DataSource newInstance(final ShardingType shardingType) 
throws SQLException, IOException {
+        switch (shardingType) {
+            case SHARDING_DATABASES:
+                return new 
ClassBasedShardingRawJavaConfiguration().getDataSource();
+            default:
+                throw new UnsupportedOperationException(shardingType.name());
+        }
+    }
+}

Reply via email to