How to consume messages from the kafka broker using java?

                                   How to consume messages from the Kafka broker using java
  • First, create the properties class object for loading the Kafka properties.
  • After Connection creation of Kafka Consumer use the add and subscribe methods get the messages from the mediator.
  • Finally, Close the Kafka consumer connection.
Kafka Consumer getting messages from zookeeper

import java.util.ArrayList; import java.util.List; import java.util.Properties; import org.apache.kafka.clients.consumer.ConsumerRecords; import org.apache.kafka.clients.consumer.KafkaConsumer; /** * * @author:Janardhan Randhi * @Date Oct 27, 2018 * Class:This class Receives(consume)the messages from the kafka broker. */ public class SimpleHLConsumer { public static void main(String[] args) { Properties properties = new Properties(); properties.put("bootstrap.servers", "localhost:9092"); properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); properties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); properties.put("", "test-group"); KafkaConsumer kafkaConsumer = new KafkaConsumer(properties); List topics = new ArrayList(); topics.add("janardhan"); kafkaConsumer.subscribe(topics); try{ while (true){ ConsumerRecords records = kafkaConsumer.poll(10); for (Object record: records){ System.out.println(record); } } }catch (Exception e){ System.out.println(e.getMessage()); }finally { kafkaConsumer.close(); } } }


I’m the Founder of I am a Professional Blogger, Application developer, YouTuber. I’ve been blogging since 2015.I spend a lot of time learning new techniques and actively help other people learn web development through a variety of help groups and writing web development tutorials for my website and blog about advancements in web design and development.Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie or planning a trip to someplace I've never been before.

1 Response to "How to consume messages from the kafka broker using java?"

  1. 카지노사이트_
    안전 바카라사이트_
    바카라사이트 검증_