flink整合java,Flink for Java demo,使用 lambda 表达式报错

南门展
2023-12-01

public class WordCountA {

public static void main(String[] args) throws Exception {

ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

String path = "xxx/hello.txt";

DataSet inputData = env.readTextFile(path);

DataSet> wordCount = inputData.flatMap((String line, Collector> 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)

 类似资料: