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 in Apache Kafka by akhtar
• 28,700 points
292 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 by MD
• 66,460 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
902 views
0 votes
1 answer

Is there any change in consumer offsets if a new partition(s) is added to a Kafka topic?

Yes, it stays the same. An offset is ...READ MORE

answered Jul 9, 2018 in Apache Kafka by nitinrawat895
• 10,950 points
727 views
+1 vote
10 answers

Writing the Kafka consumer output to a file

System.out.println(String.valueOf(output.offset()) + ": " + new String(bytes, ...READ MORE

answered Dec 7, 2018 in Apache Kafka by Harsh
16,755 views
0 votes
1 answer

How to reset the offset of messages consumed from Kafka?

The reset option only prints the result ...READ MORE

answered Jul 9, 2018 in Apache Kafka by Shubham
• 13,450 points
8,443 views
+1 vote
13 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 in Apache Kafka by Pawan
• 380 points
120,585 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
332 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 in Apache Kafka by Pawan
• 380 points
1,228 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 in Apache Kafka by MD
• 66,460 points
1,852 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 in Apache Kafka by MD
• 66,460 points
115 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 in Apache Kafka by MD
• 66,460 points
165 views