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

5 days ago in Apache Kafka by akhtar
• 1,440 points
12 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 5 days ago by MD
• 2,750 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
452 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,840 points
459 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
9,990 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,370 points
3,755 views
+1 vote
12 answers

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

As of kafka 2.3.0 version, there is ...READ MORE

answered Sep 8, 2019 in Apache Kafka by Brajkishore
• 240 points
61,545 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
196 views
0 votes
1 answer

How can I create multiple consumers in apache kafka?

Hi@akhtar, If you already created multiple producers then ...READ MORE

answered Feb 6 in Apache Kafka by MD
• 2,750 points
17 views
0 votes
1 answer

How to know leader information in kafka?

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

answered 6 days ago in Apache Kafka by MD
• 2,750 points
16 views
0 votes
1 answer

How to create kafka producer API?

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

answered 6 days ago in Apache Kafka by MD
• 2,750 points
13 views
0 votes
1 answer

How can I create a topic in apache kafka?

Hi@akhtar, To create kafka topic you can use ...READ MORE

answered Feb 6 in Apache Kafka by MD
• 2,750 points
24 views