利用lambda方法,将POJO类Event转换为二元数组
结果报以下错误:
Exception in thread "main" org.apache.flink.api.common.functions.InvalidTypesException: The return type of function 'main(Transformation_returnType.java:23)' could not be determined automatically, due to type erasure. You can give type information hints by using the returns(...) method on the result of the transformation call, or by letting your function implement the 'ResultTypeQueryable' interface.
发生了泛型擦除。。。
分析问题对于像 flatMap() 这样的函数,它的函数签名 void flatMap(IN value, Collector解决问题out) 被 Java 编译器编译成了 void flatMap(IN value, Collector out) ,也就是说将 Collector 的泛 型信息擦除掉了。这样 Flink 就无法自动推断输出的类型信息了。 当使用 map() 函数返回 Flink 自定义的元组类型时也会发生类似的问题。下例中的函数签 名 Tuple2 map(Event value) 被类型擦除为 Tuple2 map(Event value) 。 在这种情况下,我们需要显式地指定类型信息,否则输出将被视为 Object 类型,这会导 致低效的序列化。
1、使用显式的 ".returns(...)" DataStream> stream3 = clicks .map( event -> Tuple2.of(event.user, 1L) ) .returns(Types.TUPLE(Types.STRING, Types.LONG)); // 声明返回类型 stream3.print();
2、使用类来替代 Lambda 表达式 clicks.map(new MyTuple2Mapper()).print(); // 底下实现自定义的MyTuple2Mapper类
3、使用匿名类来代替 Lambda 表达式 clicks.map(new MapFunction>() { @Override public Tuple2 map(Event value) throws Exception { return Tuple2.of(value.user, 1L); } }).print();



