NoClassDefFoundError 在尝试在 Kafka 中创建分区时发生错误。

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

NoClassDefFoundError when trying to create partitions in Kafka

问题

以下是要翻译的内容:

我有以下代码:

public KafkaReader(KafkaConsumer<Long, byte[]> consumer, String topic) {
    this.consumer = consumer;
    this.topicPartition = new TopicPartition(topic, ((consumer.partitionsFor(topic).get(0)).partition()));
    consumer.assign(Collections.singleton(topicPartition));
}

但是当执行 consumer.partitionsFor(topic) 时,我得到以下错误:

java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/JsonNode

	at org.apache.kafka.common.requests.MetadataRequest$Builder.<clinit>(MetadataRequest.java:36)
	at org.apache.kafka.clients.consumer.KafkaConsumer.partitionsFor(KafkaConsumer.java:1962)
	at org.apache.kafka.clients.consumer.KafkaConsumer.partitionsFor(KafkaConsumer.java:1930)
	at data.KafkaReader.<init>(KafkaReader.java:20)
	at data.KafkaReaderTest.playground(KafkaReaderTest.java:12)
...
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.JsonNode
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	... 68 more

pom.xml 中,我定义了以下依赖:

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <!-- 其他依赖... -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.10</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.8.10</version>
    </dependency>
</dependencies>

我能找到的建议是将 jackson-annotationsjackson-core 添加到 Maven 依赖中,但这并没有帮助。

英文:

I have the following Code:

  public KafkaReader(KafkaConsumer&lt;Long, byte[]&gt; consumer, String topic) {
    this.consumer = consumer;
    this.topicPartition = new TopicPartition(topic, ((consumer.partitionsFor(topic).get(0)).partition()));
    consumer.assign(Collections.singleton(topicPartition));
}

but when consumer.partitionsFor(topic) is being executed I get the following error:

java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/JsonNode

at org.apache.kafka.common.requests.MetadataRequest$Builder.&lt;clinit&gt;(MetadataRequest.java:36)
at org.apache.kafka.clients.consumer.KafkaConsumer.partitionsFor(KafkaConsumer.java:1962)
at org.apache.kafka.clients.consumer.KafkaConsumer.partitionsFor(KafkaConsumer.java:1930)
at data.KafkaReader.&lt;init&gt;(KafkaReader.java:20)
at data.KafkaReaderTest.playground(KafkaReaderTest.java:12)

...
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.JsonNode
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 68 more

In pom.xml I defined the following dependencies:

   &lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
        &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
        &lt;version&gt;${junit.jupiter.version}&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.hamcrest&lt;/groupId&gt;
        &lt;artifactId&gt;hamcrest-all&lt;/artifactId&gt;
        &lt;version&gt;1.3&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;commons-cli&lt;/groupId&gt;
        &lt;artifactId&gt;commons-cli&lt;/artifactId&gt;
        &lt;version&gt;1.4&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
        &lt;artifactId&gt;lombok&lt;/artifactId&gt;
        &lt;version&gt;1.18.12&lt;/version&gt;
        &lt;scope&gt;provided&lt;/scope&gt;
    &lt;/dependency&gt;
    &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;
    &lt;dependency&gt;
        &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
        &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
        &lt;version&gt;1.7.25&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;log4j&lt;/groupId&gt;
        &lt;artifactId&gt;log4j&lt;/artifactId&gt;
        &lt;version&gt;1.2.17&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
        &lt;artifactId&gt;jackson-core&lt;/artifactId&gt;
        &lt;version&gt;2.8.10&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
        &lt;artifactId&gt;jackson-annotations&lt;/artifactId&gt;
        &lt;version&gt;2.8.10&lt;/version&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;

All I cound find as advice was to add jackson-annotations and -core to maven dependencies, but it did not help.

答案1

得分: 3

找不到类: com.fasterxml.jackson.databind.JsonNode

所以你需要

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.10</version>
    </dependency>

然而,你应该真正使用`jackson-bom`来一致地导入所有相关的依赖版本
英文:

ClassNotFoundException: com.fasterxml.jackson.databind.JsonNode

So you will need

&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.8.10&lt;/version&gt;
&lt;/dependency&gt;

However, you should really be using the jackson-bom to import all related dependency versions consistently

答案2

得分: 3

这是客户端2.6版本中的一个错误:https://issues.apache.org/jira/browse/KAFKA-10378

我建议继续使用2.5版本的客户端,直到发布2.6.1或2.7版本为止。除非有一个关键的2.6功能是您无法没有的,那么建议包含您自己的依赖是一个明智的选择。

英文:

This is a bug in 2.6 version of the clients: https://issues.apache.org/jira/browse/KAFKA-10378

I recommend sticking to 2.5 clients until 2.6.1 or 2.7 is released. Unless there is a critical 2.6 feature that you can't live without, and then the recommendation to include your own dependency is a sound one.

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

发表评论

匿名网友

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

确定