0

I have a producer which push meassages to Kafa topic using REST API. Now how can I have a consumer which can consume those meassges using REST API.

I have tried using @GetMapping but it did not worked

Sravani Ps
  • 1
  • 1
  • 2
  • https://stackoverflow.com/questions/53982971/jsonparseexception-reading-data-using-kafka-rest-api – ZAKARIA ABOUD Jul 17 '19 at 16:56
  • I assumer you're using Spring. What do you mean exactly by consuming by REST API ? Kafka is meant to be stream oriented. You would like to have some kind of REST endpoint that delivers a stream to you ? It reminds me this conversation : https://stackoverflow.com/questions/4831322/how-do-streaming-resources-fit-within-the-restful-paradigm Could you share more details about what you're trying to achieve ? Yannick – Yannick Jul 17 '19 at 21:46

1 Answers1

1

Confluent platform has REST proxy which exposes Kafka topics over REST. It allows you to use REST to produce consume data to a topic. See example here

A sample for consuming json data from a topic

Create a consumer for JSON data, starting at the beginning of the topic's log and subscribe to a topic. Then consume some data using the base URL in the first response. Finally, close the consumer with a DELETE to make it leave the group and clean up its resources.

$ curl -X POST -H "Content-Type: application/vnd.kafka.v2+json" \
          --data '{"name": "my_consumer_instance", "format": "json", "auto.offset.reset": "earliest"}' \
          http://localhost:8082/consumers/my_json_consumer
      {"instance_id":"my_consumer_instance",
      "base_uri":"http://localhost:8082/consumers/my_json_consumer/instances/my_consumer_instance"}

$ curl -X POST -H "Content-Type: application/vnd.kafka.v2+json" --data '{"topics":["jsontest"]}' \
     http://localhost:8082/consumers/my_json_consumer/instances/my_consumer_instance/subscription


$ curl -X GET -H "Accept: application/vnd.kafka.json.v2+json" \
          http://localhost:8082/consumers/my_json_consumer/instances/my_consumer_instance/records
      [{"key":null,"value":{"foo":"bar"},"partition":0,"offset":0,"topic":"jsontest"}]

$ curl -X DELETE -H "Content-Type: application/vnd.kafka.v2+json" \
          http://localhost:8082/consumers/my_json_consumer/instances/my_consumer_instance
asolanki
  • 1,333
  • 11
  • 18