我一直在使用Apache Flink编写原型应用程序。在此过程中,我选择将 org.apache.flink.streaming.api.functions.windowing.WindowFunction 用于特定的用例。但是,在编写apply()函数的主体时,我遇到了这个错误(下面的代码不是来自我正在编写的应用程序 - 我的数据类型不同 - 它来自Flink文档站点中提供的示例代码):< / p>
import scala.collection.Iterable
import scala.collection.Map
import org.apache.flink.streaming.api.functions.windowing.WindowFunction
import org.apache.flink.streaming.api.windowing.windows.{TimeWindow}
import org.apache.flink.util.Collector
import scala.collection.JavaConversions._
class MyWindowFunction extends WindowFunction[(String, Long), String, String, TimeWindow] {
def apply(key: String, window: TimeWindow, input: Iterable[(String, Long)], out: Collector[String]): Unit = {
var count = 0L
for (in <- input) {
count = count + 1
}
out.collect(s"Window $window count: $count")
}
}
编译器抱怨:
Error:(16, 7) class MyWindowFunction needs to be abstract, since method apply in trait WindowFunction of type
(x$1: String, x$2: org.apache.flink.streaming.api.windowing.windows.TimeWindow,
x$3: Iterable[(String, Long)],
x$4: org.apache.flink.util.Collector[String])Unit is not defined
class MyWindowFunction extends WindowFunction[(String, Long), String, String, TimeWindow] {
我已经检查了 apply()中参数的顺序;他们似乎是正确的。
出于某种原因,我未能发现错误的确切来源。有人可以帮我解决一下这个问题吗?
答案 0 :(得分:4)
我找到了这个错误的原因。
我不清楚的事实是Apache Flink的API需要一个java.lang.Iterable而不是它的Scala等价物:
class MyWindowFunction extends
WindowFunction[(String, Long), String, String, TimeWindow] {
override
def apply(
key: String,
w: TimeWindow,
iterable: Iterable[(String, Long)], // from java.lang.Iterable
collector: Collector[String]): Unit = {
// ....
}
}
所以,我必须妥善导入:
import java.lang.Iterable // From Java
import java.util.Map // From Java
import org.apache.flink.streaming.api.functions.windowing.WindowFunction
import org.apache.flink.streaming.api.windowing.windows.TimeWindow
import org.apache.flink.util.Collector
import scala.collection.JavaConversions._ // Implicit conversions
class MyWindowFunction
extends WindowFunction[(String, Long), String, String, TimeWindow] {
override
def apply(
key: String,
w: TimeWindow,
iterable: Iterable[(String, Long)],
collector: Collector[String]): Unit = {
// ....
}
}
一切都很好!