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("group.id", "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(); } } }

SUBSCRIBE TO OUR NEWSLETTER

I’m the Founder of quickdevops.com. 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. 카지노사이트_ https://casinojoin77.blogspot.com/2021/05/magic-tips-to-win-baccarat-game.html
    온라인카지노_ https://marryann1220.blogspot.com/2021/05/enjoy-amazing-casino-online-and-all-its.html
    바카라사이트_ https://leeshiela699.blogspot.com/2021/05/best-mobile-gaming-genres-to-try-in-2021.html
    온라인바카라_ https://msno0202.blogspot.com/2021/05/want-to-enjoy-both-gambling-and-betting.html
    안전 바카라사이트_ https://dadda7788.blogspot.com/2021/05/the-four-best-podcasts-in-paris-online.html
    바카라사이트 검증_ https://casinojoin77.blogspot.com/2021/05/the-best-casinosite-without-license-in.html

    ReplyDelete