V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Umenezumi
V2EX  ›  问与答

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

  •  
  •   Umenezumi · 2020-11-16 11:06:54 +08:00 · 630 次点击
    这是一个创建于 1283 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 条回复    2020-11-16 11:18:46 +08:00
    Umenezumi
        1
    Umenezumi  
    OP
       2020-11-16 11:18:46 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3735 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 04:08 · PVG 12:08 · LAX 21:08 · JFK 00:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.