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. PLAY NEW CASINO GAMES ONLINE:

    https://www.omgqq.com 우리카지노사이트
    https://www.omgqq.com/thekingcasino 더킹카지노
    https://www.omgqq.com/sandscasino 샌즈카지노
    https://www.omgqq.com/firstcasino 퍼스트카지노
    https://www.omgqq.com/yescasino 예스카지노
    https://www.omgqq.com/supercaisno 슈퍼카지노
    https://www.omgqq.com/gatsbycasino 개츠비카지노
    https://www.omgqq.com/33casino 33카지노
    https://www.omgqq.com/worldcasino 월드카지노
    https://www.omgqq.com/blog

    ReplyDelete