CheerwayRen commented on issue #8038:
URL: https://github.com/apache/shardingsphere/issues/8038#issuecomment-856397676
The query statement is the same as before,
------------------------------------------------
@Bean
@Primary
public DataSource multipleDataSource(@Qualifier("base") DataSource base,
@Qualifier("aliyun") DataSource aliyun) throws SQLException {
Map<String, DataSource> targetDataSources = new HashMap<>(2);
targetDataSources.put(DataSourceEnum.BASE.getValue(), base);
targetDataSources.put(DataSourceEnum.ALIYUN.getValue(), aliyun);
// 配置 day_report 表规则
ShardingTableRuleConfiguration dayReportRuleConfiguration = new
ShardingTableRuleConfiguration("mng_sale_amount_day",
"base.mng_sale_amount_day_$->{2011..2030}0$->{1..9},base.mng_sale_amount_day_$->{2011..2030}1$->{0..2},aliyun.mng_sale_amount_day_$->{2011..2030}0$->{1..9},aliyun.mng_sale_amount_day_$->{2011..2030}1$->{0..2}");
// 配置分表规则 第一个精确,第二个范围
dayReportRuleConfiguration.setTableShardingStrategy(new
StandardShardingStrategyConfiguration("SALE_YMD",
"dayReportTableShardingAlgorithm"));
// Sharding全局配置
//多表的话,就配置多个就好了
ShardingRuleConfiguration shardingRuleConfiguration = new
ShardingRuleConfiguration();
shardingRuleConfiguration.getTables().add(dayReportRuleConfiguration);
//分库规则配置
shardingRuleConfiguration.getShardingAlgorithms().put("dayReportTableShardingAlgorithm",
new ShardingSphereAlgorithmConfiguration("dayReportRule", new Properties()));
// 创建数据源
DataSource dataSource =
ShardingSphereDataSourceFactory.createDataSource(targetDataSources,
Collections.singleton(shardingRuleConfiguration), new Properties());
return dataSource;
}
------------------------------------------------------------------------------
@Slf4j
public class DayReportPreciseShardingAlgorithm implements
StandardShardingAlgorithm<String> {
private static String month_format = "yyyyMM";
private Properties props = new Properties();
//精准匹配 in 和 =
@Override
public String doSharding(Collection<String> tableNames,
PreciseShardingValue<String> shardingValue) {
Date reportDay = DateUtil.parse(shardingValue.getValue(),
"yyyyMMdd");
String reportMonth = DateUtil.format(reportDay, month_format);
return "mng_sale_amount_day_" + reportMonth;
}
//范围匹配 between
@Override
public Collection<String> doSharding(Collection<String> collection,
RangeShardingValue<String> rangeShardingValue) {
Range<String> reportDayRange = rangeShardingValue.getValueRange();
//最小值
String lowerDate = reportDayRange.lowerEndpoint();
if (StringUtils.isEmpty(lowerDate)) {
//数据从2011年开始
lowerDate = "201101";
} else {
lowerDate = lowerDate.substring(0, 6);
}
//最大值
String upperDate = reportDayRange.upperEndpoint();
if (StringUtils.isEmpty(upperDate)) {
//数据到当前时间结束
upperDate = DateUtil.format(new Date(), month_format);
} else {
upperDate = upperDate.substring(0, 6);
}
return getMonthTableEnums("mng_sale_amount_day_",
DateUtil.parse(lowerDate, month_format), DateUtil.parse(upperDate,
month_format));
}
private List<String> getMonthTableEnums(String table, DateTime start,
DateTime end) {
List<String> list = new ArrayList<>();
while (!start.isAfter(end)) {
list.add(table + DateUtil.format(start, month_format));
start.offset(DateField.MONTH, 1);
}
return list;
}
@Override
public void init() {
log.info("init day report rule");
}
@Override
public String getType() {
return "dayReportRule";
}
@Override
public Properties getProps() {
return props;
}
@Override
public void setProps(Properties props) {
this.props = props;
}
}
-------------------------------------------------------------
Do you need any more information?
--
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:
[email protected]