kafka低级消费者api读取消息不完整

克里斯蒂安 发表于: 2016-07-25   最后更新时间: 2016-07-25 23:03:45   4,745 游览

301个数字放入3个分区,旧的高级消费者api来的,读取的时候总是会跳过两个offset来读,是这样的:
C0:1,10,19,...,289
c1:2,11,20,...,290
C2:3,12,21,...,291
我看了下offset,每个消费者读取的时候,总是跳过两个offset来读,不知道为什么。
这样读的话,总是有些数据就读取不到了,请问这是什么情况?要怎么解决?

发表于 2016-07-25

错了,是这样
分区0 :1,10,19,...,289
分区1 :2,11,20,...,290
分区2 :3,12,21,...,291

跳2个offset?没看懂你的意思。
你这301条数据,正常的话会被你的3个consumer平分,一人拿到100条,还有一个拿到101条。
你可以关注下这篇文章,基础知识:https://www.orchome.com/5

你说的对,但是读取的时候,是跳着读取的,没有一个接一个读取,分区0读了1,接着就读10了,分区1读了2,接着就读了11,分区2读了3,接着就读了12,这样中间的456789都没读

那你的消息少了吗?

少了,中间那些信息没读出来

检查下你客户端,kafka取消息是按块取的(一次取多个)

consumer的客户端是0.8高级api,主要代码就是 
 Map<String, Integer> topicCountMap = new HashMap<String, Integer>();

        topicCountMap.put(KafkaProducer.TOPIC, new Integer(1));

    Map&lt;String, List&lt;KafkaStream&lt;byte[],byte[]&gt;&gt;&gt; consumerMap = 
            consumer.createMessageStreams(topicCountMap);
    KafkaStream&lt;<span style="color: inherit; font-size: 1em; white-space: inherit; line-height: 1.3; background-color: initial;">byte[],byte[]</span><span style="color: inherit; font-size: 1em; white-space: inherit; line-height: 1.3; background-color: initial;">&gt; stream = consumerMap.get(KafkaProducer.TOPIC).get(</span><span class="hljs-number" style="font-size: 1em; white-space: inherit; line-height: 1.3; box-sizing: border-box; color: rgb(42, 161, 152); background-color: initial;">0</span><span style="color: inherit; font-size: 1em; white-space: inherit; line-height: 1.3; background-color: initial;">);</span><br>        ConsumerIterator&lt;<span style="color: inherit; font-size: 1em; white-space: inherit; line-height: 1.3; background-color: initial;">byte[],byte[]</span><span style="color: inherit; font-size: 1em; white-space: inherit; line-height: 1.3; background-color: initial;">&gt; it = stream.iterator();</span><br>        <span class="hljs-keyword" style="box-sizing: border-box; color: rgb(133, 153, 0);">while</span> (it.hasNext())
        System.out.println(it.next().message());
}</code></pre><br/>

你直接运行demo会丢消息么?

在producer发送时输出一下,都有的,而且用shell查看也是对的,两个分区100,另一个101,就是读的时候,加入offset读了20,那么接下来offset就是23,中间隔了两个,每个分区都是这样

我的意思是你直接拿demo运行,看看消息会不会丢,因为我不清楚你对demo做了哪些改动。

我发现了,在
while (it.hasNext()){
System.out.println(it.next().message());
}
如果这样输出是对的,如果System.out.println(it.next().message()+it.next().offset());加上offset后,输出就会跳过一些内容,这是怎么回事?

it.next(),相当于读取一次,it.next相当于又读取一次。

哦哦,原来这样的,谢谢

鹤鸣 -> 半兽人 6年前

您好,我也碰到了这种情况,我写的跟demo一样,但还是跳着消费的。不知道错在了哪儿

你的答案

查看kafka相关的其他问题或提一个您自己的问题