Scala KafkaConsumer Position()与Committed()的比较

Scala KafkaConsumer Position()与Committed()的比较,scala,apache-kafka,kafka-consumer-api,Scala,Apache Kafka,Kafka Consumer Api,如果我需要使用特定消费群体的最新提交的偏移量(用于startingOffset from Spark Structured Streaming),我应该使用什么 我的代码显示提交的已弃用 committed(TopicPartition partition): OffsetAndMetadata Get the last committed offset for the given partition (whether the commit happened by this process

如果我需要使用特定消费群体的最新提交的偏移量(用于startingOffset from Spark Structured Streaming),我应该使用什么

我的代码显示提交的已弃用

committed(TopicPartition partition): OffsetAndMetadata  

Get the last committed offset for the given partition (whether the commit happened by this process or another).

org.apache.kafka
卡夫卡客户
2.4.1
官方文件:

补偿和消费者地位 Kafka为分区中的每条记录维护一个数字偏移量。该偏移量充当该分区内记录的唯一标识符,还表示使用者在分区中的位置。例如,位于位置5的使用者已使用偏移量为0到4的记录,然后将接收偏移量为5的记录。实际上,与消费者用户相关的位置有两个概念: 消费者的位置给出了下一条记录的偏移量。它将比使用者在该分区中看到的最高偏移量大一个。每次消费者在call to poll(长)中收到消息时,它都会自动前进

提交位置是安全存储的最后一个偏移量。如果进程失败并重新启动,这是使用者将恢复到的偏移量。使用者可以定期自动提交偏移量;或者,它可以选择通过调用一个提交API(例如commitSync和commitSync)手动控制此提交位置。

您需要将Spark流媒体作业中的提交偏移量用作启动偏移量

position
API的计数器在其运行时由KafkaConsumer递增,并且可能与
committed
API的结果略有不同,因为使用者可能提交偏移量,也可能不提交偏移量,如果提交,则可能异步执行

在Kafka 2.4.1中,不推荐使用方法
committed(partition)
,建议使用该方法获取主题分区的
Set
。其签名为:

  <dependency>
      <groupId>org.apache.kafka</groupId>
      <artifactId>kafka-clients</artifactId>
      <version>2.4.1</version>
    </dependency>

已提交公共映射(设置分区)

在使用Scala时,需要将Scala集转换为Java集。这可以按照描述来完成。

我们在scala中有没有使用committed的例子,以及如何从Ite获取(分区->偏移量)的映射示例:val last=consumer.committed(partitions.toSet)如何提取分区和偏移量的映射{“0”->200,“1”->100}它已经是一个映射了。所以,试着找出制作scala地图的最佳方法。last.foreach(x=>println(x._1+“->”+x._2.offset())…不确定。请帮忙
  val latestOffset = consumer.position(partition)
  val last=consumer.committed(partition)

  <dependency>
      <groupId>org.apache.kafka</groupId>
      <artifactId>kafka-clients</artifactId>
      <version>2.4.1</version>
    </dependency>

public Map<TopicPartition,OffsetAndMetadata> committed(Set<TopicPartition> partitions)