How to create Kafka consumer API

0 votes

Hi,

Can anyone tell me how to create kafka consumer API? How to write consumer API code?

Thank You

Feb 11, 2020 in Apache Kafka by akhtar
• 38,170 points
417 views

1 answer to this question.

0 votes

Hi@akhtar,

If you know the kafka producer API code than it is very simple to write kafka consumer API.

Please go through the below link for kafka producer API before kafka consumer API code.

$ https://www.edureka.co/community/63581/how-to-create-kafka-producer-api

 The kafka consumer API code is given below.

import java.util.*;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.ConsumerRecord;
public class SupplierConsumer{
        public static void main(String[] args) throws Exception{
                String topicName = "SupplierTopic";
                String groupName = "SupplierTopicGroup";
                Properties props = new Properties();
                props.put("bootstrap.servers", "localhost:9092,localhost:9093");
                props.put("group.id", groupName);
                props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
                props.put("value.deserializer", "SupplierDeserializer");
                KafkaConsumer<String, Supplier> consumer = new KafkaConsumer<>(props);
                consumer.subscribe(Arrays.asList(topicName));
                while (true){
                        ConsumerRecords<String, Supplier> records = consumer.poll(100);
                        for (ConsumerRecord<String, Supplier> record : records){
                                System.out.println("Supplier id= " + String.valueOf(record.value().getID()) + " Supplier  Name = " + record.value().getName() + " Supplier Start Date = " + record.value().getStartDate().toString());
                        }
                }
        }
}

Hope this will help you.

Thank You

answered Feb 11, 2020 by MD
• 95,160 points

Related Questions In Apache Kafka

0 votes
1 answer

How to write Kafka connector to integrate with Facebook API?

If you know the limit of API ...READ MORE

answered Apr 29, 2019 in Apache Kafka by Kumar
1,162 views
0 votes
1 answer

How to create a Kafka topic in a broker?

Hi@akhtar, To create a Kafka topic, you need ...READ MORE

answered Dec 7, 2020 in Apache Kafka by MD
• 95,160 points
103 views
0 votes
1 answer

How to create a Kafka producer?

Hi@MD, Generally, Kafka producer comes from an API. ...READ MORE

answered Dec 7, 2020 in Apache Kafka by akhtar
• 38,170 points
64 views
0 votes
1 answer

How to start a consumer in Kafka cluster?

Hi@akhtar, In Kafka, each topic is divided into a ...READ MORE

answered Dec 7, 2020 in Apache Kafka by MD
• 95,160 points
66 views
0 votes
14 answers

How can I delete all the messages from a Kafka topic?

To delete all the messages from a Kafka topic.  There are many approach ...READ MORE

answered Jun 19, 2020 in Apache Kafka by Pawan
• 380 points
157,230 views
+1 vote
0 answers

Want to delete consumed topic programatically

directly delete kafka log folder and zeekeeper ...READ MORE

Jun 4, 2019 in Apache Kafka by sarvesh
427 views
0 votes
2 answers

How can I create multiple consumers in apache kafka?

First of all, Consumer is not thread ...READ MORE

answered Jun 17, 2020 in Apache Kafka by Pawan
• 380 points
1,947 views
0 votes
1 answer

How to know leader information in kafka?

Hi@akhtar, To know leader information you can use ...READ MORE

answered Feb 10, 2020 in Apache Kafka by MD
• 95,160 points
3,836 views
0 votes
1 answer

How to create kafka producer API?

Hi@akhtar, To create kafka producer API, you can ...READ MORE

answered Feb 11, 2020 in Apache Kafka by MD
• 95,160 points
183 views
0 votes
1 answer

How can i connect to an api which gives me weather data to Apache kafka?

Hi, To connect an API in kafka, you ...READ MORE

answered Apr 13, 2020 in Apache Kafka by MD
• 95,160 points
332 views