未调用ProcessWindowFunction#process

时间:2019-07-20 04:58:21

标签: apache-flink

我有一个简单的代码,如下所示,当我在IDE中运行该代码时,控制台上未打印任何内容,有人可以帮忙看看吗?谢谢

import org.apache.flink.api.java.tuple.Tuple
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.TimeCharacteristic
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.streaming.api.scala.function.ProcessWindowFunction
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.api.windowing.windows.TimeWindow
import org.apache.flink.util.Collector

object WindowTest {

  def main(args: Array[String]): Unit = {
    val env = StreamExecutionEnvironment.getExecutionEnvironment
    env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime)
    val ds = env.fromElements(
      (1, "a"), (2, "b"), (3, "c"), (4, "e"), (5, "f"), (6, "g"), (7, "h"), (8, "g"), (1, "1a"), (2, "2b"), (3, "3c"), (4, "4e"), (5, "5f"), (6, "g"), (7, "h"), (8, "g")
    )
    val ds2 = ds.keyBy(0).timeWindow(Time.seconds(10))
      .process(new ProcessWindowFunction[(Int, String), String, Tuple, TimeWindow] {
        override def process(key: Tuple, context: Context, elements: Iterable[(Int, String)], out: Collector[String]): Unit = {
          val k = key.getField[Int](0)
          val w = context.window
          val start = w.getStart
          val end = w.getEnd
          val hc = context.window.hashCode()
          //NOT CALLED
          println(s"k=$k,start=$start, end=$end,hc=$hc")
        }
      })
    ds2.print() 
    env.execute()

    Thread.sleep(30 * 1000)


  }

}

1 个答案:

答案 0 :(得分:1)

因为窗口永远不会触发,所以永远不会调用您的ProcessWindowFunction。它从未触发过,因为它在几毫秒内就可以完成运行(大致来说),因此它不太可能在系统时钟时间恰好在10秒边界的时候运行,这是必须要做的要触发的处理时间窗口。