一、问题描述
Flink程序,批处理程序,执行报错:
Exception in thread "main" java.lang.RuntimeException: No data sinks have been created yet. A program needs at least one sink that consumes data. Examples are writing the data set or printing it.
at org.apache.flink.api.java.ExecutionEnvironment.createProgramPlan(ExecutionEnvironment.java:945)
at org.apache.flink.api.java.ExecutionEnvironment.createProgramPlan(ExecutionEnvironment.java:923)
at org.apache.flink.api.java.LocalEnvironment.execute(LocalEnvironment.java:85)
at org.apache.flink.api.scala.ExecutionEnvironment.execute(ExecutionEnvironment.scala:539)
at com.bd.flink._1203DataSet.DataSetCounter$.main(DataSetCounter.scala:52)
at com.bd.flink._1203DataSet.DataSetCounter.main(DataSetCounter.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
二、问题原因
没有sink过程,flink执行之前,要求至少一个sink去消费现有的数据。
三、解决问题
添加sink过程即可,注意sink(writeAsText)过程,需要在env执行过程之前:
...
val filepath="data//output-scala-count"
//默认写到文件
result.writeAsText(filepath,FileSystem.WriteMode.OVERWRITE)
//执行获得结果result
val jobResult= env.execute("CounterAcc")
val num=jobResult.getAccumulatorResult[Long]("ele-counts-scala")