Skip to content

Instantly share code, notes, and snippets.

@chiwanpark
Created July 14, 2015 17:47
Show Gist options
  • Save chiwanpark/e71d27cc8edae8bc7298 to your computer and use it in GitHub Desktop.
Save chiwanpark/e71d27cc8edae8bc7298 to your computer and use it in GitHub Desktop.
Flink Custom Partitioner Example
import org.apache.flink.api.common.functions.Partitioner;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.tuple.Tuple2;
public class PartitionCustomExample {
public static class MyPartitioner implements Partitioner<Integer> {
@Override
public int partition(Integer key, int numPartitions) {
return key % numPartitions;
}
}
public static void main(String[] args) throws Exception {
final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
DataSet<Tuple2<Integer, String>> data = env.fromElements(
new Tuple2<Integer, String>(1, "a"), new Tuple2<Integer, String>(1000, "b"),
new Tuple2<Integer, String>(2, "k"), new Tuple2<Integer, String>(1020, "c"));
DataSet<Tuple2<Integer, String>> partitionedData =
data.partitionCustom(new MyPartitioner(), 0);
partitionedData.print();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment