V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Umenezumi
V2EX  ›  问与答

Flink for Java demo,使用 lambda 表达式报错

  •  
  •   Umenezumi · Nov 16, 2020 · 1001 views
    This topic created in 1992 days ago, the information mentioned may be changed or developed.
    public class WordCountA {
        public static void main(String[] args) throws Exception {
            ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    
            String path = "xxx/hello.txt";
            DataSet<String> inputData = env.readTextFile(path);
    
            DataSet<Tuple2<String, Integer>> wordCount = inputData.flatMap((String line, Collector<Tuple2<String, Integer>> out) -> {
                for (String word : line.split(" ")) {
                    out.collect(Tuple2.of(word, 1));
                }
            }).groupBy(0)
                    .sum(1);
    
            wordCount.print();
        }
    }
    

    报错信息:

    Exception in thread "main" org.apache.flink.api.common.functions.InvalidTypesException: The return type of function 'main(WordCountA.java:19)' 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.java.DataSet.getType(DataSet.java:178)
    	at org.apache.flink.api.java.DataSet.groupBy(DataSet.java:701)
    	at com.haojiang.WordCountA.main(WordCountA.java:23)
    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.api.java.DataSet.flatMap(DataSet.java:266)
    	at com.haojiang.WordCountA.main(WordCountA.java:19)
    
    1 replies    2020-11-16 11:18:46 +08:00
    Umenezumi
        1
    Umenezumi  
    OP
       Nov 16, 2020
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   950 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:40 · PVG 02:40 · LAX 11:40 · JFK 14:40
    ♥ Do have faith in what you're doing.