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:21:52 UTC

[GitHub] [shardingsphere] RafaelGuo edited a comment on issue #6948: PreciseShardingAlgorithm does not work

RafaelGuo edited a comment 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=ds-0-1073741823,ds-1073741824-2147483647
   
   spring.shardingsphere.datasource.ds-0-1073741823.type=com.alibaba.druid.pool.DruidDataSource
   spring.shardingsphere.datasource.ds-0-1073741823.driver-class-name=com.mysql.cj.jdbc.Driver
   spring.shardingsphere.datasource.ds-0-1073741823.url=jdbc:mysql://127.0.0.1:3306/sharding0?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
   spring.shardingsphere.datasource.ds-0-1073741823.username=root
   spring.shardingsphere.datasource.ds-0-1073741823.password=123456
   spring.shardingsphere.datasource.ds-0-1073741823.initial-size=5
   spring.shardingsphere.datasource.ds-0-1073741823.max-active=20
   spring.shardingsphere.datasource.ds-0-1073741823.min-idle=5
   spring.shardingsphere.datasource.ds-0-1073741823.max-wait=60000
   
   spring.shardingsphere.datasource.ds-1073741824-2147483647.type=com.alibaba.druid.pool.DruidDataSource
   spring.shardingsphere.datasource.ds-1073741824-2147483647.driver-class-name=com.mysql.cj.jdbc.Driver
   spring.shardingsphere.datasource.ds-1073741824-2147483647.url=jdbc:mysql://127.0.0.1:3306/sharding1?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
   spring.shardingsphere.datasource.ds-1073741824-2147483647.username=root
   spring.shardingsphere.datasource.ds-1073741824-2147483647.password=123456
   spring.shardingsphere.datasource.ds-1073741824-2147483647.initial-size=5
   spring.shardingsphere.datasource.ds-1073741824-2147483647.max-active=20
   spring.shardingsphere.datasource.ds-1073741824-2147483647.min-idle=5
   spring.shardingsphere.datasource.ds-1073741824-2147483647.max-wait=60000
   
   spring.shardingsphere.rules.sharding.tables.sns_resource_basic.actual-data-nodes=ds-0-1073741823.sns_resource_basic,ds-1073741824-2147483647.sns_resource_basic
   spring.shardingsphere.rules.sharding.tables.sns_resource_essay.actual-data-nodes=ds-0-1073741823.sns_resource_essay,ds-1073741824-2147483647.sns_resource_essay
   spring.shardingsphere.rules.sharding.tables.sns_resource_cache.actual-data-nodes=ds-0-1073741823.sns_resource_cache,ds-1073741824-2147483647.sns_resource_cache
   
   spring.shardingsphere.rules.sharding.tables.sns_resource_basic.database-strategy.standard.sharding-column=res_id
   spring.shardingsphere.rules.sharding.tables.sns_resource_basic.database-strategy.standard.precise-algorithm-class-name=com.tdx.sharding.configration.CustomPreciseShardingAlgorithm
   
   spring.shardingsphere.rules.sharding.tables.sns_resource_essay.database-strategy.standard.sharding-column=res_id
   spring.shardingsphere.rules.sharding.tables.sns_resource_essay.database-strategy.standard.precise-algorithm-class-name=com.tdx.sharding.configration.CustomPreciseShardingAlgorithm
   
   spring.shardingsphere.rules.sharding.tables.sns_resource_cache.database-strategy.standard.sharding-column=res_id
   spring.shardingsphere.rules.sharding.tables.sns_resource_cache.database-strategy.standard.precise-algorithm-class-name=com.tdx.sharding.configration.CustomPreciseShardingAlgorithm`


----------------------------------------------------------------
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