Flink Scala API-应用新WindowFunction vs应用函数
我修改了Flink的基本wordcount示例,并使用了窗口函数 WindowedStream的apply方法被重载,它接受一个函数:Flink Scala API-应用新WindowFunction vs应用函数,scala,apache-flink,Scala,Apache Flink,我修改了Flink的基本wordcount示例,并使用了窗口函数 WindowedStream的apply方法被重载,它接受一个函数: def apply[R: TypeInformation]( function: (K, W, Iterable[T], Collector[R]) => Unit): DataStream[R] = { ... } 以及WindowFunction: def apply[R: TypeInformation]( function: Wi
def apply[R: TypeInformation](
function: (K, W, Iterable[T], Collector[R]) => Unit): DataStream[R] = { ... }
以及WindowFunction:
def apply[R: TypeInformation](
function: WindowFunction[T, R, K, W]): DataStream[R] = { ... }
当在WindowedStream上给apply方法一个函数时,我得到了要编译的代码,但是我的代码没有用WindowFunction编译(我不知道为什么…)
这是基本流程:
val windowCounts: WindowedStream[WordWithCount, String, TimeWindow] = text
.flatMap { w => w.split("\\s") }
.map { w => WordWithCount(w, 1) }
.keyBy(t => "all")
.window(SlidingProcessingTimeWindows.of(Time.seconds(30), Time.seconds(10)))
这是我对窗口函数的实现。
这个对我很有用:
def distinctCount(
s: String, tw: TimeWindow, input: Iterable[WordWithCount],
out: Collector[String]): Unit = {
val discount = input.map(t => t.word).toSet.size
out.collect(s"Distinct elements: $discount")
}
// compiles
val distinctCountStream = windowCounts.apply { distinctCount _ }
这一个不编译:
class DiscountWindowFunction extends WindowFunction[WordWithCount, String, String, TimeWindow] {
override def apply(key: String, window: TimeWindow, input: lang.Iterable[WordWithCount], out: Collector[String]): Unit = {
val discount = input.map(t => t.word).toSet.size
out.collect(s"Distinct elements: $discount")
}
def apply(key: String, window: TimeWindow, input: Iterable[(String, Int)], out: Collector[String]): Unit = {
apply(key, window, input.asJava, out)
}
}
// does not compile
val distinctCount = windowCounts.apply(new DiscountWindowFunction())
我使用的是Flink 1.3.2,这是我的导入:
import java.lang
import org.apache.flink.streaming.api.functions.windowing.WindowFunction
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.api.windowing.windows.TimeWindow
import org.apache.flink.util.Collector
import scala.collection.JavaConversions._
import scala.collection.JavaConverters._
您已经导入了Java数据流API中使用的
WindowFunction
您的代码应该在替换时编译
import org.apache.flink.streaming.api.functions.windowing.WindowFunction
借
顺便说一句,谢谢您提供完整的信息:-)哦,是的,谢谢!更改导入是解决方案。我没有想到会有一个
WindowFunction.scala
。
import org.apache.flink.streaming.api.scala.function.WindowFunction