未能向 kafka 经纪人生产 java.nio.BufferUnderflowException

Failing to produce to kafka brokers java.nio.BufferUnderflowException

我能够从 kafka-console-producer.sh 生成消息,但是 java 客户端无法生成消息,但出现下面提到的异常。

使用java7,Kafka版本为0.8.2.1

发现异常

java.nio.BufferUnderflowException
at java.nio.Buffer.nextGetIndex(Buffer.java:498)
at java.nio.HeapByteBuffer.getLong(HeapByteBuffer.java:406)
at kafka.api.ProducerResponse$$anonfun$$anonfun$apply.apply(ProducerResponse.scala:40)
at kafka.api.ProducerResponse$$anonfun$$anonfun$apply.apply(ProducerResponse.scala:36)
at scala.collection.TraversableLike$$anonfun$map.apply(TraversableLike.scala:244)
at scala.collection.TraversableLike$$anonfun$map.apply(TraversableLike.scala:244)
at scala.collection.immutable.Range.foreach(Range.scala:141)
at scala.collection.TraversableLike$class.map(TraversableLike.scala:244)
at scala.collection.AbstractTraversable.map(Traversable.scala:105)
at kafka.api.ProducerResponse$$anonfun.apply(ProducerResponse.scala:36)
at kafka.api.ProducerResponse$$anonfun.apply(ProducerResponse.scala:33)
at scala.collection.TraversableLike$$anonfun$flatMap.apply(TraversableLike.scala:251)
at scala.collection.TraversableLike$$anonfun$flatMap.apply(TraversableLike.scala:251)
at scala.collection.immutable.Range.foreach(Range.scala:141)
at scala.collection.TraversableLike$class.flatMap(TraversableLike.scala:251)
at scala.collection.AbstractTraversable.flatMap(Traversable.scala:105)
at kafka.api.ProducerResponse$.readFrom(ProducerResponse.scala:33)
at kafka.producer.SyncProducer.send(SyncProducer.scala:114)
at kafka.producer.async.DefaultEventHandler.kafka$producer$async$DefaultEventHandler$$send(DefaultEventHandler.scala:275)
at kafka.producer.async.DefaultEventHandler$$anonfun$dispatchSerializedData.apply(DefaultEventHandler.scala:113)
at kafka.producer.async.DefaultEventHandler$$anonfun$dispatchSerializedData.apply(DefaultEventHandler.scala:105)
at scala.collection.TraversableLike$WithFilter$$anonfun$foreach.apply(TraversableLike.scala:772)
at scala.collection.mutable.HashMap$$anonfun$foreach.apply(HashMap.scala:98)
at scala.collection.mutable.HashMap$$anonfun$foreach.apply(HashMap.scala:98)
at scala.collection.mutable.HashTable$class.foreachEntry(HashTable.scala:226)
at scala.collection.mutable.HashMap.foreachEntry(HashMap.scala:39)
at scala.collection.mutable.HashMap.foreach(HashMap.scala:98)
at scala.collection.TraversableLike$WithFilter.foreach(TraversableLike.scala:771)
at kafka.producer.async.DefaultEventHandler.dispatchSerializedData(DefaultEventHandler.scala:105)
at kafka.producer.async.DefaultEventHandler.handle(DefaultEventHandler.scala:78)
at kafka.producer.Producer.send(Producer.scala:78)
at kafka.javaapi.producer.Producer.send(Producer.scala:35)
at com.myapplication.kafka.KafkaProducer.write(KafkaProducer.java:40)

关于如何调试的任何指示?

发生这种情况是因为生成消息的 kafka 客户端使用的是 0.10 jar,而 kafka 集群是 0.8.2.1

添加正确的 kafka 依赖项 0.8.2.1 解决了这个问题。