尝试使用lambda简化flink代码报错
SingleOutputStreamOperator<String> wordStream = lineStream.flatMap( (value, out) -> {
for (String s : value.split(" ")) {
out.collect(s);
}
}) ;
Exception in thread "main" org.apache.flink.api.common.functions.InvalidTypesException: The return type of function 'main(BoundWordcount_lambda.java:25)' 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.
at org.apache.flink.api.dag.Transformation.getOutputType(Transformation.java:484)
at org.apache.flink.streaming.api.datastream.DataStream.getType(DataStream.java:190)
at org.apache.flink.streaming.api.datastream.DataStream.flatMap(DataStream.java:638)
at com.flink.BoundWordcount_lambda.main(BoundWordcount_lambda.java:32)
Caused by: 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.
at org.apache.flink.api.java.typeutils.TypeExtractionUtils.validateLambdaType(TypeExtractionUtils.java:351)
at org.apache.flink.api.java.typeutils.TypeExtractionUtils.extractTypeFromLambda(TypeExtractionUtils.java:176)
at org.apache.flink.api.java.typeutils.TypeExtractor.getUnaryOperatorReturnType(TypeExtractor.java:515)
at org.apache.flink.api.java.typeutils.TypeExtractor.getFlatMapReturnTypes(TypeExtractor.java:168)
at org.apache.flink.streaming.api.datastream.DataStream.flatMap(DataStream.java:637)
at com.flink.BoundWordcount_lambda.main(BoundWordcount_lambda.java:25)
大意是flink需要返回值的数据类型,修改后如下:
SingleOutputStreamOperator<String> wordStream = lineStream.flatMap((FlatMapFunction<String, String>) (value, out) -> {
for (String s : value.split(" ")) {
out.collect(s);
}
}).returns(String.class);
还可以精简,但是参数类型必须保存:
SingleOutputStreamOperator<String> wordStream = lineStream
.flatMap((String value, Collector<String> out) -> {
for (String word : value.split(" ")) {
out.collect(word);
}
})
//.returns(String.class);
.returns(Types.STRING);
还没有评论,来说两句吧...