Flink用Lambda编程时需注意

调用完Transformation后有时需要调用returns方法指定返回的数据类型

例如:
#将单词和1组合
flatMaped.map( word -> Tuple2.of(word, 1)).returns(Types.TUPLE(Types.STRING, Types.INT))

如果没有返回数据类型,则会报错

Caused by: org.apache.flink.api.common.functions.InvalidTypesException: The generic type parameters of 'Tuple2' 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.MapFunction' interface. Otherwise the type has to be specified explicitly using type information.
	at org.apache.flink.api.java.typeutils.TypeExtractionUtils.validateLambdaType(TypeExtractionUtils.java:350)
	at org.apache.flink.api.java.typeutils.TypeExtractor.getUnaryOperatorReturnType(TypeExtractor.java:579)
	at org.apache.flink.api.java.typeutils.TypeExtractor.getMapReturnTypes(TypeExtractor.java:175)
	at org.apache.flink.streaming.api.datastream.DataStream.map(DataStream.java:599)
	at cn.mydoit.day02.ReduceDemo.main(ReduceDemo.java:35)

猜你喜欢

转载自blog.csdn.net/weixin_43648241/article/details/109015752