1、当通过sparkstreaming连接kafka,即使你指定了一个不存在offset,即大于上边界或小于下边界,Kafka也将会根据这一条配置reset你的offset值,比如earliest或latest。
2、初始启动时,指定了一个OutOfRange的初始offset时,Spark不会理你的auto.offset.reset,而是按照用户设置的来,但是由于设置的数组上界比如超过本身存储的上界,或者存储的下界超过了本身存储的下界,那么就会得到提示的报错信息
3、因此需要设定修改默认值
import org.apache.kafka.clients.consumer.KafkaConsume
val consumer = new KafkaConsumer[String, String](pros)
consumer.subscribe(util.Collections.singletonList(userPlaySongTopic))
consumer.poll(1000)
// fromOffsets该处即为你根据kafka初始设定的partition保存的的offset值
for (topicPartition <- fromOffsets.keySet) {
consumer.seekToBeginning(util.Collections.singletonList(topicPartition))
// 得到这个topic所有partition中的offset
val offset: Long = consumer.position(topicPartition)
println("offset:" + offset)
//获得redis保存的offset
val consumedOffset: Long = fromOffsets.getOrElse(topicPartition, 0L)
println("consumedOffset:" + consumedOffset)
if (offset > consumedOffset) {
fromOffsets = fromOffsets.updated(topicPartition, offset)
}
189



被折叠的 条评论
为什么被折叠?



