Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
<version>4.13.2</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.TimeZone;

/**
* 先根据日期分组,再根据时间hash使得短期内数据分布的更均匀
* 优点可以避免扩容时的数据迁移,又可以一定程度上避免范围分片的热点问题
Expand All @@ -29,6 +31,7 @@ public class PartitionByRangeDateHash extends AbstractPartitionAlgorithm impleme
private long partionTime;

private static final long oneDay = 86400000;
private static final TimeZone UTC = TimeZone.getTimeZone("UTC");

private String groupPartionSize;
private int intGroupPartionSize;
Expand All @@ -40,13 +43,16 @@ public void init()
{
try
{
beginDate = new SimpleDateFormat(dateFormat).parse(sBeginDate)
.getTime();
SimpleDateFormat sdfInit = new SimpleDateFormat(dateFormat);
sdfInit.setTimeZone(UTC);
beginDate = sdfInit.parse(sBeginDate).getTime();
intGroupPartionSize = Integer.parseInt(groupPartionSize);
formatter = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(dateFormat);
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
sdf.setTimeZone(UTC);
return sdf;
}
};
if (intGroupPartionSize <= 0)
Expand Down Expand Up @@ -80,8 +86,7 @@ public Integer calculateStart(String columnValue)
{
try
{
long targetTime = new SimpleDateFormat(dateFormat).parse(
columnValue).getTime();
long targetTime = formatter.get().parse(columnValue).getTime();
int targetPartition = (int) ((targetTime - beginDate) / partionTime);
return targetPartition * intGroupPartionSize;

Expand All @@ -96,8 +101,7 @@ public Integer calculateEnd(String columnValue)
{
try
{
long targetTime = new SimpleDateFormat(dateFormat).parse(
columnValue).getTime();
long targetTime = formatter.get().parse(columnValue).getTime();
int targetPartition = (int) ((targetTime - beginDate) / partionTime);
return (targetPartition+1) * intGroupPartionSize - 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ public void test() throws ParseException {
partition.init();

Integer calculate = partition.calculate("2014-01-01 00:00:00");
Assert.assertEquals(true, 3 == calculate);
Assert.assertEquals(true, 1 == calculate);

calculate = partition.calculate("2014-01-01 00:00:01");
Assert.assertEquals(true, 1 == calculate);
Assert.assertEquals(true, 0 == calculate);

calculate = partition.calculate("2014-01-04 00:00:00");
Assert.assertEquals(true, 7 == calculate);
Assert.assertEquals(true, 6 == calculate);

calculate = partition.calculate("2014-01-04 00:00:01");
Assert.assertEquals(true, 11== calculate);
Assert.assertEquals(true, 10 == calculate);


Date beginDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-01-01 00:00:00");
Expand Down