我如何在 Scala 中实现 Kafka Consumer
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38757621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I implement Kafka Consumer in Scala
提问by annedroiid
I'm trying to implement a kafka consumer in scala. I've seen a million tutorials for how to do it in Java, and even some (like this one) that say it's for scala but it's written in Java.
我正在尝试在 Scala 中实现一个 kafka 消费者。我已经看过一百万篇关于如何使用 Java 进行操作的教程,甚至有些(比如这个)说它是为 Scala 编写的,但它是用 Java 编写的。
Does anyone know where I can find an example of how to write it in Scala? I've only just started to learn Scala so maybe the linked example can be used in Scala even though it's written in Java or something, but I honestly have no idea what I'm doing at the moment. Everything I google just links me to how to do it in Java.
有谁知道我在哪里可以找到如何在 Scala 中编写它的示例?我才刚刚开始学习 Scala,所以也许链接的示例可以在 Scala 中使用,即使它是用 Java 或其他语言编写的,但老实说,我现在不知道我在做什么。我在谷歌上搜索的所有内容都将我链接到如何在 Java 中进行操作。
回答by Yuval Itzchakov
The reason you're seeing most of the examples in Java is that the new KafkaProducerstarting 0.8.2.2 is written in Java.
您看到大多数 Java 示例的原因是新的KafkaProducer起始 0.8.2.2 是用 Java 编写的。
Assuming you're using sbt as your build system, and assuming your working with Kafka 0.8.2.2 (you can change the version as needed), you'll need:
假设您使用 sbt 作为构建系统,并假设您使用 Kafka 0.8.2.2(您可以根据需要更改版本),您将需要:
libraryDependencies ++= {
Seq(
"org.apache.kafka" %% "kafka" % "0.8.2.2",
"org.apache.kafka" % "kafka-clients" % "0.8.2.2",
)
}
A simple example should get you started:
一个简单的例子应该让你开始:
import scala.collection.JavaConverters._
import org.apache.kafka.clients.consumer.KafkaConsumer
import org.apache.kafka.common.serialization.StringDeserializer
object KafkaExample {
def main(args: Array[String]): Unit = {
val properties = new Properties()
properties.put("bootstrap.servers", "localhost:9092")
properties.put("group.id", "consumer-tutorial")
properties.put("key.deserializer", classOf[StringDeserializer])
properties.put("value.deserializer", classOf[StringDeserializer])
val kafkaConsumer = new KafkaConsumer[String, String](properties)
kafkaConsumer.subscribe("firstTopic", "secondTopic")
while (true) {
val results = kafkaConsumer.poll(2000).asScala
for ((topic, data) <- results) {
// Do stuff
}
}
}
回答by Shiv4nsh
You can also look to a working template totally build on Scala here: https://github.com/knoldus/activator-kafka-scala-producer-consumerThis application contains the code that you want to use in here.
您还可以在此处查看完全基于 Scala 构建的工作模板:https: //github.com/knoldus/activator-kafka-scala-producer-consumer该应用程序包含您要在此处使用的代码。
I hope I solved your problem thanks !
我希望我解决了你的问题谢谢!

