简单的 Kafka 消费者示例无法通过 java api 工作

我正在编写 java api 来创建 kafka 消费者我有一个错误:


Exception in thread "main" java.lang.NoSuchMethodError: org.apache.kafka.clients.consumer.Consumer.subscribe(Ljava/util/Collection;)V

at in.co.hadooptutorials.data.generator.cdr.kafkaconsumer.main(kafkaconsumer.java:38)

下面是我的代码:


public class kafkaconsumer {

  public static void main(String[] args) throws IOException {

    String TOPIC_NAME = "test";

    Properties props = new Properties();

    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"localhost:9092");

    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.IntegerDeserializer");

    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");

    props.put(ConsumerConfig.GROUP_ID_CONFIG, "test" );

    props.put("enable.auto.commit", "false");

    props.put("auto.commit.interval.ms", "1000");

    props.put("session.timeout.ms", "30000");

    props.put("partition.assignment.strategy", "range");

    Consumer<String, String> consumer = new KafkaConsumer<>.(props);

    consumer.subscribe(Collections.singletonList("test"));

    ConsumerRecords<String, String> records = consumer.poll(100);


    System.out.println(consumer);

    System.out.println(records);

    consumer.close();

  } 

}


pem file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>


  <groupId>in.co.hadooptutorials</groupId>

  <artifactId>cdr-data-generator</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>jar</packaging>


  <name>cdr-data-generator</name>

  <url>http://maven.apache.org</url>


我正在添加我的 pem 文件的代码 pem 文件的完整代码还添加了 kafka clients-2.2.0 的 jar 它是一个 cdr 数据项目,使用 kafka 将数据存储到 cassandra 如何解决这个问题?我正在尝试仍然收到错误。


胡子哥哥
浏览 103回答 1
1回答

幕布斯6054654

确保在您的 POM 文件中具有以下依赖项:<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka --><dependency>&nbsp; &nbsp; <groupId>org.apache.kafka</groupId>&nbsp; &nbsp; <artifactId>kafka_2.12</artifactId>&nbsp; &nbsp; <version>2.2.0</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients --><dependency>&nbsp; &nbsp; <groupId>org.apache.kafka</groupId>&nbsp; &nbsp; <artifactId>kafka-clients</artifactId>&nbsp; &nbsp; <version>2.2.0</version></dependency>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java