Apache kafka Kafka使用者返回空迭代器
在我的示例程序中,我尝试发布一个文件,并尝试立即使用它。但是我的使用者迭代器返回null。 知道我做错了什么吗 测试Apache kafka Kafka使用者返回空迭代器,apache-kafka,kafka-consumer-api,kafka-producer-api,Apache Kafka,Kafka Consumer Api,Kafka Producer Api,在我的示例程序中,我尝试发布一个文件,并尝试立即使用它。但是我的使用者迭代器返回null。 知道我做错了什么吗 测试 **main(){** KafkaMessageProducer producer = new KafkaMessageProducer(topic, file); producer.generateMessgaes(); MessageListener listener = new MessageListener(topic);
**main(){**
KafkaMessageProducer producer = new KafkaMessageProducer(topic, file);
producer.generateMessgaes();
MessageListener listener = new MessageListener(topic);
listener.start();
}
MessageListener
public void start() {
Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
topicCountMap.put(topic, new Integer(CoreConstants.THREAD_SIZE));
Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumerConnector
.createMessageStreams(topicCountMap);
List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(topic);
executor = Executors.newFixedThreadPool(CoreConstants.THREAD_SIZE);
for (KafkaStream<byte[], byte[]> stream : streams) {
System.out.println("The stream is --"+ stream.iterator().makeNext().topic());
executor.submit(new ListenerThread(stream));
}
try { // without this wait the subsequent shutdown happens immediately before any messages are delivered
Thread.sleep(10000);
} catch (InterruptedException ie) {
}
if (consumerConnector != null) {
consumerConnector.shutdown();
}
if (executor != null) {
executor.shutdown();
}
}
public void start(){
Map topicCountMap=新HashMap();
topicCountMap.put(主题,新整数(CoreConstants.THREAD_SIZE));
映射consumerMap=consumerConnector
.createMessageStreams(topicCountMap);
列表流=consumerMap.get(主题);
executor=Executors.newFixedThreadPool(CoreConstants.THREAD\u SIZE);
用于(卡夫卡斯特林流:流){
System.out.println(“流是--”+stream.iterator().makeNext().topic());
提交(新ListenerThread(stream));
}
尝试{//如果不等待,则在传递任何消息之前立即关闭
睡眠(10000);
}捕获(中断异常ie){
}
if(consumerConnector!=null){
consumerConnector.shutdown();
}
if(executor!=null){
executor.shutdown();
}
}
ListenerThread
public class ListenerThread implements Runnable {
private KafkaStream<byte[], byte[]> stream;
public ListenerThread(KafkaStream<byte[], byte[]> msgStream) {
this.stream = msgStream;
System.out.println("----------" + stream.iterator().makeNext().topic());
}
public void run() {
try {
ConsumerIterator<byte[], byte[]> it = stream.iterator();
while (it.hasNext()) {
// MessageAndMetadata<byte[], byte[]> messageAndMetadata =
// it.makeNext();
// String topic = messageAndMetadata.topic();
// byte[] message = messageAndMetadata.message();
System.out.println("111111111111111111111111111");
FileProcessor processor = new FileProcessor();
processor.processFile("LOB_TOPIC", it.next().message());
}
公共类ListenerThread实现可运行{
私人卡夫卡斯特伦溪流;
公共ListenerThread(KafkaStream msgStream){
this.stream=msgStream;
System.out.println(“-----------”+stream.iterator().makeNext().topic());
}
公开募捐{
试一试{
ConsumerIterator it=stream.iterator();
while(it.hasNext()){
//MessageAndMetadata MessageAndMetadata=
//it.makeNext();
//字符串topic=messageAndMetadata.topic();
//byte[]message=messageAndMetadata.message();
System.out.println(“111111111111111111”);
FileProcessor=新的FileProcessor();
processFile(“LOB_主题”,it.next().message());
}
在上面的迭代器中,它不会进入while循环,因为迭代器为null。但我确信我正在向同一主题发布一条消息,并且消费者会侦听该主题
如果有任何帮助,我将不胜感激。我昨天也遇到了同样的问题。在尝试使用它一段时间后,我无法从当前主题中阅读它。因此,我采取了以下步骤 a、 阻止了我的消费者 b、 阻止了制作人 c、 停止卡夫卡服务器 bin/zookeeper-server-stop.sh config/zookeeper.properties d、 动物园管理员停了下来 bin/zookeeper-server-stop.sh config/zookeeper.properties 之后,我删除了我的主题。 bin/kafka-topics.sh--删除--zookeeper本地主机:2181--主题测试 我还删除了按照“设置多代理群集”创建的文件,但我认为这并没有造成问题 a、 动物园管理员说 b、 卡夫卡 c、 开始制作并向卡夫卡发送一些消息
它又开始工作了。我不确定这是否对你有帮助。但似乎我的制作人与消费者的连接断开了。希望这有帮助。我也有同样的问题。我可以在消费者终端看到消息,但我的应用程序现在显示“空迭代器”。它曾经奏效,并突然发生。如果有人知道原因,我将不胜感激。我不知道我是如何克服这个问题的。在某个时候,它消失了。但我现在面临类似的问题,我在卡夫卡用户邮件列表中发布了这一问题。主题“如果我的制作人生产,那么为什么消费者不能消费?它卡住了@poll()