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/12/10 08:00:21 UTC

[GitHub] [shardingsphere] mailtous opened a new issue #8562: how to create sharding table strategy Class?

mailtous opened a new issue #8562:
URL: https://github.com/apache/shardingsphere/issues/8562


   ```xml
       <bean id="shardingAlgorithm" class="com.dayi35.ShardingAlgorithm" ><description>shardingAlgorithm</description></bean>
   
       <sharding:standard-strategy id="table_strategy" sharding-column="create_date"
                                    precise-algorithm-ref="com.dayi35.ShardingAlgorithm"
                                    range-algorithm-ref="com.dayi35.ShardingAlgorithm"/>
   
     <!--   <sharding:inline-strategy  id="table_strategy" sharding-column="create_date"
                                 algorithm-expression="dataSource.sharding_$->{2019..2020}"/>-->
   
   
       <sharding:data-source id="shardingDataSource">
           <sharding:sharding-rule data-source-names="dataSource">
               <sharding:table-rules>
                   <sharding:table-rule logic-table="test_sharding"
                                        actual-data-nodes="dataSource.test_sharding_$->{2019..2020}"
                                        table-strategy-ref="table_strategy" />
               </sharding:table-rules>
           </sharding:sharding-rule>
           <sharding:props>
               <prop key="sql.show">true</prop>
           </sharding:props>
       </sharding:data-source>
   ````
   ```java
   public class ShardingAlgorithm implements PreciseShardingAlgorithm, RangeShardingAlgorithm<Integer>,
           FactoryBean<ShardingAlgorithm> {
   
       private ApplicationContext applicationContext;
   
       /**
        * 缓存存在的表
        */
       private List<String> tables;
   
       private final String systemLogHead = "test_sharding_";
   
       private Boolean isLoad = false;
   
       public ShardingAlgorithm() {
       }
   
       public ShardingAlgorithm(ApplicationContext applicationContext) {
           this.applicationContext = applicationContext;
       }
   
       public void init(){
           tables = getAllSystemLogTable();
       }
   
       @Override
       public String doSharding(Collection availableTargetNames, PreciseShardingValue shardingValue) {
           if (!isLoad) {
               isLoad = true;
           }
           String target = shardingValue.getValue().toString();
           String year = target.substring(target.lastIndexOf("_") + 1, target.lastIndexOf("_") + 5);
           if (!tables.contains(systemLogHead + year)) {
             //  DBUtil.createLogTable(year);
               tables.add(year);
           }
           return shardingValue.getLogicTableName() + "_" + year;
       }
   
       @Override
       public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Integer> shardingValue) {
           if (!isLoad) {
               isLoad = true;
           }
           Collection<String> availables = new ArrayList<>();
           Range valueRange = shardingValue.getValueRange();
           for (String target : tables) {
               Integer shardValue = Integer.parseInt(target.substring(target.lastIndexOf("_") + 1, target.lastIndexOf("_") + 5));
               if (valueRange.hasLowerBound()) {
                   String lowerStr = valueRange.lowerEndpoint().toString();
                   Integer start = Integer.parseInt(lowerStr.substring(0, 4));
                   if (start - shardValue > 0) {
                       continue;
                   }
               }
               if (valueRange.hasUpperBound()) {
                   String upperStr = valueRange.upperEndpoint().toString();
                   Integer end = Integer.parseInt(upperStr.substring(0, 4));
                   if (end - shardValue < 0) {
                       continue;
                   }
               }
               availables.add(target);
           }
           return availables;
       }
   
       private List<String> getAllSystemLogTable() {
           List tables = new ArrayList();
           tables.add("test_sharding_2019");
           tables.add("test_sharding_2020");
           return tables;
       }
   
       @Override
       public ShardingAlgorithm getObject() throws Exception {
           return new ShardingAlgorithm();
       }
   
       @Override
       public Class<?> getObjectType() {
           return ShardingAlgorithm.class;
       }
   
       @Override
       public boolean isSingleton() {
           return false;
       }
   }
   ````
   when spring-mvc start will error :
   
   org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'com.xxx.ShardingAlgorithm' is defined


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



[GitHub] [shardingsphere] menghaoranss commented on issue #8562: how to create sharding table strategy Class?

Posted by GitBox <gi...@apache.org>.
menghaoranss commented on issue #8562:
URL: https://github.com/apache/shardingsphere/issues/8562#issuecomment-744214805


   I think @huanghao495430759 's reply can solve your question,i will close this issue, thanks.


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



[GitHub] [shardingsphere] menghaoranss closed issue #8562: how to create sharding table strategy Class?

Posted by GitBox <gi...@apache.org>.
menghaoranss closed issue #8562:
URL: https://github.com/apache/shardingsphere/issues/8562


   


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



[GitHub] [shardingsphere] huanghao495430759 commented on issue #8562: how to create sharding table strategy Class?

Posted by GitBox <gi...@apache.org>.
huanghao495430759 commented on issue #8562:
URL: https://github.com/apache/shardingsphere/issues/8562#issuecomment-742374662


      it is bean reference,not bean class name.
   `<sharding:standard-strategy id="table_strategy" sharding-column="create_date"
                                    precise-algorithm-ref="shardingAlgorithm"
                                    range-algorithm-ref="shardingAlgorithm"/>`


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