99import java .text .ParseException ;
1010import java .text .SimpleDateFormat ;
1111
12+ import java .util .TimeZone ;
13+
1214/**
1315 * 先根据日期分组,再根据时间hash使得短期内数据分布的更均匀
1416 * 优点可以避免扩容时的数据迁移,又可以一定程度上避免范围分片的热点问题
@@ -29,6 +31,7 @@ public class PartitionByRangeDateHash extends AbstractPartitionAlgorithm impleme
2931 private long partionTime ;
3032
3133 private static final long oneDay = 86400000 ;
34+ private static final TimeZone UTC = TimeZone .getTimeZone ("UTC" );
3235
3336 private String groupPartionSize ;
3437 private int intGroupPartionSize ;
@@ -40,13 +43,16 @@ public void init()
4043 {
4144 try
4245 {
43- beginDate = new SimpleDateFormat (dateFormat ).parse (sBeginDate )
44- .getTime ();
46+ SimpleDateFormat sdfInit = new SimpleDateFormat (dateFormat );
47+ sdfInit .setTimeZone (UTC );
48+ beginDate = sdfInit .parse (sBeginDate ).getTime ();
4549 intGroupPartionSize = Integer .parseInt (groupPartionSize );
4650 formatter = new ThreadLocal <SimpleDateFormat >() {
4751 @ Override
4852 protected SimpleDateFormat initialValue () {
49- return new SimpleDateFormat (dateFormat );
53+ SimpleDateFormat sdf = new SimpleDateFormat (dateFormat );
54+ sdf .setTimeZone (UTC );
55+ return sdf ;
5056 }
5157 };
5258 if (intGroupPartionSize <= 0 )
@@ -80,8 +86,7 @@ public Integer calculateStart(String columnValue)
8086 {
8187 try
8288 {
83- long targetTime = new SimpleDateFormat (dateFormat ).parse (
84- columnValue ).getTime ();
89+ long targetTime = formatter .get ().parse (columnValue ).getTime ();
8590 int targetPartition = (int ) ((targetTime - beginDate ) / partionTime );
8691 return targetPartition * intGroupPartionSize ;
8792
@@ -96,8 +101,7 @@ public Integer calculateEnd(String columnValue)
96101 {
97102 try
98103 {
99- long targetTime = new SimpleDateFormat (dateFormat ).parse (
100- columnValue ).getTime ();
104+ long targetTime = formatter .get ().parse (columnValue ).getTime ();
101105 int targetPartition = (int ) ((targetTime - beginDate ) / partionTime );
102106 return (targetPartition +1 ) * intGroupPartionSize - 1 ;
103107
0 commit comments