Flink 执行中报错:
Exception in thread "main" java.lang.RuntimeException: No new data sinks have been defined since the last execution. The last execution refers to the latest call to 'execute()', 'count()', 'collect()', or 'print()'.
代码如下:
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.operators.FlatMapOperator;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;
public class WordCount {
public static void main(String[] args) throws Exception {
ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(1);
DataSource<String> textFile = env.readTextFile("datas/1.txt");
FlatMapOperator<String, Tuple2<String, Long>> flatMapOperator = textFile.flatMap((String line, Collector<Tuple2<String, Long>> out) -> {
String[] words = line.split(" ");
for (String word : words) {
out.collect(Tuple2.of(word, 1L));
}
})
// 为防止泛型擦除,使用returns指明数据类型
.returns(Types.TUPLE(Types.STRING, Types.LONG));
flatMapOperator
.groupBy(0).sum(1).print();
// env.execute();
}
}
说明: 读取文件进行单词操作,属于批处理,最后 print()
已经触发程序执行了,execute
用于流式处理,在这没必要重复写了。
解决方法: 将最后一行 env.execute();
注释掉即可