如何在scala中公开kafka(commitsync with partitions)的java方法?

yws3nbqq  于 2021-06-06  发布在  Kafka
关注(0)|答案(1)|浏览(346)

我正试图通过scala公开java方法(关于原始java方法的更多细节——它来自kafka)
这是原始的java方法:

public void commitSync(Map<TopicPartition,OffsetAndMetadata> offsets)

如何在scala中向方法公开和传递参数?我有点像:

def commitSync() = {
     consumer.commitSync(...)
}

谢谢。

6jjcrrmo

6jjcrrmo1#

您的scala片段看起来是正确的,下面是我如何填充其余部分的:

import org.apache.kafka.clients.consumer.{KafkaConsumer, OffsetAndMetadata}
import org.apache.kafka.common.TopicPartition

import collection.mutable.Map
import collection.JavaConverters._

//initialise your consumer the way you want
val consumer = createKafkaConsumer(config, subscriptions)

//you could accept a scala.collection.mutable.Map here
def commitSync(offsets: Map[TopicPartition, OffsetAndMetadata]) = {
    //and then convert it to a java.util.Map
    consumer.commitSync(offsets.asJava)
}

相关问题