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 2022/03/23 13:43:45 UTC

[GitHub] [shardingsphere] CheerwayRen commented on issue #8638: Bug for USE JAVA API to to configure two tables

CheerwayRen commented on issue #8638:
URL: https://github.com/apache/shardingsphere/issues/8638#issuecomment-1076391694


   Hello, I also encounter this problem now. When I use one table to divide the table, it can work normally. When I add another table, the rule of dividing the table will become invalid. The version I use is  5.0.0-beta
   Here is my java configuration
   
   `@Bean
       public DynamicDataSourceProvider dynamicDataSourceProvider() {
           Map<String, DataSourceProperty> datasourceMap = properties.getDatasource();
           return new AbstractDataSourceProvider() {
               @SneakyThrows
               @Override
               public Map<String, DataSource> loadDataSources() {
                   Map<String, DataSource> dataSourceMap = createDataSourceMap(datasourceMap);
                   // 拿到两个数据源,对sharding进行改造
                   DataSource sharding = dataSourceMap.get(SHARDING_DATA_SOURCE_NAME);
                   Map<String, DataSource> targetDataSources = new HashMap<>(1);
                   // 这里的key一定要保持和ShardingTableRuleConfiguration中actualDataNodes中前缀一致
                   targetDataSources.put(SHARDING_DATA_SOURCE_NAME, sharding);
                   // 考虑到备份表的机制 每次扫描备份表还有最近五年的分表
                   Date now = new Date();
                   String shardingStart = DateUtils.format(DateUtils.beginOfYear(DateUtils.offsetYear(now, 0 - SalesPropertiesConstant.SALE_MODE_DEFALUT_TABLE_BACKUP_YEAR)), SalesPropertiesConstant.SALE_YM_FORMAT);
                   String shardingEnd = DateUtils.format(DateUtils.endOfYear(DateUtils.offsetYear(now, 1)), SalesPropertiesConstant.SALE_YM_FORMAT);
                   System.setProperty("backupEnd", shardingStart);
                   // 配置 day_report 表规则 实际表为过去五年加未来一年 + 备份表
                   ShardingRuleConfiguration shardingRuleConfiguration = new ShardingRuleConfiguration();
                   //day table sharding rule
                   String dayReportActualDataNodes = "sharding.mng_sale_amount_day_$->{" + shardingStart.substring(SpecialCharConstants.FINAL_INT_ZERO, SpecialCharConstants.FINAL_INT_FOUR) + ".." + shardingEnd.substring(SpecialCharConstants.FINAL_INT_ZERO, SpecialCharConstants.FINAL_INT_FOUR) + "}0$->{1..9},sharding.mng_sale_amount_day_$->{" + shardingStart.substring(SpecialCharConstants.FINAL_INT_ZERO, SpecialCharConstants.FINAL_INT_FOUR) + ".." + shardingEnd.substring(SpecialCharConstants.FINAL_INT_ZERO, SpecialCharConstants.FINAL_INT_FOUR) + "}1$->{0..2},sharding.mng_sale_amount_day_backup";
                   ShardingTableRuleConfiguration dayReportRuleConfiguration = new ShardingTableRuleConfiguration(SalesPropertiesConstant.SALE_DAY_REPORT_TABLE_NAME, dayReportActualDataNodes);
                   StandardShardingStrategyConfiguration standardShardingStrategyConfiguration=  new StandardShardingStrategyConfiguration("SALE_YMD", "dayReportTableShardingAlgorithm");
                   dayReportRuleConfiguration.setTableShardingStrategy(standardShardingStrategyConfiguration);
                   shardingRuleConfiguration.getTables().add(dayReportRuleConfiguration);
                   shardingRuleConfiguration.getShardingAlgorithms().put("dayReportTableShardingAlgorithm", new ShardingSphereAlgorithmConfiguration("dayReportRule", new Properties()));
   
                   //brand table sharding rule
                   String brandDailyActualDataNodes = "sharding.mng_sale_brand_daily_data_$->{" + shardingStart.substring(SpecialCharConstants.FINAL_INT_ZERO, SpecialCharConstants.FINAL_INT_FOUR) + ".." + shardingEnd.substring(SpecialCharConstants.FINAL_INT_ZERO, SpecialCharConstants.FINAL_INT_FOUR) + "},sharding.mng_sale_brand_daily_data_backup";
                   ShardingTableRuleConfiguration brandDailyRuleConfiguration = new ShardingTableRuleConfiguration(SalesPropertiesConstant.SALE_BRAND_DAILY_TABLE_NAME, brandDailyActualDataNodes);
                   brandDailyRuleConfiguration.setTableShardingStrategy(new StandardShardingStrategyConfiguration("SALE_YMD", "brandDailyPreciseShardingAlgorithm"));
                   shardingRuleConfiguration.getTables().add(brandDailyRuleConfiguration);
                   shardingRuleConfiguration.getShardingAlgorithms().put("brandDailyPreciseShardingAlgorithm", new ShardingSphereAlgorithmConfiguration("brandDailyRule", new Properties()));
                   shardingRuleConfiguration.setDefaultTableShardingStrategy(standardShardingStrategyConfiguration);
                   Properties prop=  new Properties();
                   prop.setProperty("sql-show","true");
   
   
                   DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource(targetDataSources, Collections.singleton(shardingRuleConfiguration), prop);
                   dataSourceMap.put(SHARDING_DATA_SOURCE_NAME, dataSource);
                   return dataSourceMap;
               }
           };
       }`
   
   brand table sharding rule  is my new addition then is do,Then the rules didn't take effect. Please see what's wrong with my configuration
   
   @huanghao495430759 @menghaoranss 
   
   
   


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org