forked from apache/shardingsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,074 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
sharding-jdbc-example/sharding-jdbc-example-jdbc-masterslave/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<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"> | ||
<parent> | ||
<artifactId>sharding-jdbc-example</artifactId> | ||
<groupId>com.dangdang</groupId> | ||
<version>1.2.2-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>sharding-jdbc-example-jdbc-masterslave</artifactId> | ||
</project> |
130 changes: 130 additions & 0 deletions
130
...rslave/src/main/java/com/dangdang/ddframe/rdb/sharding/example/jdbc/masterslave/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Copyright 1999-2015 dangdang.com. | ||
* <p> | ||
* Licensed 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. | ||
* </p> | ||
*/ | ||
|
||
package com.dangdang.ddframe.rdb.sharding.example.jdbc.masterslave; | ||
|
||
import com.dangdang.ddframe.rdb.sharding.api.HintManager; | ||
import com.dangdang.ddframe.rdb.sharding.api.MasterSlaveDataSourceFactory; | ||
import com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule; | ||
import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule; | ||
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule; | ||
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule; | ||
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy; | ||
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy; | ||
import com.dangdang.ddframe.rdb.sharding.example.jdbc.masterslave.algorithm.ModuloDatabaseShardingAlgorithm; | ||
import com.dangdang.ddframe.rdb.sharding.example.jdbc.masterslave.algorithm.ModuloTableShardingAlgorithm; | ||
import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource; | ||
import org.apache.commons.dbcp.BasicDataSource; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public final class Main { | ||
|
||
// CHECKSTYLE:OFF | ||
public static void main(final String[] args) throws SQLException { | ||
// CHECKSTYLE:ON | ||
DataSource dataSource = getShardingDataSource(); | ||
printSimpleSelect(dataSource); | ||
System.out.println("--------------"); | ||
printGroupBy(dataSource); | ||
System.out.println("--------------"); | ||
printHintSimpleSelect(dataSource); | ||
} | ||
|
||
private static void printSimpleSelect(final DataSource dataSource) throws SQLException { | ||
String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=? AND o.order_id=?"; | ||
try ( | ||
Connection conn = dataSource.getConnection(); | ||
PreparedStatement preparedStatement = conn.prepareStatement(sql)) { | ||
preparedStatement.setInt(1, 10); | ||
preparedStatement.setInt(2, 1001); | ||
try (ResultSet rs = preparedStatement.executeQuery()) { | ||
while (rs.next()) { | ||
System.out.println(rs.getInt(1)); | ||
System.out.println(rs.getInt(2)); | ||
System.out.println(rs.getInt(3)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static void printGroupBy(final DataSource dataSource) throws SQLException { | ||
String sql = "SELECT o.user_id, COUNT(*) FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id GROUP BY o.user_id"; | ||
try ( | ||
Connection conn = dataSource.getConnection(); | ||
PreparedStatement preparedStatement = conn.prepareStatement(sql) | ||
) { | ||
ResultSet rs = preparedStatement.executeQuery(); | ||
while (rs.next()) { | ||
System.out.println("user_id: " + rs.getInt(1) + ", count: " + rs.getInt(2)); | ||
} | ||
} | ||
} | ||
|
||
private static void printHintSimpleSelect(final DataSource dataSource) throws SQLException { | ||
String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id"; | ||
try ( | ||
HintManager hintManager = HintManager.getInstance(); | ||
Connection conn = dataSource.getConnection(); | ||
PreparedStatement preparedStatement = conn.prepareStatement(sql)) { | ||
hintManager.addDatabaseShardingValue("t_order", "user_id", 10); | ||
hintManager.addTableShardingValue("t_order", "order_id", 1001); | ||
try (ResultSet rs = preparedStatement.executeQuery()) { | ||
while (rs.next()) { | ||
System.out.println(rs.getInt(1)); | ||
System.out.println(rs.getInt(2)); | ||
System.out.println(rs.getInt(3)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static ShardingDataSource getShardingDataSource() { | ||
DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap()); | ||
TableRule orderTableRule = TableRule.builder("t_order").actualTables(Arrays.asList("t_order_0", "t_order_1")).dataSourceRule(dataSourceRule).build(); | ||
TableRule orderItemTableRule = TableRule.builder("t_order_item").actualTables(Arrays.asList("t_order_item_0", "t_order_item_1")).dataSourceRule(dataSourceRule).build(); | ||
ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Arrays.asList(orderTableRule, orderItemTableRule)) | ||
.bindingTableRules(Collections.singletonList(new BindingTableRule(Arrays.asList(orderTableRule, orderItemTableRule)))) | ||
.databaseShardingStrategy(new DatabaseShardingStrategy("user_id", new ModuloDatabaseShardingAlgorithm())) | ||
.tableShardingStrategy(new TableShardingStrategy("order_id", new ModuloTableShardingAlgorithm())).build(); | ||
return new ShardingDataSource(shardingRule); | ||
} | ||
|
||
private static Map<String, DataSource> createDataSourceMap() { | ||
Map<String, DataSource> result = new HashMap<>(2); | ||
result.put("ds_0", MasterSlaveDataSourceFactory.createDataSource("ds_0", createDataSource("ds_0_master"), createDataSource("ds_0_slave_0"), createDataSource("ds_0_slave_1"))); | ||
result.put("ds_1", MasterSlaveDataSourceFactory.createDataSource("ds_1", createDataSource("ds_1_master"), createDataSource("ds_1_slave_0"), createDataSource("ds_1_slave_1"))); | ||
return result; | ||
} | ||
|
||
private static DataSource createDataSource(final String dataSourceName) { | ||
BasicDataSource result = new BasicDataSource(); | ||
result.setDriverClassName(com.mysql.jdbc.Driver.class.getName()); | ||
result.setUrl(String.format("jdbc:mysql://localhost:3306/%s", dataSourceName)); | ||
result.setUsername("root"); | ||
result.setPassword(""); | ||
return result; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...rame/rdb/sharding/example/jdbc/masterslave/algorithm/ModuloDatabaseShardingAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 1999-2015 dangdang.com. | ||
* <p> | ||
* Licensed 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. | ||
* </p> | ||
*/ | ||
|
||
package com.dangdang.ddframe.rdb.sharding.example.jdbc.masterslave.algorithm; | ||
|
||
import com.dangdang.ddframe.rdb.sharding.api.ShardingValue; | ||
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.SingleKeyDatabaseShardingAlgorithm; | ||
import com.google.common.collect.Range; | ||
|
||
import java.util.Collection; | ||
import java.util.LinkedHashSet; | ||
|
||
public final class ModuloDatabaseShardingAlgorithm implements SingleKeyDatabaseShardingAlgorithm<Integer> { | ||
|
||
@Override | ||
public String doEqualSharding(final Collection<String> dataSourceNames, final ShardingValue<Integer> shardingValue) { | ||
for (String each : dataSourceNames) { | ||
if (each.endsWith(shardingValue.getValue() % 2 + "")) { | ||
return each; | ||
} | ||
} | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
@Override | ||
public Collection<String> doInSharding(final Collection<String> dataSourceNames, final ShardingValue<Integer> shardingValue) { | ||
Collection<String> result = new LinkedHashSet<>(dataSourceNames.size()); | ||
for (Integer value : shardingValue.getValues()) { | ||
for (String dataSourceName : dataSourceNames) { | ||
if (dataSourceName.endsWith(value % 2 + "")) { | ||
result.add(dataSourceName); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public Collection<String> doBetweenSharding(final Collection<String> dataSourceNames, final ShardingValue<Integer> shardingValue) { | ||
Collection<String> result = new LinkedHashSet<>(dataSourceNames.size()); | ||
Range<Integer> range = shardingValue.getValueRange(); | ||
for (Integer i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) { | ||
for (String each : dataSourceNames) { | ||
if (each.endsWith(i % 2 + "")) { | ||
result.add(each); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...ddframe/rdb/sharding/example/jdbc/masterslave/algorithm/ModuloTableShardingAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 1999-2015 dangdang.com. | ||
* <p> | ||
* Licensed 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. | ||
* </p> | ||
*/ | ||
|
||
package com.dangdang.ddframe.rdb.sharding.example.jdbc.masterslave.algorithm; | ||
|
||
import com.dangdang.ddframe.rdb.sharding.api.ShardingValue; | ||
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.SingleKeyTableShardingAlgorithm; | ||
import com.google.common.collect.Range; | ||
|
||
import java.util.Collection; | ||
import java.util.LinkedHashSet; | ||
|
||
public final class ModuloTableShardingAlgorithm implements SingleKeyTableShardingAlgorithm<Integer> { | ||
|
||
@Override | ||
public String doEqualSharding(final Collection<String> tableNames, final ShardingValue<Integer> shardingValue) { | ||
for (String each : tableNames) { | ||
if (each.endsWith(shardingValue.getValue() % 2 + "")) { | ||
return each; | ||
} | ||
} | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Collection<String> doInSharding(final Collection<String> tableNames, final ShardingValue<Integer> shardingValue) { | ||
Collection<String> result = new LinkedHashSet<>(tableNames.size()); | ||
for (Integer value : shardingValue.getValues()) { | ||
for (String tableName : tableNames) { | ||
if (tableName.endsWith(value % 2 + "")) { | ||
result.add(tableName); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public Collection<String> doBetweenSharding(final Collection<String> tableNames, final ShardingValue<Integer> shardingValue) { | ||
Collection<String> result = new LinkedHashSet<>(tableNames.size()); | ||
Range<Integer> range = shardingValue.getValueRange(); | ||
for (Integer i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) { | ||
for (String each : tableNames) { | ||
if (each.endsWith(i % 2 + "")) { | ||
result.add(each); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
} |
Oops, something went wrong.