英文:
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 '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] 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
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.6.0</version>
</dependency>
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
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.5</version> <!-- latest? -->
</dependency>
( Although it's quite likely that you might need more than that one )
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论