为什么Spark Streaming with kafka总是在seektoEnd之前poll(0)


  • Spark版本:2.4.5
  • 组件:Spark Streaming
  • 类:DirectKafkaInputDStream

在类DirectKafkaInputDStream,我有点困惑,为什么要调用paraniodPoll之前seekToEnd?

protected def latestOffsets(): Map[TopicPartition, Long] = {
val c = consumer
paranoidPoll(c)
val parts = c.assignment().asScala
// make sure new partitions are reflected in currentOffsets
val newPartitions = parts.diff(currentOffsets.keySet)
// Check if there's any partition been revoked because of consumer rebalance.
val revokedPartitions = currentOffsets.keySet.diff(parts)
if (revokedPartitions.nonEmpty) {
throw new IllegalStateException(s"Previously tracked partitions " +
s"${revokedPartitions.mkString("[", ",", "]")} been revoked by Kafka because of consumer " +
s"rebalance. This is mostly due to another stream with same group id joined, " +
s"please check if there're different streaming application misconfigure to use same " +
s"group id. Fundamentally different stream should use different group id")
}
// position for new partitions determined by auto.offset.reset if no commit
currentOffsets = currentOffsets ++ newPartitions.map(tp => tp -> c.position(tp)).toMap
// find latest available offsets
c.seekToEnd(currentOffsets.keySet.asJava)
parts.map(tp => tp -> c.position(tp)).toMap
}

如果没有poll(0), assign()可能返回空集,poll确保客户端连接到Kafka协调器节点。

但是poll(0)已经被kafka-client弃用了,请检查Spark的替代API

参见:kafkconsumer assignment()返回空

最新更新