[@小川游鱼][¥20]kafka的消费者stream iterator block,读不出消息?-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

[@小川游鱼][¥20]kafka的消费者stream iterator block,读不出消息?

晓生寒 2018-12-14 16:23:17 1074

rt,能够向kafka produce数据,kafka里也看得到,但是consumer却读不出数据,offset是0,程序在stream的iterator被阻塞了。demo代码如下:

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

import kafka.consumer.ConsumerConfig;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector;
import kafka.serializer.StringDecoder;
import kafka.utils.VerifiableProperties;

/**

  • Created by Administrator on 2015/10/10.
    */

public class KafkaConsumer {

private final ConsumerConnector consumer;

private KafkaConsumer() {
    Properties props = new Properties();
    props.put("auto.offset.reset", "smallest");
    //zookeeper 配置
    props.put("zookeeper.connect", "Master:2181");

    //group 代表一个消费组
    props.put("group.id", "consumer3");

    // 连接zk的session超时时间
    props.put("zookeeper.session.timeout.ms", "4000");
    props.put("zookeeper.sync.time.ms", "200");//zk follower落后于zk leader的最长时间        props.put("auto.commit.interval.ms", "1000");//往zookeeper上写offset的频率        props.put("auto.offset.reset", "smallest");//如果offset出了返回,则 smallest: 自动设置reset到最小的offset. largest : 自动设置offset到最大的offset. 其它值不允许,会抛出异常        
    //序列化类 
    props.put("serializer.class", "kafka.serializer.StringEncoder");
    props.put("auto.commit.interval.ms", "1000");
    ConsumerConfig config = new ConsumerConfig(props);

    consumer = kafka.consumer.Consumer.createJavaConsumerConnector(config);
}

void consume() {
    Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
    topicCountMap.put("consumertest", new Integer(1));

    StringDecoder keyDecoder = new StringDecoder(new VerifiableProperties());
    StringDecoder valueDecoder = new StringDecoder(new VerifiableProperties());

    Map<String, List<KafkaStream<String, String>>> consumerMap =
            consumer.createMessageStreams(topicCountMap,keyDecoder,valueDecoder);
    KafkaStream<String, String> stream = consumerMap.get("consumertest").get(0);
    ConsumerIterator<String, String> it = stream.iterator();
    System.out.println("consumer starting...");
    while (it.hasNext())
        System.out.println(it.next().message());
    System.out.println("consumer over");
}

public static void main(String[] args) {
    new KafkaConsumer().consume();
}

}

谢谢~

消息中间件 Kafka
分享到
取消 提交回答
全部回答(0)
微服务
使用钉钉扫一扫加入圈子
+ 订阅

构建可靠、高效、易扩展的技术基石

推荐文章
相似问题