Spark Streaming 的 countByValueAndWindow 是如何工作的?

How does Spark Streaming's countByValueAndWindow work?

我有一个正在处理网站点击事件流的 Spark Streaming 应用程序。每个事件都有一个 属性,其中包含一个 GUID,用于标识该事件所属的用户会话。

我的应用程序正在使用窗口计算每个会话发生的事件数:

def countEvents(kafkaStream: DStream[(String, Event)]): DStream[(String, Session)] = {

  // Get a list of the session GUIDs from the events
  val sessionGuids = kafkaStream
    .map(_._2)
    .map(_.getSessionGuid)

  // Count up the GUIDs over our sliding window
  val sessionGuidCountsInWindow = sessionGuids.countByValueAndWindow(Seconds(60), Seconds(1))

  // Create new session objects with the event count
  sessionGuidCountsInWindow
    .map({
      case (guidS, eventCount) =>
        guidS -> new Session().setGuid(guidS).setEventCount(eventCount)
  })
}

我的理解是 countByValueAndWindow 函数只计算调用该函数的 DStream 中的值。换句话说,在上面的代码中,对 countByValueAndWindow 的调用应该 return 事件只对我们调用该函数的 sessionGuids DStream 中的会话 GUID 计数。

但我观察到一些不同的东西;对 countByValueAndWindow 的调用是 returning 计算不在 sessionGUID 中的会话 GUID。它似乎是 returning 计数在前几批中处理的会话 GUID。我只是误解了这个功能是如何工作的吗?我无法在网上找到任何有用的文档。

我的一个同事比我更精通 Spark 的方式,帮助我解决了这个问题。显然我误解了 countByValueAndWindow 函数的工作方式。我认为它只会 return 计算您为其调用函数的 DStream 中的值。但实际上,它 returns 计算整个 window 中的所有值。为了解决我的问题,我只是在我的输入 DStream 和 countByValueAndWindow 操作产生的 DStream 之间执行连接。因此,我最终只得到输入 DStream 中值的结果。