简体   繁体   English

使用JAVA的Kafka Consumer

[英]Kafka Consumer with JAVA

I have a single Kafka-Broker with multiple topics each having a single partition. 我有一个Kafka-Broker,其中有多个主题,每个主题都有一个分区。

I have a consumer that works just fine consuming the messages from the topic 我有一个消费者,可以很好地使用主题中的消息

My problem is I need to improve the through put of the message queue by increasing the number of partitions, say I have four partitions on a topic, is there a way that i can write four consumers each pointed to individual partition on the topic??? 我的问题是我需要通过增加分区数来提高消息队列的吞吐量,比如说我在一个主题上有四个分区,有没有办法我可以写四个消费者,每个消费者指向该主题上的单个分区? ?

import java.util.*;
import kafka.consumer.Consumer;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector;

public class KafkaConsumer {
   private ConsumerConnector consumerConnector = null;
   private final String topic = "mytopic";

   public void initialize() {
         Properties props = new Properties();
         props.put("zookeeper.connect", "localhost:2181");
         props.put("group.id", "testgroup");
         props.put("zookeeper.session.timeout.ms", "400");
         props.put("zookeeper.sync.time.ms", "300");
         props.put("auto.commit.interval.ms", "1000");
         ConsumerConfig conConfig = new ConsumerConfig(props);
         consumerConnector = Consumer.createJavaConsumerConnector(conConfig);
   }

   public void consume() {
         //Key = topic name, Value = No. of threads for topic
         Map<String, Integer> topicCount = new HashMap<String, Integer>();       
         topicCount.put(topic, new Integer(1));

         //ConsumerConnector creates the message stream for each topic
         Map<String, List<KafkaStream<byte[], byte[]>>> consumerStreams =
               consumerConnector.createMessageStreams(topicCount);         

         // Get Kafka stream for topic 'mytopic'
         List<KafkaStream<byte[], byte[]>> kStreamList =
                                              consumerStreams.get(topic);
         // Iterate stream using ConsumerIterator
         for (final KafkaStream<byte[], byte[]> kStreams : kStreamList) {
                ConsumerIterator<byte[], byte[]> consumerIte = kStreams.iterator();

                while (consumerIte.hasNext())
                       System.out.println("Message consumed from topic
                                     [" + topic + "] : "       +
                                       new String(consumerIte.next().message()));              
         }
         //Shutdown the consumer connector
         if (consumerConnector != null)   consumerConnector.shutdown();          
   }

   public static void main(String[] args) throws InterruptedException {
         KafkaConsumer kafkaConsumer = new KafkaConsumer();
         // Configure Kafka consumer
         kafkaConsumer.initialize();
         // Start consumption
         kafkaConsumer.consume();
   }

} }

Essentially, all you need to do is start several consumers that are all in the same consumer group. 本质上,您需要做的就是启动几个都属于同一消费者组的消费者。 If you are using the new consumer from kafka 0.9 or later, or if you are using the high-level consumer, kafka will take care of dividing up the partitions making sure each partition is read by one consumer. 如果您使用的是来自kafka 0.9或更高版本的新使用者,或者您使用的是高级使用者,则kafka将负责划分分区,以确保每个使用者都能读取每个分区。 If you have more partitions than consumers, then some consumers will receive messages from multiple partitions, but no partition will ever be read by more than one consumer from the same consumer group to ensure messages are not duplicated. 如果您的分区比使用者更多,则某些使用者将从多个分区接收消息,但是同一消费者组中的一个以上的使用者不会读取任何分区,以确保不重复消息。 So you never want more consumers than partitions, since some consumers will be idle. 因此,您永远不希望分区使用更多的使用者,因为某些使用者将处于空闲状态。 You can also fine tune which consumer reads each partition using the simple consumer https://cwiki.apache.org/confluence/display/KAFKA/0.8.0+SimpleConsumer+Example 您还可以使用简单的使用者https://cwiki.apache.org/confluence/display/KAFKA/0.8.0+SimpleConsumer+Example来微调哪个使用者读取每个分区。

It seems you are using the old consumer from Kafka 0.8 or before. 看来您使用的是Kafka 0.8或更低版本的老消费者。 You may want to consider switching to the new consumer. 您可能要考虑切换到新使用者。 http://kafka.apache.org/documentation.html#intro_consumers http://kafka.apache.org/documentation.html#intro_consumers

Here is another good article with detailed examples of writing consumers using the new consumer: http://www.confluent.io/blog/tutorial-getting-started-with-the-new-apache-kafka-0-9-consumer-client/ 这是另一篇很好的文章,其中包含使用新使用者编写使用者的详细示例: http : //www.confluent.io/blog/tutorial-getting-started-with-the-new-apache-kafka-0-9-consumer-客户/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM