本文介绍了如何在 Scala 中公开 Kafka 的 Java 方法(commitSync with partitions)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Scala 公开 Java 方法(有关原始 Java 方法的更多详细信息 - 它来自 Kafka)

I am trying to expose Java method via Scala (more details on original java method - it is coming from Kafka)

这是原始的Java方法:

This is original Java method:

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

如何向 Scala 中的方法公开和传递参数?我有类似的东西:

How to expose and pass parameters to a method in Scala? I have something like:

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

谢谢.

推荐答案

您的 Scala 代码段看起来是正确的,这就是我填写其余部分的方式:

Your Scala snippet looks correct, this is how I'd fill up the rest of it:

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)
}

这篇关于如何在 Scala 中公开 Kafka 的 Java 方法(commitSync with partitions)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 08:20