繁体   English   中英

Flink:ProcessWindowFunction

[英]Flink: ProcessWindowFunction

我最近正在Flink的新版本中研究ProcessWindowFunction 它说ProcessWindowFunction支持全局状态和窗口状态。 我使用Scala API进行尝试。 到目前为止,我可以使全局状态正常工作,但是我没有任何运气可以使它成为窗口状态。 我正在做的是处理系统日志并计算按主机名和严重性级别键入的日志数。 我想计算两个相邻窗口之间的日志计数差异。 这是我实现ProcessWindowFunction代码。

class LogProcWindowFunction extends ProcessWindowFunction[LogEvent, LogEvent, Tuple, TimeWindow] {
  // Create a descriptor for ValueState
  private final val valueStateWindowDesc = new ValueStateDescriptor[Long](
    "windowCounters",
    createTypeInformation[Long])

  private final val reducingStateGlobalDesc = new ReducingStateDescriptor[Long](
    "globalCounters",
    new SumReduceFunction(),
    createTypeInformation[Long])

  override def process(key: Tuple, context: Context, elements: Iterable[LogEvent], out: Collector[LogEvent]): Unit = {
    // Initialize the per-key and per-window ValueState
    val valueWindowState = context.windowState.getState(valueStateWindowDesc)
    val reducingGlobalState = context.globalState.getReducingState(reducingStateGlobalDesc)
    val latestWindowCount = valueWindowState.value()
    println(s"lastWindowCount: $latestWindowCount ......")
    val latestGlobalCount = if (reducingGlobalState.get() == null) 0L else reducingGlobalState.get()
    // Compute the necessary statistics and determine if we should launch an alarm
    val eventCount = elements.size
    // Update the related state
    valueWindowState.update(eventCount.toLong)
    reducingGlobalState.add(eventCount.toLong)
    for (elem <- elements) {
      out.collect(elem)
    }
  }
}

我总是从窗口状态中获得0值,而不是应该是以前的更新计数。 我已经为这种问题苦苦挣扎了好几天了。 有人可以帮我弄清楚吗? 谢谢。

每个窗口状态的范围是单个窗口实例。 对于上述process方法,每次调用新窗口都在范围内,因此latestWindowCount始终为零。

对于仅触发一次的普通香草窗口,每个窗口状态都没有用。 只有在某个窗口多次触发(例如,后期触发)时,您才能充分利用每个窗口的状态。 如果您想记住一个窗口到另一个窗口的某些内容,则可以使用全局窗口状态来完成。

有关使用每个窗口的状态来记住要在后期触发中使用的数据的示例,请参阅Flink 高级窗口培训中的幻灯片13-19。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM