为什么kafka生产者抛出java.lang.noclassdeffounderror:kafka-producer

时间:2017-07-15 02:12:11

标签: scala intellij-idea sbt apache-kafka kafka-producer-api

我正在尝试在scala SBT中运行此示例kafka生成器代码,但我很容易收到此错误。我可以看到该类在版本0.11.0.0的kafka-clients上可用。

我是SBT和KAFKA的新手,我今天花了一整天才能生成数据。你能帮忙吗?感谢

Github:www.github.com/axeish/kafkawatcher

我的built.sbt如下:

name := "KafkaWatcher"
version := "1.0"
scalaVersion := "2.12.2"

libraryDependencies += "org.apache.kafka" % "kafka-clients" % "0.11.0.0"
resolvers += Resolver.mavenLocal

我的制作人代码是这样的:

import java.util

import java.util.Properties import org.apache.kafka.clients.producer._

object producer extends App {

  val props = new Properties()   props.put("bootstrap.server", "localhost:9092");   props.put("acks","1")   props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")   props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")
  val producer = new KafkaProducer[String, String](props)
  val topic="kafka"
  for(i<- 1 to 50) {
    val record = new ProducerRecord(topic, "key"+i, "value"+i)
    producer.send(record)   }

  producer.close()

}

和, 我收到了以下错误:

  

线程中的异常&#34; main&#34; java.lang.NoClassDefFoundError:   org / apache / kafka / clients / producer / KafkaProducer at   java.lang.Class.getDeclaredMethods0(Native Method)at   java.lang.Class.privateGetDeclaredMethods(Class.java:2701)at at   java.lang.Class.privateGetMethodRecursive(Class.java:3048)at   java.lang.Class.getMethod0(Class.java:3018)

0 个答案:

没有答案