Flink 中处理偏斜数据的其他选项有哪些?

What are the other options to handle skew data in Flink?

正在研究Flink中的数据倾斜处理以及如何改变low-level control of physical partition in order to have an even processing of tuples. I have created synthetic skewed data sources and I aim to process (aggregate) them over a window. Here is the complete code

streamTrainsStation01.union(streamTrainsStation02)
        .union(streamTicketsStation01).union(streamTicketsStation02)
        // map the keys
        .map(new StationPlatformMapper(metricMapper)).name(metricMapper)
        .rebalance() // or .rescale() .shuffle()
        .keyBy(new StationPlatformKeySelector())
        .window(TumblingProcessingTimeWindows.of(Time.seconds(20)))
        .apply(new StationPlatformRichWindowFunction(metricWindowFunction)).name(metricWindowFunction)
        .setParallelism(4)
        .map(new StationPlatformMapper(metricSkewedMapper)).name(metricSkewedMapper)
        .addSink(new MqttStationPlatformPublisher(ipAddressSink, topic)).name(metricSinkFunction)
        ;

根据 Flink 仪表板,我看不出 .shuffle().rescale().rebalance() 之间有太大差异。尽管文档说 rebalance() 转换更适合数据倾斜。

之后我尝试使用 .partitionCustom(partitioner, "someKey")。但是,令我惊讶的是,我无法在 window 操作上使用 setParallelism(4)。文档说

Note: This operation is inherently non-parallel since all elements have to pass through the same operator instance.

我不明白为什么。如果允许我做 partitionCustom,为什么我不能在那之后使用并行性?这是 complete code.

streamTrainsStation01.union(streamTrainsStation02)
        .union(streamTicketsStation01).union(streamTicketsStation02)
        // map the keys
        .map(new StationPlatformMapper(metricMapper)).name(metricMapper)
        .partitionCustom(new StationPlatformKeyCustomPartitioner(), new StationPlatformKeySelector())
        .windowAll(TumblingProcessingTimeWindows.of(Time.seconds(20)))
        .apply(new StationPlatformRichAllWindowFunction(metricWindowFunction)).name(metricWindowFunction)
        .map(new StationPlatformMapper(metricSkewedMapper)).name(metricSkewedMapper)
        .addSink(new MqttStationPlatformPublisher(ipAddressSink, topic)).name(metricSinkFunction)
        ;

谢谢, 费利佩

我从 FLink-user-mail list 得到了答案。基本上在 rebalance() 之后使用 keyBy() 会杀死 rebalance() 试图做的所有效果。我发现的第一个(临时)解决方案是创建一个关心倾斜密钥的复合密钥。

public class CompositeSkewedKeyStationPlatform implements Serializable {
    private static final long serialVersionUID = -5960601544505897824L;
    private Integer stationId;
    private Integer platformId;
    private Integer skewParameter;
}

我在map函数上用了之后才用keyBy()

public class StationPlatformSkewedKeyMapper
        extends RichMapFunction<MqttSensor, Tuple2<CompositeSkewedKeyStationPlatform, MqttSensor>> {
    private SkewParameterGenerator skewParameterGenerator;

    public StationPlatformSkewedKeyMapper() {
        this.skewParameterGenerator = new SkewParameterGenerator(10);
    }

    @Override
    public Tuple2<CompositeSkewedKeyStationPlatform, MqttSensor> map(MqttSensor value) throws Exception {
        Integer platformId = value.getKey().f2;
        Integer stationId = value.getKey().f4;
        Integer skewParameter = 0;

        if (stationId.equals(new Integer(2)) && platformId.equals(new Integer(3))) {
            skewParameter = this.skewParameterGenerator.getNextItem();
        }
        CompositeSkewedKeyStationPlatform compositeKey = new CompositeSkewedKeyStationPlatform(stationId, platformId,
                skewParameter);
        return Tuple2.of(compositeKey, value);
    }
}

这是我的complete solution