使用kafka-client 2.6时出错。

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

Error when trying to use kafka-client 2.6

问题

我正在学习Kafka当我尝试实现一个只会发送Hello World的简单生产者时遇到了问题

package com.github.joe.kafka.tutorial1;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

public class ProducerDemo {
    public static void main(String[] args) {

        Properties properties = new Properties();
        String bootstrapServers = "127.0.0.1:9092";
        String topic = "first_topic";
        properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,bootstrapServers);
        properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName());

        KafkaProducer<String,String> producer = new KafkaProducer<String, String>(properties);

        ProducerRecord<String,String> record = new ProducerRecord<String, String>(topic,"Hello World");

        producer.send(record);
        producer.flush();
        producer.close();

    }
}

问题在于,当我使用kafka-client v2.0.0时,它可以工作,但是当我使用kafka-client 2.6.0时,出现故障,并且我得到以下错误:

[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.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.JsonNode
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 6 more
[main] INFO org.apache.kafka.clients.producer.KafkaProducer - [Producer clientId=producer-1] Closing the Kafka producer with timeoutMillis = 9223372036854775807 ms.

<details>
<summary>英文:</summary>

I&#39;m learning Kafka and when I was trying  to implement a simple producer that will only send &quot;Hello World&quot;

    package com.github.joe.kafka.tutorial1;
    
    import org.apache.kafka.clients.producer.KafkaProducer;
    import org.apache.kafka.clients.producer.ProducerConfig;
    import org.apache.kafka.clients.producer.ProducerRecord;
    import org.apache.kafka.common.serialization.StringSerializer;
    
    import java.util.Properties;
    
    public class ProducerDemo {
        public static void main(String[] args) {
    
            Properties properties = new Properties();
            String bootstrapServers = &quot;127.0.0.1:9092&quot;;
            String topic = &quot;first_topic&quot;;
            properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,bootstrapServers);
            properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
            properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName());
    
            KafkaProducer&lt;String,String&gt; producer = new KafkaProducer&lt;String, String&gt;(properties);
    
            ProducerRecord&lt;String,String&gt; record = new ProducerRecord&lt;String, String&gt;(topic,&quot;Hello World&quot;);
    
            producer.send(record);
            producer.flush();
            producer.close();
    
        }
    }

The problem is when I&#39;m using kafka-client v2.0.0 it works, but it fails when I&#39;m using kafka-client 2.6.0 and I get the following error:


    [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.lang.Thread.run(Thread.java:748)
    Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.JsonNode
    	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    	... 6 more
    [main] INFO org.apache.kafka.clients.producer.KafkaProducer - [Producer clientId=producer-1] Closing the Kafka producer with timeoutMillis = 9223372036854775807 ms.




</details>


# 答案1
**得分**: 1

正如事实证明的那样,在Kafka客户端2.6.0版本中,有一个额外的依赖项:
**Jackson Databind**
这在2.0.0版本中不是必需的。

<details>
<summary>英文:</summary>

as it turns out , for Kafka-client 2.6.0 there&#39;s an additional dependency: 
**Jackson Databind**
this was not required for version 2.0.0

</details>



# 答案2
**得分**: 1

上面的建议对我起了作用。我添加了以下依赖项,问题得到了解决。
```xml
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.9.8</version>
</dependency>
英文:

The above suggestion worked for me. I added the below dependency which resolved the problem.

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

huangapple
  • 本文由 发表于 2020年10月5日 10:46:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64201856.html
匿名

发表评论

匿名网友

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

确定