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/11/25 10:25:43 UTC

[GitHub] [shardingsphere] haoqg edited a comment on issue #8309: how to congurate self implement sharding algorithm?

haoqg edited a comment on issue #8309:
URL: https://github.com/apache/shardingsphere/issues/8309#issuecomment-733615552


   5.0 是用的SPI机制,假如需要使用自定义分库或者分表策略的话,以官方的mod为例子,我们需要这么干,首先
   在项目里的resources 目录下新建一个文件夹  META-INF/services,文件夹里新建一个文件org.apache.shardingsphere.sharding.spi.ShardingAlgorithm
   ![image](https://user-images.githubusercontent.com/9582571/100214534-fb971500-2f4a-11eb-9a29-64990c165bed.png)
   在此文件里把自己的类加上
   比如,我这里新建一个类,
   
   
   `public final class BatchIdShardingAlgorithm implements StandardShardingAlgorithm<Comparable<?>>, ShardingAutoTableAlgorithm {
       private static final String SHARDING_COUNT_KEY = "sharding-count";
       private Properties props = new Properties();
       private int shardingCount;
   
       public BatchIdShardingAlgorithm() {
       }
   
       @Override
       public void init() {
           this.shardingCount = this.getShardingCount();
       }
   
       private int getShardingCount() {
           return 0;
       }
   
       @Override
       public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Comparable<?>> shardingValue) {
   
        //todo 自己的分表逻辑
           return "0";
       }
   
       @Override
       public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Comparable<?>> shardingValue) {
           return this.isContainAllTargets(shardingValue) ? availableTargetNames : this.getAvailableTargetNames(availableTargetNames, shardingValue);
       }
   
       private boolean isContainAllTargets(RangeShardingValue<Comparable<?>> shardingValue) {
           return !shardingValue.getValueRange().hasUpperBound() || shardingValue.getValueRange().hasLowerBound() && this.getLongValue(shardingValue.getValueRange().upperEndpoint()) - this.getLongValue(shardingValue.getValueRange().lowerEndpoint()) >= (long)(this.shardingCount - 1);
       }
   
       private Collection<String> getAvailableTargetNames(Collection<String> availableTargetNames, RangeShardingValue<Comparable<?>> shardingValue) {
           Collection<String> result = new LinkedHashSet(availableTargetNames.size());
   
           for(long i = this.getLongValue(shardingValue.getValueRange().lowerEndpoint()); i <= this.getLongValue(shardingValue.getValueRange().upperEndpoint()); ++i) {
               Iterator var6 = availableTargetNames.iterator();
   
               while(var6.hasNext()) {
                   String each = (String)var6.next();
                   if (each.endsWith(String.valueOf(i % (long)this.shardingCount))) {
                       result.add(each);
                   }
               }
           }
   
           return result;
       }
   
       private long getLongValue(Comparable<?> value) {
           return Long.parseLong(value.toString());
       }
   
       @Override
       public int getAutoTablesAmount() {
           return this.shardingCount;
       }
   
       @Override
       public String getType() {
           return "BATCH-ID";
       }
   
       @Override
       public Collection<String> getAllPropertyKeys() {
           return Collections.singletonList("sharding-count");
       }
   
       @Override
       @Generated
       public Properties getProps() {
           return this.props;
       }
   
       @Override
       @Generated
       public void setProps(Properties props) {
           this.props = props;
       }
   
       @Generated
       public void setShardingCount(int shardingCount) {
           this.shardingCount = shardingCount;
       }
   }`
   
   然后就可以使用自定义的策略了
   
   Properties tableShardingAlgorithmrProps = new Properties();
           shardingRuleConfig.getShardingAlgorithms().put("tableShardingAlgorithm", new ShardingSphereAlgorithmConfiguration("BATCH-ID", tableShardingAlgorithmrProps));
   
   大概就是这么个机制,
   其实源码里就是这么干的,有图为证
   ![image](https://user-images.githubusercontent.com/9582571/100214851-516bbd00-2f4b-11eb-9389-c25b770b6a15.png)
   


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