[英]Kafka server contains messages but consumer cannot receive any
Using a producer I entered some messages inside Kafka server which is running on localhost. 我使用生产者在运行于localhost的Kafka服务器内部输入了一些消息。 Zookeeper is also on localhost.
Zookeeper也在本地主机上。 I have used
ConsumerGroupExample
as given here . 我使用了此处给出的
ConsumerGroupExample
。
However, the consumer does not seem to receive any message! 但是,消费者似乎没有收到任何消息! the
kafka-console-consumer.sh
script can pull out all those messages, but the code cannot. kafka-console-consumer.sh
脚本可以提取所有这些消息,但是代码不能。 What is wrong? 怎么了? The Consumer code is exactly as it is given on that page.
使用者代码与该页面上给出的完全相同。
zookeeper="localhost:2181", group id = "test-a", topic = "test".
This is the same topic on which I published messages. 这是我发布消息的同一主题。 Here's code for the producer:
这是生产者的代码:
package test;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
import java.util.Properties;
import kafka.javaapi.producer.Producer;
public class KakfaProducer {
public static void main(String[] args) {
Properties prop = new Properties();
prop.put("zookeeper.connect", "localhost:2181");
prop.put("metadata.broker.list", "localhost:9092");
prop.put("serializer.class", "kafka.serializer.StringEncoder");
ProducerConfig producerConfig = new ProducerConfig(prop);
Producer<String, String> producer = new<String, String> Producer(producerConfig);
Task t = new Task(producer);
Thread newThread = new Thread(t);
newThread.start();
}
}
class Task implements Runnable {
Producer<String, String> producer;
public Task(Producer producer) {
this.producer = producer;
}
public void run() {
long num = 1;
while (true) {
String topic = "test";
KeyedMessage<String, String> message = new<String, String> KeyedMessage(topic, "Hello Test message " + num);
producer.send(message);
synchronized (this) {
num++;
}
if (num % 1000 == 0) {
System.out.println("Total messages sent by Thread-" + Thread.currentThread().getId() + num);
}
}
}
}
Try changing your group.id
to something else and test. 尝试将
group.id
更改为其他内容并进行测试。
When you used kafka-console-conumser.sh
to test your consumer, it consumed data from the topic and changed an offset. 当您使用
kafka-console-conumser.sh
测试您的使用者时,它消耗了主题中的数据并更改了偏移量。 That's why your Consumer code couldn't consume with the same group.id
. 这就是为什么您的使用者代码无法与同一
group.id
一起使用的group.id
。
Also, when you change your group.id
, add to your Consumer : 另外,当您更改
group.id
,将其添加到使用者:
props.put("auto.offset.reset", "earliest");
This will make sure that your new consumer group consumes data from the beginning. 这将确保您的新使用者组从一开始就使用数据。
Your consumer didn't receive any data : 您的消费者未收到任何数据:
You already used the same group.id to consume before, and it has a new offset. 您之前已经使用了相同的group.id进行消费,并且具有新的偏移量。 It will consume only when there is new data in your Broker.
仅当您的Broker中有新数据时,它才会消耗。 (Change the
group.id
and test again.) (更改
group.id
并再次测试。)
Try auto.offset.reset=earliest
. 尝试使用
auto.offset.reset=earliest
。 The consumer will read messages from the beginning, and you can use this for testing your consumer. 使用者将从头开始阅读消息,您可以使用它来测试您的使用者。 There are also ways to read from a certain offset.
也有从特定偏移量读取的方法。
seek()
, seekToBeginning()
, and seekToEnd()
. seek()
, seekToBeginning()
和seekToEnd()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.