1.背景

在订单、流水等场景下,与时间段关联最紧密的大数据业务,单表很容易就超过了千万级别,这时MySQL的性能大打折扣,利用ShardingSphere我们可以完成按照时间分库分表,从而降低单表的数据量。

2.实现

其他代码参考前一篇文章

  1. spring:
  2. shardingsphere:
  3. datasource:
  4. names: ds1
  5. ds1:
  6. type: com.alibaba.druid.pool.DruidDataSource
  7. driver-class-name: com.mysql.cj.jdbc.Driver
  8. url: jdbc:mysql://localhost:3306/ds1?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
  9. username: root
  10. password: 123456
  11. sharding:
  12. tables:
  13. sensor_xmz:
  14. actualDataNodes: ds$->{2021...2025}.sensor_xmz_$->{1..4}
  15. # 分库策略
  16. databaseStrategy:
  17. standard:
  18. shardingColumn: create_time
  19. preciseAlgorithmClassName: com.zym.config.PreciseModuloDatabaseShardingAlgorithm
  20. # 分表策略
  21. tableStrategy:
  22. standard:
  23. shardingColumn: create_time
  24. preciseAlgorithmClassName: com.zym.config.PreciseModuloTableShardingAlgorithm
  25. props:
  26. sql:
  27. show: true
  28. main:
  29. allow-bean-definition-overriding: true

精确分片,则需要自己实现分片类

PreciseModuloDatabaseShardingAlgorithm.java

  1. package com.zym.config;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
  4. import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
  5. import java.util.Collection;
  6. public class PreciseModuloDatabaseShardingAlgorithm implements PreciseShardingAlgorithm<String> {
  7. @Override
  8. public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {
  9. //配置的分库分片的sharding-column对应的值,也就是具体时间
  10. String str=preciseShardingValue.getValue();
  11. if (str.isEmpty()) {
  12. throw new UnsupportedOperationException("分库字段为空");
  13. }
  14. //得到具体年,截取字符串要头不要尾
  15. String value= StringUtils.substring(str,0,4);
  16. for (String each:collection) {
  17. if(each.endsWith(value)){
  18. return each;
  19. }
  20. }
  21. return null;
  22. }
  23. }

PreciseModuloTableShardingAlgorithm.java

  1. package com.zym.config;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
  4. import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
  5. import java.util.Collection;
  6. public class PreciseModuloTableShardingAlgorithm implements PreciseShardingAlgorithm<String> {
  7. @Override
  8. public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {
  9. //配置的分片的sharding-column对应的值
  10. String timeValue = preciseShardingValue.getValue();
  11. //判断timeValue是否为空
  12. if(StringUtils.isBlank(timeValue)){
  13. throw new UnsupportedOperationException("分表字段为空");
  14. }
  15. for (String each:collection) {
  16. //得到具体月,截取字符串要头不要尾
  17. String mouthValue= StringUtils.substring(timeValue,5,7);
  18. int mouth = Integer.parseInt(mouthValue);
  19. String suffix = null;
  20. if (mouth >= 1 && mouth <= 3){
  21. suffix = "1";
  22. }else if (mouth >= 4 && mouth <= 6){
  23. suffix = "2";
  24. }else if (mouth >= 7 && mouth <= 9){
  25. suffix = "3";
  26. }else if (mouth >= 10 && mouth <= 12){
  27. suffix = "4";
  28. }
  29. //循环每个库,看哪个库与当前条件匹配
  30. if(each.endsWith(suffix)){
  31. return each;
  32. }
  33. }
  34. return null;
  35. }
  36. }

这样就实现了精确分片(按照时间分片)~