You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2020/08/20 07:16:49 UTC
[GitHub] [shardingsphere] RafaelGuo commented on issue #6948: PreciseShardingAlgorithm does not work
RafaelGuo commented on issue #6948:
URL: https://github.com/apache/shardingsphere/issues/6948#issuecomment-677402250
> @RafaelGuo could you show more details ?
now, I have three tables: sns_resource_basic, sns_resource_cache, sns_resource_essay. When i insert data to these tables, data should be inserted to a same database, sharding0 or sharding1, but In fact,data is insert to two deffierent databases. Please help me to check it.
1. my class
`public class CustomPreciseShardingAlgorithm implements PreciseShardingAlgorithm<String> {
private static final Logger logger = LoggerFactory.getLogger(CustomPreciseShardingAlgorithm.class);
public CustomPreciseShardingAlgorithm() {
logger.info("CustomPreciseShardingAlgorithm 初始化");
}
@Override
public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {
int hashedKey = Math.abs(preciseShardingValue.getValue().hashCode());
for (Object dbName : collection) {
String dbNameStr = (String) dbName;
String dbRange = dbNameStr.substring(dbNameStr.indexOf("-") + 1);
String[] dbRangeArr = dbRange.split("-");
Integer begin = Integer.valueOf(dbRangeArr[0]);
Integer end = Integer.valueOf(dbRangeArr[1]);
if (hashedKey >= begin && hashedKey <= end) {
return dbNameStr;
}
}
return null;
}
}`
2. my configuration
`spring.shardingsphere.datasource.names=sharding0,sharding1
spring.shardingsphere.datasource.sharding0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.sharding0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.sharding0.url=jdbc:mysql://127.0.0.1:3306/sharding0?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.shardingsphere.datasource.sharding0.username=root
spring.shardingsphere.datasource.sharding0.password=123456
spring.shardingsphere.datasource.sharding1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.sharding1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.sharding1.url=jdbc:mysql://127.0.0.1:3306/sharding1?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.shardingsphere.datasource.sharding1.username=root
spring.shardingsphere.datasource.sharding1.password=123456
spring.shardingsphere.rules.sharding.tables.sns_resource_basic.actual-data-nodes=sharding0.sns_resource_basic,sharding1.sns_resource_basic
spring.shardingsphere.rules.sharding.tables.sns_resource_essay.actual-data-nodes=sharding0.sns_resource_essay,sharding1.sns_resource_essay
spring.shardingsphere.rules.sharding.tables.sns_resource_cache.actual-data-nodes=sharding0.sns_resource_cache,sharding1.sns_resource_cache
spring.shardingsphere.rules.sharding.tables.sns_resource_basic.database-strategy.inline.sharding-column=res_id
spring.shardingsphere.rules.sharding.tables.sns_resource_basic.database-strategy.inline.algorithm-expression=sharding$->{Math.abs(res_id.hashCode())%2}
spring.shardingsphere.rules.sharding.tables.sns_resource_essay.database-strategy.inline.sharding-column=res_id
spring.shardingsphere.rules.sharding.tables.sns_resource_essay.database-strategy.inline.algorithm-expression=sharding$->{Math.abs(res_id.hashCode())%2}
spring.shardingsphere.rules.sharding.tables.sns_resource_cache.database-strategy.inline.sharding-column=res_id
spring.shardingsphere.rules.sharding.tables.sns_resource_cache.database-strategy.inline.algorithm-expression=sharding$->{Math.abs(res_id.hashCode())%2}`
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org