Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 卡夫卡消费者无法在Windows上读取数据_Java_Apache Kafka - Fatal编程技术网

Java 卡夫卡消费者无法在Windows上读取数据

Java 卡夫卡消费者无法在Windows上读取数据,java,apache-kafka,Java,Apache Kafka,我正试图编写一个简单的卡夫卡消费者来读取windows机器上的数据。 但我的消费者无法读取任何数据。制作人制作了20多条消息,但没有一条消息被消费 以下是我的消费者代码: Properties properties= new Properties(); properties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"localhost:9092"); properties.setProperty(ConsumerC

我正试图编写一个简单的卡夫卡消费者来读取windows机器上的数据。 但我的消费者无法读取任何数据。制作人制作了20多条消息,但没有一条消息被消费

以下是我的消费者代码:

 Properties properties= new Properties();
    properties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"localhost:9092");
    properties.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
    properties.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class.getName());
    properties.setProperty(ConsumerConfig.GROUP_ID_CONFIG,"my_application");
    properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false");

    // properties.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");

    //create the consumer

    KafkaConsumer<String, String> consumer= new KafkaConsumer<String, String>(properties);

    consumer.subscribe(Collections.singleton("first_topic"));

        ConsumerRecords<String, String> record=consumer.poll(Duration.ofMillis(100));
        for(ConsumerRecord data: record){
            System.out.println("Key: "+data.key()+" and value: "+data.value());
            System.out.println("Topic: "+data.topic());
            System.out.println("Partition: "+data.partition());
        }
我必须显式地使用
--partition0
选项来获取消息。
有什么方法可以解决这个问题吗?

在代码中使用以下任何属性-

 `properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest")
它告诉消费者仅阅读最新消息,即消费者启动后发布的消息

`properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest")
当使用者开始使用分区中的消息时,它将从头开始读取

使用以下命令

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
----分组我的应用程序它应该是——分组我的应用程序

请按照以下步骤操作

a。获取主题的分区

./kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic test
Topic: test PartitionCount: 1   ReplicationFactor: 1    Configs: segment.bytes=1073741824
    Topic: test Partition: 0    Leader: 0   Replicas: 0 Isr: 0
b。获取主题的偏移量。

/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic test --time -1
test:0:11
c。使用分区的消息

/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test   --partition 0 
您还可以指定一个指示从哪个偏移开始的
--offset
参数。如果不存在,则消耗从分区的末尾开始

您还可以使用基于GUI的工具查看名为kafka tool的主题的每个分区中的数据。它是管理卡夫卡集群的工具

Java代码

TopicPartition partition0 = new TopicPartition(topic, 0);
 TopicPartition partition1 = new TopicPartition(topic, 1);
 consumer.assign(Arrays.asList(partition0, partition1));
您可以在下面的URL中使用java获得详细的实现


在代码中使用以下任何属性-

 `properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest")
它告诉消费者仅阅读最新消息,即消费者启动后发布的消息

`properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest")
当使用者开始使用分区中的消息时,它将从头开始读取

使用以下命令

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
----分组我的应用程序它应该是——分组我的应用程序

请按照以下步骤操作

a。获取主题的分区

./kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic test
Topic: test PartitionCount: 1   ReplicationFactor: 1    Configs: segment.bytes=1073741824
    Topic: test Partition: 0    Leader: 0   Replicas: 0 Isr: 0
b。获取主题的偏移量。

/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic test --time -1
test:0:11
c。使用分区的消息

/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test   --partition 0 
您还可以指定一个指示从哪个偏移开始的
--offset
参数。如果不存在,则消耗从分区的末尾开始

您还可以使用基于GUI的工具查看名为kafka tool的主题的每个分区中的数据。它是管理卡夫卡集群的工具

Java代码

TopicPartition partition0 = new TopicPartition(topic, 0);
 TopicPartition partition1 = new TopicPartition(topic, 1);
 consumer.assign(Arrays.asList(partition0, partition1));
您可以在下面的URL中使用java获得详细的实现


谢谢你的回答。但行为还是一样的。记录计数仍然是0。我尝试了两种偏移量配置,但都不起作用。谢谢你的回答。但行为还是一样的。记录计数仍然是0。我尝试了两种偏移量配置,但都不起作用。