Global sorting in Apache Flink

懵懂的女人 提交于 2019-12-04 18:50:46

问题


sortPartition method of a dataset sorts the dataset locally based on some specified fields. How can I get my large Dataset sorted globally in an efficient way in Flink?


回答1:


This is currently not easily possible because Flink does not provide a built-in range partitioning strategy, yet.

A work-around is to implement a custom Partitioner:

DataSet<Tuple2<Long, Long>> data = ...
data
  .partitionCustom(new Partitioner<Long>() {
    int partition(Long key, int numPartitions) {
      // your implementation
    }
  }, 0)
  .sortPartition(0, Order.ASCENDING)
  .writeAsText("/my/output");

Note: In order to achieve balanced partitions with a custom partitioner, you need to know about the value range and distribution of the key.

Support for a range partitioner (with automatic sampling) in Apache Flink is currently work in progress and should be available soon.

Edit (June 7th, 2016): Range partitioning was added to Apache Flink with version 1.0.0. You can globally sort a data set as follows:

DataSet<Tuple2<Long, Long>> data = ...
data
  .partitionByRange(0)
  .sortPartition(0, Order.ASCENDING)
  .writeAsText("/my/output");

Note that range partitioning samples the input data set to compute a data distribution for equally-sized partitions.



来源:https://stackoverflow.com/questions/34071445/global-sorting-in-apache-flink

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!