kafka消费者代码未完全运行

时间:2015-03-25 12:29:17

标签: apache-kafka kafka-consumer-api

我正在尝试一个简单的kafka生产者消费者客户端api,我的生产者类工作得很好,因为我可以从控制台看到消费者的消息但是当我运行消费者代码时没有显示任何东西,我没有得到什么问题或者我在做错的地方

这是生产者代码 -

package com.app.test;
import java.util.Properties;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;

public class SimpleProducer {
    private static Producer<Integer, String> producer;
    private final Properties props = new Properties();public    SimpleProducer()
    {
      props.put("metadata.broker.list", "localhost:9092");
      props.put("serializer.class", "kafka.serializer.StringEncoder");
      props.put("request.required.acks", "1");
      producer = new Producer<Integer, String>(new ProducerConfig(props));
    }
public static void main(String[] args) {
    SimpleProducer sp = new SimpleProducer();
    String topic = "test4";
    String messageStr = "hello";
    KeyedMessage<Integer, String> data = new KeyedMessage<Integer, String>(topic, messageStr);
    System.out.println("producer : "+producer);
    producer.send(data);
    producer.close();
  }
}

消费者类是 -

package com.app.test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import kafka.consumer.Consumer;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector;

public class SimpleHLConsumer {

  private final ConsumerConnector consumer;
  private final String topic;

  public SimpleHLConsumer(String zookeeper, String groupId, String topic) {
      Properties props = new Properties();
    props.put("zookeeper.connect", zookeeper);
    props.put("group.id", groupId);
    props.put("zookeeper.session.timeout.ms", "500");
    props.put("zookeeper.sync.time.ms", "250");
    props.put("auto.commit.interval.ms", "1000");

    consumer = Consumer.createJavaConsumerConnector(
    new ConsumerConfig(props));
    this.topic = topic;
  }



  public void testConsumer() {


    Map<String, Integer> topicCount = new HashMap<String, Integer>();
        // Define single thread for topic
    topicCount.put(topic, new Integer(1));      

    System.out.println("check1");

    Map<String, List<KafkaStream<byte[], byte[]>>> consumerStreams = consumer.createMessageStreams(topicCount);

    System.out.println("check2");
    List<KafkaStream<byte[], byte[]>> streams = consumerStreams.get(topic);

   for (KafkaStream stream : streams) {
        System.out.println("test----");
        System.out.println("test----"+stream.toString());
        ConsumerIterator<byte[], byte[]> consumerIte = stream.iterator();
        while (consumerIte.hasNext()) {
            try {
                System.out.println("Message from Single Topic: " + new String(consumerIte.next().message(), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    if (consumer != null)
      consumer.shutdown();
  }



  public static void main(String[] args) {

    String topic = "test4";
SimpleHLConsumer simpleHLConsumer = new SimpleHLConsumer("localhost:2181", "testgroup", topic);
    simpleHLConsumer.testConsumer();
  }
}

用于检查我已经在testConsumer()方法中使用sysout应用了2次检查,因此在运行时只显示check1,即代码未到达check2,我认为consumer.createMessageStreams(topicCount);存在一些问题,那么什么是原因以及如何解决?

1 个答案:

答案 0 :(得分:1)

代码中没有问题。

只需在你的jar中构建包含 kafka-version (你正在使用的kafka版本)的jar并运行它并尝试从生产者控制台发送消息。

希望这可以帮到你。