如何在Java中创建一个Kafka生产者

huangapple go评论69阅读模式
英文:

How can I produce a kafka producer in Java

问题

我试图创建一个Kafka生产者的Java实例,但出现了以下错误,但当我运行控制台生产者时它是正常工作的。

我认为错误源于导入部分。

错误信息:

[main] INFO org.apache.kafka.common.utils.AppInfoParser - Kafka版本:2.6.0
[main] INFO org.apache.kafka.common.utils.AppInfoParser - Kafka提交ID:62abe01bee039651
[main] INFO org.apache.kafka.common.utils.AppInfoParser - Kafka启动时间:1602094910199
[kafka-producer-network-thread | producer-1] ERROR org.apache.kafka.common.utils.KafkaThread - 线程'kafka-producer-network-thread | producer-1'中的未捕获异常:
java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/JsonNode
	at org.apache.kafka.common.requests.ApiVersionsRequest$Builder.<clinit>(ApiVersionsRequest.java:36)
	at org.apache.kafka.clients.NetworkClient.handleConnections(NetworkClient.java:910)
	at org.apache.kafka.clients.NetworkClient.poll(NetworkClient.java:555)
	at org.apache.kafka.clients.producer.internals.Sender.runOnce(Sender.java:325)
	at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:240)
	at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.JsonNode
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	... 6 more
[main] INFO org.apache.kafka.clients.producer.KafkaProducer - [Producer clientId=producer-1] 以timeoutMillis = 9223372036854775807毫秒关闭Kafka生产者。
英文:

I'm trying to create a java instance of a Kafka producer which throws this error, but when I run the console producer it works.

I believe the error stems from as the imports

The error:

[main] INFO org.apache.kafka.common.utils.AppInfoParser - Kafka version: 2.6.0
[main] INFO org.apache.kafka.common.utils.AppInfoParser - Kafka commitId: 62abe01bee039651
[main] INFO org.apache.kafka.common.utils.AppInfoParser - Kafka startTimeMs: 1602094910199
[kafka-producer-network-thread | producer-1] ERROR org.apache.kafka.common.utils.KafkaThread - Uncaught exception in thread &#39;kafka-producer-network-thread | producer-1&#39;:
java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/JsonNode
	at org.apache.kafka.common.requests.ApiVersionsRequest$Builder.&lt;clinit&gt;(ApiVersionsRequest.java:36)
	at org.apache.kafka.clients.NetworkClient.handleConnections(NetworkClient.java:910)
	at org.apache.kafka.clients.NetworkClient.poll(NetworkClient.java:555)
	at org.apache.kafka.clients.producer.internals.Sender.runOnce(Sender.java:325)
	at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:240)
	at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.JsonNode
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	... 6 more
[main] INFO org.apache.kafka.clients.producer.KafkaProducer - [Producer clientId=producer-1] Closing the Kafka producer with timeoutMillis = 9223372036854775807 ms.

答案1

得分: 2

如果您仅使用kafka-clients依赖项,如文档所示:

<dependency>
  <groupId>org.apache.kafka</groupId>
  <artifactId>kafka-clients</artifactId>
  <version>2.6.0</version>
</dependency>

您应该注意,在它的pom.xml中,对jackson-databind的依赖标记为provided,这意味着它期望/假设您将在Web容器内运行您的消费者,该容器很可能有自己的实现,并且不希望干扰。

然而,如果您在没有jackson的类路径的jvm中运行 - 只需在您自己的pom.xml中添加任何您喜欢的版本:

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.10.5</version> <!-- 最新版本? -->
</dependency>

(尽管您很可能需要不止这一个依赖)。

英文:

If you're using just kafka-clients dependency as you can see in the documentation

&lt;dependency&gt;
  &lt;groupId&gt;org.apache.kafka&lt;/groupId&gt;
  &lt;artifactId&gt;kafka-clients&lt;/artifactId&gt;
  &lt;version&gt;2.6.0&lt;/version&gt;
&lt;/dependency&gt;

You should notice that in it's pom.xml the dependency to jackson-databind is marked as provided, meaning - it expects/assumes that you'll be running your consumer inside a web container, which most likely has it's own implementation and does not want to interfere.

However, if you're running in a jvm that doesn't have jackson in classpath already - then just add whatever version you like in your own pom.xml

&lt;dependency&gt;
  &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
  &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
  &lt;version&gt;2.10.5&lt;/version&gt; &lt;!-- latest? --&gt;
&lt;/dependency&gt;

( Although it's quite likely that you might need more than that one )

huangapple
  • 本文由 发表于 2020年10月8日 03:18:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64250875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定