Apache Flink:ProcessWindowFunction实现

时间:2018-11-29 22:05:20

标签: scala apache apache-flink

我正在尝试使用Scala在我的Apache Flink项目中使用ProcessWindowFunction。不幸的是,我已经无法实现基本的ProcessWindowFunction,就像Apache Flink文档中使用的那样。

这是我的代码:

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())

我非常感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

花了一些时间与另外2个人进行调试后,我们终于找到了问题所在。

在我的代码中,我使用了以下导入:

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

但是使用Scala时正确的导入似乎是:

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

答案 1 :(得分:0)

$args = array(
            'orderby'=>'date',
            'post_status'=>'publish'
        );

//sort by bank if isset
if(isset($_POST['bank'])){
    $args['tax_query'][]= array(
                'taxonomy'=>'banks',
                'field'=>'id',
                'terms'=>$_POST['bank']
                );
}
if(isset($_POST['card_type'])){
    $args['tax_query']['relation']=   'AND';//you can remove this

    $args['tax_query'][]=   array(
                            'taxonomy'=>'cardtype',
                            'field'=>'id',
                            'terms'=>$_POST['card_type']
                    );

}
$the_query = new WP_Query( $args );