Flink之Java lambda表达式

翟青青
2023-12-01

一.简介

Java 8引入了一些新的语言功能,旨在更快,更清晰地编码。它具有最重要的功能,即所谓的“ Lambda表达式”,为函数式编程打开了大门。Lambda表达式允许以直接方式实现和传递函数,而无需声明其他(匿名)类。

注意: Flink支持对Java API的所有运算符使用lambda表达式,但是,每当lambda表达式使用Java泛型时,都需要显式声明类型信息。

二.范例与限制

下面的示例说明如何实现一个简单的内联map()函数,该函数使用lambda表达式对输入进行平方。函数的输入i和输出参数的类型map()无需声明,因为它们是Java编译器推断的。

env.fromElements(1, 2, 3)
// returns the squared i
.map(i -> i*i)
.print();

Flink可以从方法签名的实现中自动提取结果类型信息,out map(in value)因为out【map的返回值类型】它不是通用的,而是in类型是可以自动推导的。

三.异常分析及解决方案

不幸的是,flatMap()带有签名的功能flatMap(IN value, Collector out)是flatMap(IN value, Collector out)由Java编译器编译进来的。这使得Flink无法自动推断输出类型的类型信息。

Flink很可能会引发类似于以下内容的异常:

org.apache.flink.api.common.functions.InvalidTypesException: The generic type parameters of 'Collector' are missing.
    In many cases lambda methods don't provide enough information for automatic type extraction when Java generics are involved.
    An easy workaround is to use an (anonymous) class instead that implements the 'org.apache.flink.api.common.functions.FlatMapFunction' interface.
    Otherwise the type has to be specified explicitly using type information.

在这种情况下,需要明确指定类型信息,否则输出将被视为Object导致无效序列化的类型。

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.util.Collector;

DataSet<Integer> input = env.fromElements(1, 2, 3);

// collector type must be declared
input.flatMap((Integer number, Collector<String> out) -> {
    StringBuilder builder = new StringBuilder();
    for(int i = 0; i < number; i++) {
        builder.append("a");
        out.collect(builder.toString());
    }
})
// provide type information explicitly
.returns(Types.STRING)
// prints "a", "a", "aa", "a", "aa", "aaa"
.print();

当使用map()具有通用返回类型的函数时,也会发生类似的问题。在下面的示例中Tuple2<Integer, Integer> map(Integer value)是基于删除方法签名Tuple2 map(Integer value)。

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;

env.fromElements(1, 2, 3)
    .map(i -> Tuple2.of(i, i))    // no information about fields of Tuple2
    .print();

通常,这些问题可以通过多种方式解决:

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;

// use the explicit ".returns(...)"
env.fromElements(1, 2, 3)
    .map(i -> Tuple2.of(i, i))
    .returns(Types.TUPLE(Types.INT, Types.INT))
    .print();

// use a class instead
env.fromElements(1, 2, 3)
    .map(new MyTuple2Mapper())
    .print();

public static class MyTuple2Mapper extends MapFunction<Integer, Tuple2<Integer, Integer>> {
    @Override
    public Tuple2<Integer, Integer> map(Integer i) {
        return Tuple2.of(i, i);
    }
}

// use an anonymous class instead
env.fromElements(1, 2, 3)
    .map(new MapFunction<Integer, Tuple2<Integer, Integer>> {
        @Override
        public Tuple2<Integer, Integer> map(Integer i) {
            return Tuple2.of(i, i);
        }
    })
    .print();

// or in this example use a tuple subclass instead
env.fromElements(1, 2, 3)
    .map(i -> new DoubleTuple(i, i))
    .print();

public static class DoubleTuple extends Tuple2<Integer, Integer> {
    public DoubleTuple(int f0, int f1) {
        this.f0 = f0;
        this.f1 = f1;
    }
}
 类似资料: