1. sharding sphere 4.0.0-RC1 version by year table (automatic construction of the table)
1.1 Overview
The article left a pit, sharding sphere itself does not provide automatic function built form, but I thought, we can bypass it set itself, its own data fragmentation is achieved by slicing algorithm, as some of inheritance interfaces PreciseShardingAlgorithm
, RangeShardingAlgorithm
etc., when the scope of the inquiry, we need the original availableTargetNames
to determine the parameters of an existing table, so do not check the table does not exist, insert is the same reason
But in fact we do not need to use availableTargetNames
parameters in the system initialization time on their own to query existing tables and then cached, of course, the course I have stepped on some pit, because LogShardingAlgorithm
the loading process and sequential read my database of poor control, in theory I can connect the database at any time to read, but I need to read the spring loaded configuration environment and then decide which database even after continuous testing arrangements or not, and finally adopted the following way, first call slicing algorithm time to read and cache
After doing this the effect achieved is doing insert data, I determine the date for the 2020 table is in the cache, then there is the table in the database, or I create the table, then the table is added to the cache; and doing range queries, we request easy-range parameters, choose from the cache table, these tables are the existence of such a database you exist 2018, 2019, 2020 table, you query 2017 - 2021, then it will only check 18,19,20 three tables
/**
* @author: laoliangliang
* @description: 日志分片
* @create: 2020/1/2 10:19
**/
@Slf4j
public class LogShardingAlgorithm implements PreciseShardingAlgorithm, RangeShardingAlgorithm<Integer> {
/**
* 缓存存在的表
*/
private List<String> tables;
private final String systemLogHead = "system_log_";
private Boolean isLoad = false;
@Override
public String doSharding(Collection availableTargetNames, PreciseShardingValue shardingValue) {
if (!isLoad) {
tables = DBUtil.getAllSystemLogTable();
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) {
tables = DBUtil.getAllSystemLogTable();
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;
}
}
1.2. Tips
- To read here
system_log_2019
,system_log_2020
the table name simply query the mysqlinformation_schema.TABLES
, such as:
select TABLE_NAME from information_schema.`TABLES`
where TABLE_NAME like 'system_log_%'
Welcome to public concern number, reply "instructional video" progressive learning together