You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by "SDSmalin (via GitHub)" <gi...@apache.org> on 2023/05/22 05:56:11 UTC

[GitHub] [shardingsphere] SDSmalin commented on issue #16725: how to config actual-data-nodes while my actual-data-nodes tables is dynamic。

SDSmalin commented on issue #16725:
URL: https://github.com/apache/shardingsphere/issues/16725#issuecomment-1556578604

   I found a simpler code
   
   ``` yaml
   actual-data-nodes: ds0.$->{com.lingh.LocalShardingDatabasesAndTablesUtil.getActualDataNodes()}
   ```
   
   ``` JAVA
   public class LocalShardingDatabasesAndTablesUtil { 
        public static List<String> getActualDataNodes() { 
            LocalDate startTime = LocalDate.now().minusDays(10); 
            LocalDate endTime = LocalDate.now(); 
            return LongStream.range(0, ChronoUnit.DAYS.between(startTime, endTime)) 
                    .mapToObj(startTime::plusDays) 
                    .map(localDate -> "table_name_" + localDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"))) 
                    .collect(Collectors.toList()); 
        }
   }
   ```
   ``` JAVA
   public class InitActualDataNodesAO {
       @Resource
       private ShardingSphereDataSource shardingSphereDataSource;
       private final String schemaNameForTOrder = "sharding_db";
   
       public void testSharding() {
           this.reloadShardRuleActualDataNodes(shardingSphereDataSource, schemaNameForTOrder);
       }
   
       private void reloadShardRuleActualDataNodes(ShardingSphereDataSource dataSource, String schemaName) {
           // Context manager.
           org.apache.shardingsphere.mode.manager.ContextManager contextManager = dataSource.getContextManager();
   
           // Rule configuration.
           Collection<RuleConfiguration> ruleConfigList = dataSource.getContextManager()
                   .getMetaDataContexts()
                   .getMetaData(schemaName)
                   .getRuleMetaData()
                   .getConfigurations();
           // update context
           contextManager.alterRuleConfiguration(schemaName, ruleConfigList);
       }
   }
   ```
   It's just need to execute `contextManager.alterRuleConfiguration()` to reload `actual-data-nodes`


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