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

时间:2019-04-10 11:14:39

标签: java partitioning flink-streaming skew

我正在研究Flink中的数据偏斜处理,以及如何更改低level control of physical partition以便对元组进行均匀处理。我创建了合成的偏斜数据源,我的目标是在一个窗口上处理(汇总)它们。这是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")。但是,令我惊讶的是,我无法在窗口操作上使用setParallelism(4)。该文档说

  

注意:由于所有元素,此操作本质上是非并行的   必须通过相同的运算符实例。

我不明白为什么。如果允许我做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)
        ;

谢谢, 费利佩

1 个答案:

答案 0 :(得分:0)

我从FLink-用户邮件列表中得到了答案。基本上在keyBy()之后使用rebalance()会杀死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

相关问题