Shardingsphere设置实际节点格式actual-data-nodes

15,734 阅读1分钟

Shardingsphere按照年月格式分片设置实际节点格式

通过groovy脚本来设置

  user_order:
          logicTable: user_order
          actual-data-nodes: 
          ds0.user_order_$->{2019..2030}${(1..12).collect{t ->t.toString().padLeft(2,'0')}}
          tableStrategy:
            inline:
              shardingColumn: year_month
              algorithmExpression: user_order_$->{year_month}
#            standard:
#              shardingColumn: exec_month
#              preciseAlgorithmClassName: com.demo.common.config.YearMonthShardingAlgorithm
@Slf4j
public class YearMonthShardingAlgorithm implements PreciseShardingAlgorithm {
    private static final String SPLITTER = "_";

    @Override
    public String doSharding(Collection availableTargetNames, PreciseShardingValue shardingValue) {
        String tbName = StringUtils.joinWith(SPLITTER, shardingValue.getLogicTableName(), shardingValue.getValue());
        if (log.isDebugEnabled()) {
            log.info("Sharding input:{}, output:{}", JSON.toJSONString(shardingValue), tbName);
        }
        return tbName;
    }
}

关联时查询报错

  • 第一种 Sharding value must same with subquery
  • 第二种 Must have one sharding with subquery. 两种错误均在类org.apache.shardingsphere.core.route.router.sharding.ParsingSQLRouter中校验

第一种错误表示关联表的分片键不一致导致

可以通过如下配置解决:

spring:
  shardingsphere:
    props:
      sql.show: true
    sharding:
      binding-tables:
        - user, user_order