Skip to content

Commit 9995f58

Browse files
author
dapeng
committed
test partition function
1 parent aa5e8c5 commit 9995f58

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

kafka-base/kafka-base-sink/src/main/java/com/dtstack/flink/sql/sink/kafka/CustomerFlinkPartition.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,36 @@
33

44
import org.apache.flink.streaming.connectors.kafka.partitioner.FlinkFixedPartitioner;
55

6+
import org.apache.flink.streaming.connectors.kafka.partitioner.FlinkKafkaPartitioner;
67
import org.apache.flink.util.Preconditions;
78

8-
public class CustomerFlinkPartition<T> extends FlinkFixedPartitioner<T> {
9-
@Override
9+
public class CustomerFlinkPartition<T> extends FlinkKafkaPartitioner<T> {
10+
private static final long serialVersionUID = 1L;
11+
private int parallelInstanceId;
12+
13+
public CustomerFlinkPartition() {
14+
15+
}
16+
17+
public void open(int parallelInstanceId, int parallelInstances) {
18+
Preconditions.checkArgument(parallelInstanceId >= 0, "Id of this subtask cannot be negative.");
19+
Preconditions.checkArgument(parallelInstances > 0, "Number of subtasks must be larger than 0.");
20+
this.parallelInstanceId = parallelInstanceId;
21+
}
22+
1023
public int partition(T record, byte[] key, byte[] value, String targetTopic, int[] partitions) {
1124
Preconditions.checkArgument(partitions != null && partitions.length > 0, "Partitions of the target topic is empty.");
1225
if(key == null){
13-
return super.partition(record, key, value, targetTopic, partitions);
26+
return partitions[this.parallelInstanceId % partitions.length];
1427
}
1528
return partitions[key.hashCode() % partitions.length];
1629
}
30+
31+
public boolean equals(Object o) {
32+
return this == o || o instanceof CustomerFlinkPartition;
33+
}
34+
35+
public int hashCode() {
36+
return CustomerFlinkPartition.class.hashCode();
37+
}
1738
}

0 commit comments

Comments
 (0)