优化Flink应用的4种方式

原文:4 Ways to Optimize Your Flink Applications
作者:Ivan Mushketyk
翻译:Diwei

译者注:Apache Flink是一个面向分布式数据流处理和批量数据处理的开源计算平台。作者在本文介绍了一些如何优化Flink应用速度的方式。以下为译文。

Flink框架非常复杂,并提供了许多方法来调整其执行方式。本文我将介绍提高Flink应用程序性能的四种不同方法。

如果不熟悉Flink,你可以阅读一些介绍性的文章,比如这篇这篇,还有这篇。但是如果已经非常熟悉Apache Flink了,本文描述的内容可以帮助你如何提高应用程序的运行速度。

当使用类似于groupByjoinkeyBy这些操作时,Flink提供了多种方式以便用户在数据集中选择主键。用户可以使用主键选择函数:

// Join movies and ratings datasets
movies.join(ratings)
        // Use movie id as a key in both cases
        .where(new KeySelector<Movie, String>() {
            @Override
            public String getKey(Movie m) throws Exception {
                return m.getId();
            }
        })
        .equalTo(new KeySelector<Rating, String>() {
            @Override
            public String getKey(Rating r) throws Exception {
                return r.getMovieId();
            }
        })

也可以在POJO类型中指定字段名称:

movies.join(ratings)
// Use same fields as in the previous example
.where("id")
.equalTo("movieId")

但是如果现在使用的是Flink tuple类型,那么只要简单地指定字段元组的位置,就可以被用作主键了:

DataSet<Tuple2<String, String>> movies = ...
DataSet<Tuple3<String, String, Double>> ratings = ...

movies.join(ratings)
    // Specify fields positions in tuples
    .where(0)
    .equalTo(1)

可见最后一种方式的性能是最好的,但是可读性怎么样呢?代码现在看起来是不是就像下面这样?

DataSet<Tuple3<Integer, String, Double>> result = movies.join(ratings)
    .where(0)
    .equalTo(0)
    .with(new JoinFunction<Tuple2<Integer,String>, Tuple2<Integer,Double>, Tuple3<Integer, String, Double>>() {
        // What is happening here?
        @Override
        public Tuple3<Integer, String, Double> join(Tuple2<Integer, String> first, Tuple2<Integer, Double> second) throws Exception {
            // Some tuples are joined with some other tuples and some fields are returned???
            return new Tuple3<>(first.f0, first.f1, second.f1);
        }
    });

在本例中,想要提高可读性,最常见的做法就是创建一个类,该类需要继承TupleX类,并为类里面的这些字段实现getter和setter。下面是Flink Gelly库的Edge类,继承了Tuple3类:

public class Edge<K, V> extends Tuple3<K, K, V> {
    public Edge(K source, K target, V value) {
        this.f0 = source;
        this.f1 = target;
        this.f2 = value;
    }

    // Getters and setters for readability
    public void setSource(K source) {
        this.f0 = source;
    }
    public K getSource() {
        return this.f0;
    }

    // Also has getters and setters for other fields
    ...
}

复用Flink对象

另一个可以用来提高Flink应用程序性能的选项是,当从用户定义的函数返回数据时,最好使用可变对象。看看下面这个例子:

stream
    .apply(new WindowFunction<WikipediaEditEvent, Tuple2<String, Long>, String, TimeWindow>() {
        @Override
        public void apply(String userName, TimeWindow timeWindow, Iterable<WikipediaEditEvent> iterable, Collector<Tuple2<String, Long>> collector) throws Exception {
            long changesCount = ...
            // A new Tuple instance is created on every execution
            collector.collect(new Tuple2<>(userName, changesCount));
        }
    }

可以看出,apply函数每执行一次,都会新建一个Tuple2类的实例,因此增加了对垃圾收集器的压力。解决这个问题的一种方法是反复使用相同的实例:

stream
    .apply(new WindowFunction<WikipediaEditEvent, Tuple2<String, Long>, String, TimeWindow>() {
        // Create an instance that we will reuse on every call
        private Tuple2<String, Long> result = new Tuple<>();

        @Override
        public void apply(String userName, TimeWindow timeWindow, Iterable<WikipediaEditEvent> iterable, Collector<Tuple2<String, Long>> collector) throws Exception {
            long changesCount = ...

            // Set fields on an existing object instead of creating a new one
            result.f0 = userName;
            // Auto-boxing!! A new Long value may be created
            result.f1 = changesCount;

            // Reuse the same Tuple2 object
            collector.collect(result);
        }
    }

这种做法更好一点。虽然每次调用时都新建一个Tuple2的实例,但是其实还间接创建了Long类的实例。为了解决这个问题,Flink有许多所谓的value class:IntValueLongValueStringValueFloatValue等。下面介绍一下如何使用它们:

stream
    .apply(new WindowFunction<WikipediaEditEvent, Tuple2<String, Long>, String, TimeWindow>() {
        // Create a mutable count instance
        private LongValue count = new IntValue();
        // Assign mutable count to the tuple
        private Tuple2<String, LongValue> result = new Tuple<>("", count);

        @Override
        // Notice that now we have a different return type
        public void apply(String userName, TimeWindow timeWindow, Iterable<WikipediaEditEvent> iterable, Collector<Tuple2<String, LongValue>> collector) throws Exception {
            long changesCount = ...

            // Set fields on an existing object instead of creating a new one
            result.f0 = userName;
            // Update mutable count value
            count.setValue(changesCount);

            // Reuse the same tuple and the same LongValue instance
            collector.collect(result);
        }
    }

这种做法经常用在Flink库里面,如Flink Gelly。

使用注解功能

优化Flink应用程序的另一种方法是提供一些关于用户自定义的函数会对输入数据做哪些操作的信息。由于Flink无法解析和理解代码,所以可以提供一些有利于构建更有效执行计划的重要信息。可以使用以下三个注解:

  1. @ForwardedFields:指定输入值中哪些字段保持不变,哪些字段是用于输出的。

  2. @NotForwardedFields:指定在输出中未保留相同位置的字段。

  3. @ReadFields:指定用来计算结果值的字段。指定的字段应该只在计算中使用,而不仅仅是复制到输出参数中。

看一下如何使用ForwardedFields注释:

// Specify that the first element is copied without any changes
@ForwardedFields("0")
class MyFunction implements MapFunction<Tuple2<Long, Double>, Tuple2<Long, Double>> {
    @Override
    public Tuple2<Long, Double> map(Tuple2<Long, Double> value) {
       // Copy first field without change
        return new Tuple2<>(value.f0, value.f1 + 123);
    }
}

这意味着输入元组中的第一个元素没有被更改,它将返回到相同的位置。

如果不更改字段,但只需将其移动到另一个位置,那么也可以使用ForwardedFields。在下一个示例中,我们在输入tuple中互换一下字段,并通知Flink:

// 1st element goes into the 2nd position, and 2nd element goes into the 1st position
@ForwardedFields("0->1; 1->0")
class SwapArguments implements MapFunction<Tuple2<Long, Double>, Tuple2<Double, Long>> {
    @Override
    public Tuple2<Double, Long> map(Tuple2<Long, Double> value) {
       // Swap elements in a tuple
        return new Tuple2<>(value.f1, value.f0);
    }
}

上面提到的注解只能应用于只有一个输入参数的函数,例如mapflatMap。如果函数有两个输入参数,则可以使用ForwardedFieldsFirstForwardedFieldsSecond,分别提供关于第一个参数和第二个参数的信息。

下面是如何在JoinFunction接口的实现中使用这些注释:

// Two fields from the input tuple are copied to the first and second positions of the output tuple
@ForwardedFieldsFirst("0; 1")
// The third field from the input tuple is copied to the third position of the output tuple
@ForwardedFieldsSecond("2")
class MyJoin implements JoinFunction<Tuple2<Integer,String>, Tuple2<Integer,Double>, Tuple3<Integer, String, Double>>() {
    @Override
    public Tuple3<Integer, String, Double> join(Tuple2<Integer, String> first, Tuple2<Integer, Double> second) throws Exception {
        return new Tuple3<>(first.f0, first.f1, second.f1);
    }
})

Flink还提供NotForwardedFieldsFirstNotForwardedFieldsSecondReadFieldsFirst ReadFirldsSecond注释,这些注释都可以达到类似目的。

Select Join Type

如果给Flink另一个提示,那么就可以让joins速度更快,但是在讨论它的工作原理之前,先讨论一下Flink是如何执行joins的。

当Flink处理批量数据时,集群中的每台机器都存储了部分数据。要执行join,Apache Flink需要找到满足连接条件的两个数据集。为了做到这一点,Flink首先必须将两个数据集的项目放在同一台机器上。这里有两种策略:

  1. Repartition-分配策略:在这种情况下,两个数据集都被各自的主键分离了,并通过网络发送。这意味着如果数据集很大,可能需要大量的时间才能通过网络完成复制。

  2. 广播转发策略:在这种情况下,一个数据集不受影响,但是第二个数据集被复制到集群中的每台机器上,它们都有第一个数据集的一部分。

如果是将某个小数据集join到更大的数据集,那么可以使用广播转发策略,这样也可以避免第一个数据集的分区付出的昂贵代价。这很容易做到:

ds1.join(ds2, JoinHint.BROADCAST_HASH_FIRST)

这就表示第一个数据集比第二个数据集小得多。

你也可以使用其他连接提示:

  • BROADCAST_HASH_SECOND:第二个数据集要小得多
  • REPARTITION_HASH_FIRST:第一个数据集稍微小一些
  • REPARTITION_HASH_SECOND:第二个数据集要小一点
  • REPARTITION_SORT_MERGE:使用排序和合并策略对数据集进行重新分配
  • **OPTIMIZER_CHOOSES:**Flink优化器将决定如何join数据集

阅读这篇文章可以更加了解Flink是如何在本文中执行join的。

更多信息

我衷心希望每个读者都能喜欢这篇文章,并且确实能帮助你解决一些问题。

后面我会陆陆续续写更多关于Flink的文章,敬请关注!你可以在这里阅读其他文章,或还可以看一下Pluralsight课程,在这里我将详细介绍Apache Flink:Understanding Apache Flink。以下是本课程的简短预览

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值