complex复杂算法

complex复杂算法

  • ComplexKeysShardingAlgorithm

ComplexKeysShardingAlgorithm

支持多个分片键 id,user_id
支持按范围查询 id>5 and id<10
支持每个分片键里有多个值

import org.apache.shardingsphere.api.sharding.complex.ComplexKeysShardingValue;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class ComplexDbAlgorithm implements ComplexKeysShardingAlgorithm {
    @Override
    public Collection doSharding(Collection availableTargetNames, ComplexKeysShardingValue shardingValue) {
        Map columnNameAndShardingValuesMap = shardingValue.getColumnNameAndShardingValuesMap();
        System.out.println("所有的sharding列的值为"+columnNameAndShardingValuesMap);
        for (Map.Entry entry: columnNameAndShardingValuesMap.entrySet()) {
            String shardColumn = entry.getKey();
            Object shardValue = entry.getValue();
        }
        Set result = new HashSet<>();
        Collection idColl = (Collection)columnNameAndShardingValuesMap.get("id");
        for (Object idObj: idColl) {
            if (idObj instanceof Integer || idObj instanceof Long) {
                long idTemp = Long.parseLong(idObj.toString());
                long target = idTemp % 2;
                for (Object targetNames:availableTargetNames) {
                    if (targetNames.toString().endsWith(target + "")) {
                        result.add(targetNames.toString());
                    }
                }
            }
        }
        System.out.println("complex算法result="+result);
        return result;

    }
}

你可能感兴趣的:(complex复杂算法)