如何导入Kafka Clients包的ConsumerRebalanceListener

时间:2017-08-01 12:55:27

标签: java hadoop apache-kafka hdfs kafka-consumer-api

我已经编写了基本的Kafka消费者程序,如Eclipse中的以下代码段所示

package KafkaConsumer.Kafka.Consumer.Trial1;

import org.apache.kafka.clients.consumer.ConsumerConfig; 
import org.apache.kafka.clients.consumer.ConsumerRebalanceListener; 
import org.apache.kafka.clients.consumer.ConsumerRecord; 
import org.apache.kafka.clients.consumer.ConsumerRecords; 
import org.apache.kafka.clients.consumer.KafkaConsumer; 
import org.apache.kafka.common.TopicPartition;  
import java.util.Collection; 
import java.util.Collections; 
import java.util.Properties;  
public class App {
    public static void main(String[] args) {
        Properties consumerConfig = new Properties();
        consumerConfig.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "sandbox.hortonworks.com:6667");
        consumerConfig.put(ConsumerConfig.GROUP_ID_CONFIG, "my-group");
        consumerConfig.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        consumerConfig.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
        consumerConfig.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
        KafkaConsumer<byte[], byte[]> consumer = new KafkaConsumer<>(consumerConfig);
        TestConsumerRebalanceListener rebalanceListener = new TestConsumerRebalanceListener();
        consumer.subscribe(Collections.singletonList("ashtest"), rebalanceListener);
        while (true) {
            ConsumerRecords<byte[], byte[]> records = consumer.poll(1000);
            for (ConsumerRecord<byte[], byte[]> record : records) {
                System.out.printf("Received Message topic =%s, partition =%s, offset = %d, value = %s\n",record.topic(), record.partition(), record.offset(), record.value());
            }
            consumer.commitSync();
        }
    }
    private static class TestConsumerRebalanceListener implements ConsumerRebalanceListener {
        public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
            System.out.println("Called onPartitionsRevoked with partitions:" + partitions);
        }
        public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
            System.out.println("Called onPartitionsAssigned with partitions:" + partitions);
        }
    }
  }

它指向第二个导入行的错误,如下图enter image description here

所示

我已经添加了以下代码所需的以下依赖项

<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>KafkaConsumer</groupId>
    <artifactId>Kafka.Consumer.Trial1</artifactId>
    <version>0.1</version>
    <packaging>jar</packaging>

    <name>Kafka.Consumer.Trial1</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>0.8.2-beta</version>
        </dependency>
    </dependencies>
</project>

显示的错误如下所述:

    Description Resource    Path    Location    Type
ConsumerRebalanceListener cannot be resolved to a type  App.java    /Kafka.Consumer.Trial1/src/main/java/KafkaConsumer/Kafka/Consumer/Trial1    line 31 Java Problem
KEY_DESERIALIZER_CLASS_CONFIG cannot be resolved or is not a field  App.java    /Kafka.Consumer.Trial1/src/main/java/KafkaConsumer/Kafka/Consumer/Trial1    line 19 Java Problem
The import org.apache.kafka.clients.consumer.ConsumerRebalanceListener cannot be resolved   App.java    /Kafka.Consumer.Trial1/src/main/java/KafkaConsumer/Kafka/Consumer/Trial1    line 4  Java Problem
The type ConsumerRecord is not generic; it cannot be parameterized with arguments <byte[], byte[]>  App.java    /Kafka.Consumer.Trial1/src/main/java/KafkaConsumer/Kafka/Consumer/Trial1    line 25 Java Problem
The type ConsumerRecords is not generic; it cannot be parameterized with arguments <byte[], byte[]> App.java    /Kafka.Consumer.Trial1/src/main/java/KafkaConsumer/Kafka/Consumer/Trial1    line 24 Java Problem
The type KafkaConsumer is not generic; it cannot be parameterized with arguments <> App.java    /Kafka.Consumer.Trial1/src/main/java/KafkaConsumer/Kafka/Consumer/Trial1    line 20 Java Problem
The type KafkaConsumer is not generic; it cannot be parameterized with arguments <byte[], byte[]>   App.java    /Kafka.Consumer.Trial1/src/main/java/KafkaConsumer/Kafka/Consumer/Trial1    line 20 Java Problem
VALUE_DESERIALIZER_CLASS_CONFIG cannot be resolved or is not a field    App.java    /Kafka.Consumer.Trial1/src/main/java/KafkaConsumer/Kafka/Consumer/Trial1    line 18 Java Problem

但上述错误仍然存​​在。我想知道所有依赖项需要导入以使代码运行。 还需要指导如何将此代码编辑到下一级别,我必须将KafkaConsumer数据附加到HDFS上的文件

0 个答案:

没有答案