fandudu0601 opened a new issue, #21523:
URL: https://github.com/apache/shardingsphere/issues/21523

   ### Which version of ShardingSphere did you use?
   5.0.0
   ### Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?
   ShardingSphere-JDBC
   ### Expected behavior
   execute the sql by using sharding table
   ### Actual behavior
   execute the sql by using logic table
   ### Reason analyze (If you can)
   sql parse
   
   properties
   shardingsphere:
       datasource:
         names: master
         master:
           type: com.alibaba.druid.pool.DruidDataSource
           url: jdbc:oracle:thin:@
           username: root
           password: root
           driver-class-name: oracle.jdbc.OracleDriver
           platform: oracle
       rules:
         # 配置分片规则
         sharding:
           tables:
             cc_client:
               actualDataNodes: master.cc_client$->{2020..2022}
               # 配置分表策略
               tableStrategy:
                 standard:
                   shardingColumn: asset_section
                   shardingAlgorithmName: year-sharding
           shardingAlgorithms:
             year-sharding:
               type: CLASS_BASED
               props:
                 strategy: STANDARD
                 algorithmClassName: com.common.ShardingStandardAlgorithm
       props:
         sql-show: true
   
   
   customized sharding algorithm:
   
   import com.google.common.collect.Range;
   import lombok.extern.slf4j.Slf4j;
   import 
org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
   import 
org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
   import 
org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
   
   import java.util.ArrayList;
   import java.util.Collection;
   
   @Slf4j
   public class ShardingStandardAlgorithm implements 
StandardShardingAlgorithm<String> {
   
   
       @Override
       public Collection<String> doSharding(Collection<String> 
availableTargetNames, RangeShardingValue<String> shardingValue) {
           Range<String> range = shardingValue.getValueRange();
           Collection<String> result = new ArrayList<>();
           for(int i = Integer.parseInt(range.lowerEndpoint().substring(0,4)); 
i<=Integer.parseInt(range.upperEndpoint().substring(0,4)); i++){
               for(String tableName : availableTargetNames){
                   if(tableName.endsWith(String.valueOf(i))){
                       result.add(tableName);
                   }
               }
           }
           return result;
       }
   
       @Override
       public String doSharding(Collection<String> availableTargetNames, 
PreciseShardingValue<String> shardingValue) {
           for(String tableName : availableTargetNames){
               if(tableName.endsWith(shardingValue.getValue().substring(0,4))){
                   return tableName;
               }
           }
           throw new UnsupportedOperationException("不支持的分表" 
+shardingValue.getValue());
       }
   
       @Override
       public void init() {
           log.info("初始化按照年份分表");
       }
   
       @Override
       public String getType() {
           return "YEAR_SHARDING";
       }
   }
   
   
   


-- 
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: 
[email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to