8

I am beginner with apache kafka and trying to learn confluent - kafka - rest - utils, however I am confusing with how can I use it.

During search I found this documentation https://spring.io/blog/2015/04/15/using-apache-kafka-for-integration-and-data-processing-pipelines-with-spring

It is very good documentation but It doesn't help me to learn rest-utils.

The git code of confluent kafka rest utils is

https://github.com/confluentinc/kafka-rest is demonstrate how to use rest kafka. But I want to know the exact procedure to be more aware about it. with some simple explanation. Can anyone suggest me some links with how I use rest client. Please guide me towards this.

It might be a silly question but I don't have other option to learn.

thanks in advance.

Jimmy
  • 1,719
  • 3
  • 21
  • 33

2 Answers2

2

First try to create a REST service using spring MVC keeping kafka stuff aside.

Once you are able run a 'hello world' kind of REST service, then pick Kafka Docs.

Refer documents on how to create a kafka cluster, and run default console consumer and producer programs to check your cluster.

Now, write a main java program and create a kafka producer using Kafka Clients API. Refer its docs. Make sure that messages sent through main program are reaching to the consumer.

Now, inject the content of this main program in rest service, so that messages passed in request body are now passed to kafka cluster and are readable by consumer.

Hope it helps.

Deepak Bansal
  • 129
  • 2
  • 12
0

First, create a Property.java to set configuration and make sure you mark it as @Component

 private static final String TOPIC = "Kafka_Example";

public Properties settingProperties() {
    Properties props = new Properties();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    props.put("topic",TOPIC);
    return props;
}

public Property() {
}

public void sendMessage(String msg) {

    KafkaProducer<String, String> producer =
            new KafkaProducer<String, String>(settingProperties());

    ProducerRecord<String, String> record =
            new ProducerRecord<String, String>(settingProperties().getProperty("topic"),
            msg);
    producer.send(record);

    producer.close();

}

Second, In you Controller Class

@Autowired
private Property property;

Now, at last, you can make your own method

@GetMapping("/publish/{name}")
public String post(@PathVariable("name") final String name) {

    property.sendMessage(name);

    return "Published successfully";
}
  • Make Sure your TOPIC name is correct in my case its Kafka_Example

Here are commands that you have to run to set up things

  • Terminal 1 -To Run Zookerper : bin/zookeeper-server-start.sh config/zookeeper.properties
  • Terminal 2 - To Run Kafka Server : bin/kafka-server-start.sh config/server.properties
  • Terminal 3 - To create TOPIC : bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic your-topic-name
  • Terminal 3 - Consuming Via Console : bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic your-topic name --from-beginning

Now, You good to go http://localhost:8080/api/publish/<Your-name>

My Pom Dependencies

    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-clients</artifactId>
        <version>2.5.0</version>
    </dependency>

Add above dependency to use Producer API and Consumer API

Refer Docs

Give a thumbs up If you find it useful. Thanks for your valuable time. If you have any doubt please leave a comment below.

kkrkuldeep
  • 83
  • 5