Exception flink.api.common.functions.InvalidTypesException

Exception in thread “main” org.apache.flink.api.common.functions.InvalidTypesException: The return type of function ‘main(LambdaStreamWordCount.java:56)’ 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:417)
at org.apache.flink.streaming.api.datastream.DataStream.getType(DataStream.java:175)
at org.apache.flink.streaming.api.datastream.DataStream.map(DataStream.java:587)
at com.it.flink.wordcount.LambdaStreamWordCount.main(LambdaStreamWordCount.java:62)
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:350)
at org.apache.flink.api.java.typeutils.TypeExtractionUtils.extractTypeFromLambda(TypeExtractionUtils.java:176)
at org.apache.flink.api.java.typeutils.TypeExtractor.getUnaryOperatorReturnType(TypeExtractor.java:571)
at org.apache.flink.api.java.typeutils.TypeExtractor.getFlatMapReturnTypes(TypeExtractor.java:196)
at org.apache.flink.streaming.api.datastream.DataStream.flatMap(DataStream.java:611)
at com.it.flink.wordcount.LambdaStreamWordCount.main(LambdaStreamWordCount.java:56)

Process finished with exit code 1

The reason the use of lambda expressions can not automatically infer the return type you need to grow it clear that the return value type return value type .returns (Types.TUPLE (Types.STRING, Types.INT)) after each line lambda expression;

        SingleOutputStreamOperator<Tuple2<String,Integer>> wordAndOne = lines.flatMap((String line, Collector<Tuple2<String,Integer>> out) -> {

            //切分
            String[] split = line.split(" ");
            //转为stream是为了使用 lambda表达式方便
            Stream<String> stream = Arrays.stream(split);

            //收集到tuple中
            stream.forEach(x -> {
                out.collect(Tuple2.of(x, 1));
            });
            //由于使用lambda表达式 无法自动推断 返回值类型 所以需要明确指出返回值类型
            // Types 是flink 提供的类
        }).returns(Types.TUPLE(Types.STRING,Types.INT));
Published 33 original articles · won praise 12 · views 3325

Guess you like

Origin blog.csdn.net/IT_BULL/article/details/104105172