Skip to content

Latest commit

 

History

History
83 lines (66 loc) · 2.39 KB

README.md

File metadata and controls

83 lines (66 loc) · 2.39 KB

Kafka Producer Demo

This project demonstrates how to create a Kafka producer in Java and send messages to a Kafka topic.

Prerequisites

  • Java 11 or higher
  • Apache Kafka
  • Maven

Setup

  1. Install Apache Kafka: Follow the Kafka Quickstart guide to install and run Kafka on your local machine.

  2. Create a Kafka Topic: Open a terminal and create a topic named demo_java with the following command:

    kafka-topics --create --topic demo_java --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1

Running the Producer

  1. Clone the repository:

    git clone <repository-url>
    cd <repository-directory>
  2. Build the project:

    mvn clean install
  3. Run the producer:

    mvn exec:java -Dexec.mainClass="code.with.vanilson.produtor.ProducerDemo"

Verifying Message Delivery

To verify that the messages were sent successfully, you can use the Kafka console consumer:

kafka-console-consumer --bootstrap-server localhost:9092 --topic demo_java --from-beginning

Configuration

The producer properties are configured in the ProducerDemo class. Here are the key configurations:

  • bootstrap.servers: The address of the Kafka broker.
  • key.serializer: The serializer class for the key.
  • value.serializer: The serializer class for the value.

Additional Kafka Commands

  1. Reset Offsets
kafka-consumer-groups --reset-offsets --group my-group --topic demo_java --to-earliest --execute --bootstrap-server localhost:9092
  1. Delete Consumer Group
kafka-consumer-groups --delete --group my-group --bootstrap-server localhost:9092
  1. Preferred Replica Election
kafka-preferred-replica-election --bootstrap-server localhost:9092
  1. Set Minimum In-Sync Replica
kafka-configs --alter --entity-type topics --entity-name demo_java --add-config min.insync.replicas=2 --bootstrap-server localhost:9092

Producer Acknowledgements

Acks All:

kafka-console-producer --broker-list localhost:9092 --topic demo_java --producer-property acks=all

Acks 1:

kafka-console-producer --broker-list localhost:9092 --topic demo_java --producer-property acks=1

Acks 0:

kafka-console-producer --broker-list localhost:9092 --topic demo_java --producer-property acks=0