尝试在 Kafka 中创建分区时出现 NoClassDefFoundError
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)
...
原因:java.lang.ClassNotFoundException:com.fasterxml.jackson.databind.JsonNode
在 java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
在 java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
在 java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 还有 68 个
在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>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</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-annotations 和 -core 添加到 maven 依赖项中,但没有帮助。
ClassNotFoundException: com.fasterxml.jackson.databind.JsonNode
所以你需要
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.10</version>
</dependency>
但是,您确实应该使用 jackson-bom
一致地导入所有相关的依赖项版本
这是客户端 2.6 版本中的错误:https://issues.apache.org/jira/browse/KAFKA-10378
我建议在 2.6.1 或 2.7 发布之前坚持使用 2.5 客户端。除非有一个你离不开的关键 2.6 功能,否则包含你自己的依赖项的建议是一个合理的建议。
我有以下代码:
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)
... 原因:java.lang.ClassNotFoundException:com.fasterxml.jackson.databind.JsonNode 在 java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) 在 java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) 在 java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) ... 还有 68 个
在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>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</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-annotations 和 -core 添加到 maven 依赖项中,但没有帮助。
ClassNotFoundException: com.fasterxml.jackson.databind.JsonNode
所以你需要
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.10</version>
</dependency>
但是,您确实应该使用 jackson-bom
一致地导入所有相关的依赖项版本
这是客户端 2.6 版本中的错误:https://issues.apache.org/jira/browse/KAFKA-10378
我建议在 2.6.1 或 2.7 发布之前坚持使用 2.5 客户端。除非有一个你离不开的关键 2.6 功能,否则包含你自己的依赖项的建议是一个合理的建议。