ApacheFlink:processwindowfunction实现

cnh2zyt3  于 2021-06-21  发布在  Flink
关注(0)|答案(2)|浏览(329)

我正在尝试使用scala在apache flink项目中使用processwindowfunction。不幸的是,我在实现基本processwindowfunction方面已经失败了,就像ApacheFlink文档中使用的那样。
这是我的密码:

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.scala.{StreamExecutionEnvironment, _}
import org.apache.flink.streaming.api.windowing.time.Time
import org.fiware.cosmos.orion.flink.connector.{NgsiEvent, OrionSource}
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction
import org.apache.flink.streaming.api.windowing.windows.TimeWindow
import org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows
import org.apache.flink.util.Collector
import scala.collection.TraversableOnce

object StreamingJob {
 def main(args: Array[String]) {

 val env = StreamExecutionEnvironment.getExecutionEnvironment
 val eventStream = env.addSource(new OrionSource(9001))

 val processedDataStream = eventStream.flatMap(event => event.entities)
   .map(entity => (entity.id, entity.attrs("temperature").value.asInstanceOf[String]))
     .keyBy(_._1)
     .window(SlidingProcessingTimeWindows.of(Time.seconds(10), Time.seconds(5)))
     .process(new MyProcessWindowFunction())

 env.execute("Socket Window NgsiEvent")
 }
}

private class MyProcessWindowFunction extends ProcessWindowFunction[(String, String), String, String, TimeWindow] {

def process(key: String, context: Context, input: Iterable[(String, String)], out: Collector[String]): Unit = {
  var count: Int = 0
  for (in <- input) {
    count = count + 1
  }
  out.collect(s"Window ${context.window} count: $count")
 }
}

从intellij我得到以下提示:
1) 这显示了创建新类对象的位置:

Type mismatch, expected: ProcessWindowFunction[(String, String), NotInferedR, String, TimeWindow], actual: MyProcessWindowFunction

2) 这在课堂上直接显示:

Class 'MyProcessWindowFunction' must either be declared abstract or implement abstract member 'process(key:KEY, context:ProcessWindowFunction.Context, iterable:Iterable<IN>, collector:Collector<OUT>):void' in 'org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction'

生成代码时显示以下错误:

Error:(51, 16) type mismatch;
found   : org.apache.flink.MyProcessWindowFunction
required: 
org.apache.flink.streaming.api.scala.function.ProcessWindowFunction[(String, String),?,String,org.apache.flink.streaming.api.windowing.windows.TimeWindow]
  .process(new MyProcessWindowFunction())

我很感激你的帮助。

ylamdve6

ylamdve61#

在花了一些时间和另外两个人调试之后,我们终于找到了问题所在。
在我的代码中,我使用了以下导入:

import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction

但使用scala时正确的导入方式似乎是:

import org.apache.flink.streaming.api.scala.function.ProcessWindowFunction
sq1bmfud

sq1bmfud2#

//package of  ProcessWindowFunction is 
import org.apache.flink.streaming.api.scala.function.ProcessWindowFunction

//The correct way to call this method
new MyProcessWindowFunction()[(String, String), String, String, TimeWindow]

//I know the official documents don't.This may be a bug

相关问题