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/08/03 09:00:19 UTC

[GitHub] [shardingsphere] 291277058 commented on issue #6551: Question:update sql executed in all of the table

291277058 commented on issue #6551:
URL: https://github.com/apache/shardingsphere/issues/6551#issuecomment-667901195


   This is my config.
   `
   #---------------------------- 数据库分片配置
   spring.shardingsphere.sharding.tables.order_item.actual-data-nodes=ds0.order_item20207,ds0.order_item20211,ds0.order_item20217,ds0.order_item20221,ds0.order_item20227
   # 配置分表策略
   spring.shardingsphere.sharding.tables.order_item.table-strategy.complex.sharding-columns=create_date,ordernoComplexShardingConfig
   spring.shardingsphere.sharding.tables.order_item.table-strategy.complex.algorithm-class-name=com.sino.gameplus.sdkserver.payment.config.ComplexShardingConfig
   `
   And my shardingAlgorithm
   `
   package com.sino.gameplus.sdkserver.payment.config;
   
   import lombok.extern.slf4j.Slf4j;
   import org.apache.shardingsphere.api.sharding.complex.ComplexKeysShardingAlgorithm;
   import org.apache.shardingsphere.api.sharding.complex.ComplexKeysShardingValue;
   import org.springframework.stereotype.Component;
   import org.springframework.util.ObjectUtils;
   
   import java.sql.Timestamp;
   import java.time.LocalDateTime;
   import java.time.format.DateTimeFormatter;
   import java.time.temporal.ChronoField;
   import java.time.temporal.TemporalAccessor;
   import java.util.*;
   
   @Slf4j
   @Component
   public class ComplexShardingConfig implements ComplexKeysShardingAlgorithm<String> {
   
       private DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMM");
   
       @Override
       public Collection<String> doSharding(Collection collection, ComplexKeysShardingValue complexKeysShardingValue) {
           // TODO: 2020/7/27 需要理清complex的应用组合不断完善该方法;判断value为多个值时为什么情况,>< >= 等等
           String logicTableName = complexKeysShardingValue.getLogicTableName();
           Map<String, LinkedList> map = complexKeysShardingValue.getColumnNameAndShardingValuesMap();
           LinkedList<String> orderno = map.get("orderno");
           LinkedList<Timestamp> createDate = map.get("create_date");
   
           Set<String> result = new HashSet<String>() {
           };
   
           if (!ObjectUtils.isEmpty(orderno)) {
               for (String order : orderno) {
                   TemporalAccessor parse = dateTimeFormatter.parse(order.substring(0, 6));
                   String tableName = getTableName(logicTableName, parse.get(ChronoField.YEAR), parse.get(ChronoField.MONTH_OF_YEAR));
                   if (collection.contains(tableName))
                       result.add(tableName);
               }
           } else if (!ObjectUtils.isEmpty(createDate)) {
               for (Timestamp dateTime : createDate) {
                   LocalDateTime localDateTime = dateTime.toLocalDateTime();
                   String tableName = getTableName(logicTableName, localDateTime.getYear(), localDateTime.getYear());
                   if (collection.contains(tableName))
                       result.add(tableName);
               }
           } else {
               LocalDateTime now = LocalDateTime.now();
               String tableName = getTableName(logicTableName, now.getYear(), now.getMonthValue());
               result.add(tableName);
           }
   
           if (ObjectUtils.isEmpty(result))
               log.error("table not found or table list is null");
   
           return result;
       }
   
       private String getTableName(String logicTableName, int year, int month) {
   
           String tableMonth = "";
           if (month <= 5)
               tableMonth = "1";
           else
               tableMonth = "7";
           return logicTableName + year + tableMonth;
       }
   }
   
   `


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