Browse Source

策略修改及调试

yufeng 4 years ago
parent
commit
e479e17e18

+ 1 - 1
src/main/java/com/huojutech/sharding/algorithm/DatabaseShardingAlgorithm.java

@@ -5,7 +5,7 @@ import java.util.Collection;
5 5
 import io.shardingsphere.core.api.algorithm.sharding.ShardingValue;
6 6
 import io.shardingsphere.core.api.algorithm.sharding.complex.ComplexKeysShardingAlgorithm;
7 7
 
8
-public class DatabaseShardingAlgorithm implements ComplexKeysShardingAlgorithm {
8
+public class ComplexDatabaseShardingAlgorithm implements ComplexKeysShardingAlgorithm {
9 9
 
10 10
     @Override
11 11
     public Collection<String> doSharding(Collection<String> availableTargetNames, Collection<ShardingValue> shardingValues) {

+ 1 - 1
src/main/java/com/huojutech/sharding/algorithm/MyPreciseShardingAlgorithm.java

@@ -13,7 +13,7 @@ import io.shardingsphere.core.api.algorithm.sharding.ShardingValue;
13 13
 import io.shardingsphere.core.api.algorithm.sharding.complex.ComplexKeysShardingAlgorithm;
14 14
 
15 15
 @Slf4j
16
-public class MyPreciseShardingAlgorithm implements ComplexKeysShardingAlgorithm {
16
+public class ComplexTableShardingAlgorithm implements ComplexKeysShardingAlgorithm {
17 17
     
18 18
     @Override
19 19
     public Collection<String> doSharding(Collection<String> availableTargetNames,

+ 19 - 0
src/main/java/com/huojutech/sharding/algorithm/PreciseDatabaseShardingAlgorithm.java

@@ -0,0 +1,19 @@
1
+package com.huojutech.sharding.algorithm;
2
+
3
+import java.util.Collection;
4
+
5
+import io.shardingsphere.core.api.algorithm.sharding.PreciseShardingValue;
6
+import io.shardingsphere.core.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
7
+
8
+public class PreciseDatabaseShardingAlgorithm implements PreciseShardingAlgorithm<String> {
9
+
10
+    @Override
11
+    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> shardingValue) {
12
+        String tableName = null;
13
+        for (String string : availableTargetNames) {
14
+            tableName = string;
15
+        }
16
+        return tableName;
17
+    }
18
+
19
+}

+ 33 - 0
src/main/java/com/huojutech/sharding/algorithm/PreciseTableShardingAlgorithm.java

@@ -0,0 +1,33 @@
1
+package com.huojutech.sharding.algorithm;
2
+
3
+import java.sql.Timestamp;
4
+import java.util.ArrayList;
5
+import java.util.Calendar;
6
+import java.util.Collection;
7
+import java.util.Iterator;
8
+import java.util.List;
9
+
10
+import groovy.util.logging.Slf4j;
11
+import io.shardingsphere.core.api.algorithm.sharding.ListShardingValue;
12
+import io.shardingsphere.core.api.algorithm.sharding.PreciseShardingValue;
13
+import io.shardingsphere.core.api.algorithm.sharding.ShardingValue;
14
+import io.shardingsphere.core.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
15
+
16
+@Slf4j
17
+public class PreciseTableShardingAlgorithm implements PreciseShardingAlgorithm<String> {
18
+
19
+    @Override
20
+    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> shardingValue) {
21
+        long code = Long.parseLong((shardingValue.getValue()).substring(16, 20));
22
+        long year = Long.parseLong((shardingValue.getValue()).substring(0, 4));
23
+        String suffix = year + "_" + (code&0x001);
24
+        
25
+        for (String tableName : availableTargetNames) {
26
+            if (tableName.endsWith(suffix)) {
27
+                return tableName;
28
+            }
29
+        }
30
+        return null;
31
+    }
32
+
33
+}

+ 88 - 0
src/main/java/com/huojutech/sharding/algorithm/RangeTableShardingAlgorithm.java

@@ -0,0 +1,88 @@
1
+package com.huojutech.sharding.algorithm;
2
+
3
+import java.sql.Timestamp;
4
+import java.util.ArrayList;
5
+import java.util.Calendar;
6
+import java.util.Collection;
7
+import java.util.Iterator;
8
+import java.util.List;
9
+
10
+import groovy.util.logging.Slf4j;
11
+import io.shardingsphere.core.api.algorithm.sharding.ListShardingValue;
12
+import io.shardingsphere.core.api.algorithm.sharding.PreciseShardingValue;
13
+import io.shardingsphere.core.api.algorithm.sharding.RangeShardingValue;
14
+import io.shardingsphere.core.api.algorithm.sharding.ShardingValue;
15
+import io.shardingsphere.core.api.algorithm.sharding.standard.RangeShardingAlgorithm;
16
+
17
+@Slf4j
18
+public class RangeTableShardingAlgorithm implements RangeShardingAlgorithm<String> {
19
+    
20
+    
21
+    public Collection<String> doSharding(Collection<String> availableTargetNames,
22
+            Collection<ShardingValue> shardingValues) {
23
+        
24
+        Collection<Object> orderCodeValues = getShardingValue(shardingValues, "order_code");
25
+        Collection<Object> dateValues = getShardingValue(shardingValues, "gmt_create");
26
+        List<String> shardingSuffix = new ArrayList<>();
27
+        
28
+        /**例如:根据user_id + order_id 双分片键来进行分表*/
29
+        //Set<List<Integer>> valueResult = Sets.cartesianProduct(userIdValues, orderIdValues);
30
+        for (Object orderCode : orderCodeValues) {
31
+            long code = Long.parseLong(((String)orderCode).substring(16, 20));
32
+            
33
+            for (Object date : dateValues) {                
34
+                Timestamp timestamp = (Timestamp)date;
35
+                Calendar calendar = Calendar.getInstance();
36
+                calendar.setTimeInMillis(timestamp.getTime());
37
+                int year = calendar.get(Calendar.YEAR);
38
+                String suffix = year + "_" + (code&0x001);
39
+                
40
+                availableTargetNames.forEach(x -> {
41
+                    if (x.endsWith(suffix)) {
42
+                        shardingSuffix.add(x);
43
+                    }
44
+                });
45
+            }
46
+        }
47
+                
48
+        return shardingSuffix;
49
+    }
50
+
51
+    private Collection<Object> getShardingValue(Collection<ShardingValue> shardingValues, final String key) {
52
+        Collection<Object> valueSet = new ArrayList<>();
53
+        Iterator<ShardingValue> iterator = shardingValues.iterator();
54
+        while (iterator.hasNext()) {
55
+            ShardingValue next = iterator.next();
56
+            if (next instanceof ListShardingValue) {
57
+                ListShardingValue value = (ListShardingValue) next;
58
+                /**例如:根据user_id + order_id 双分片键来进行分表*/
59
+                if (value.getColumnName().equals(key)) {
60
+                    return value.getValues();
61
+                }
62
+            }
63
+        }
64
+        return valueSet;
65
+    }
66
+
67
+    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> shardingValue) {
68
+        long code = Long.parseLong((shardingValue.getValue()).substring(16, 20));
69
+        long year = Long.parseLong((shardingValue.getValue()).substring(0, 4));
70
+        String suffix = year + "_" + (code&0x001);
71
+        
72
+        for (String tableName : availableTargetNames) {
73
+            if (tableName.endsWith(suffix)) {
74
+                return tableName;
75
+            }
76
+        }
77
+        return null;
78
+    }
79
+
80
+    @Override
81
+    public Collection<String> doSharding(Collection<String> availableTargetNames,
82
+            RangeShardingValue<String> shardingValue) {
83
+        
84
+        
85
+        return null;
86
+    }
87
+
88
+}

+ 2 - 0
src/main/java/com/huojutech/sharding/repository/OrderRepository.java

@@ -37,4 +37,6 @@ public interface OrderRepository {
37 37
     void dropTable();
38 38
     
39 39
     List<Order> queryByGmtCreate();
40
+    
41
+    Order selectById();
40 42
 }

+ 1 - 1
src/main/java/com/huojutech/sharding/service/DemoService.java

@@ -65,7 +65,7 @@ public class DemoService {
65 65
 //            orderItemRepository.insert(item);
66 66
         }
67 67
         System.out.println(orderRepository.queryByGmtCreate());
68
-        System.out.println("2.Delete--------------");
68
+        System.out.println(orderRepository.selectById());
69 69
 //        for (Long each : orderIds) {
70 70
 //            orderRepository.delete(each);
71 71
 //            orderItemRepository.delete(each);

+ 4 - 0
src/main/resources/META-INF/mappers/OrderMapper.xml

@@ -41,4 +41,8 @@
41 41
     <select id="queryByGmtCreate" resultMap="baseResultMap">
42 42
         SELECT * FROM t_order where gmt_create > '2018-09-12 00:00:00'
43 43
     </select>
44
+    
45
+    <select id="selectById" resultMap="baseResultMap">
46
+        SELECT * FROM t_order where order_code = '20180330487921993657' limit 1
47
+    </select>
44 48
 </mapper>

+ 8 - 8
src/main/resources/application-sharding.properties

@@ -11,24 +11,24 @@ sharding.jdbc.datasource.ds_0.password=123456
11 11
 #sharding.jdbc.config.sharding.default-database-strategy.standard.algorithm-class-name=io.shardingsphere.example.spring.boot.mybatis.algorithm.DatabaseShardingAlgorithm
12 12
 #sharding.jdbc.config.sharding.default-key-generator-class-name= io.shardingsphere.example.spring.boot.mybatis.service.IDGenerator
13 13
 
14
-sharding.jdbc.config.sharding.tables.t_order.database-strategy.complex.sharding-columns=order_code,gmt_create
15
-sharding.jdbc.config.sharding.tables.t_order.database-strategy.complex.algorithm-class-name=com.huojutech.sharding.algorithm.DatabaseShardingAlgorithm
14
+sharding.jdbc.config.sharding.tables.t_order.database-strategy.standard.sharding-column=order_code
15
+sharding.jdbc.config.sharding.tables.t_order.database-strategy.standard.precise-algorithm-class-name=com.huojutech.sharding.algorithm.PreciseDatabaseShardingAlgorithm
16 16
 sharding.jdbc.config.sharding.tables.t_order.actual-data-nodes=ds_0.t_order_$->{2018..2019}_$->{0..1}
17
-sharding.jdbc.config.sharding.tables.t_order.table-strategy.complex.sharding-columns=order_code,gmt_create
17
+sharding.jdbc.config.sharding.tables.t_order.table-strategy.standard.sharding-column=order_code
18 18
 sharding.jdbc.config.sharding.tables.t_order.key-generator-column-name=order_id
19
-sharding.jdbc.config.sharding.tables.t_order.table-strategy.complex.algorithm-class-name=com.huojutech.sharding.algorithm.MyPreciseShardingAlgorithm
19
+sharding.jdbc.config.sharding.tables.t_order.table-strategy.standard.precise-algorithm-class-name=com.huojutech.sharding.algorithm.PreciseTableShardingAlgorithm
20 20
 
21 21
 #sharding.jdbc.config.sharding.tables.t_order_item.actual-data-nodes=ds_$->{0..1}.t_order_item_$->{0..1}
22 22
 #sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.sharding-column=order_id
23 23
 #sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order_item_$->{order_id % 2}
24 24
 #sharding.jdbc.config.sharding.tables.t_order_item.key-generator-column-name=order_item_id
25 25
 
26
-sharding.jdbc.config.sharding.tables.t_order_item.database-strategy.complex.sharding-columns=order_code,gmt_create
27
-sharding.jdbc.config.sharding.tables.t_order_item.database-strategy.complex.algorithm-class-name=com.huojutech.sharding.algorithm.DatabaseShardingAlgorithm
26
+sharding.jdbc.config.sharding.tables.t_order_item.database-strategy.standard.sharding-column=order_code
27
+sharding.jdbc.config.sharding.tables.t_order_item.database-strategy.standard.precise-algorithm-class-name=com.huojutech.sharding.algorithm.PreciseDatabaseShardingAlgorithm
28 28
 sharding.jdbc.config.sharding.tables.t_order_item.actual-data-nodes=ds_0.t_order_item_$->{2018..2019}_$->{0..1}
29
-sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.complex.sharding-columns=order_code,gmt_create
29
+sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.standard.sharding-column=order_code
30 30
 sharding.jdbc.config.sharding.tables.t_order_item.key-generator-column-name=order_item_id
31
-sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.complex.algorithm-class-name=com.huojutech.sharding.algorithm.MyPreciseShardingAlgorithm
31
+sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.standard.precise-algorithm-class-name=com.huojutech.sharding.algorithm.PreciseTableShardingAlgorithm
32 32
 
33 33
 sharding.jdbc.config.sharding.binding-tables[0]= t_order,t_order_item
34 34