【问题标题】:how to create consumer using REST API to read json data from Kafka topic如何使用 REST API 创建消费者以从 Kafka 主题读取 json 数据
【发布时间】:2019-07-17 16:54:41
【问题描述】:
我有一个生产者,它使用 REST API 将消息推送到 Kafa 主题。现在我怎样才能拥有一个可以使用 REST API 使用这些消息的消费者。
我尝试过使用@GetMapping,但没有成功
【问题讨论】:
标签:
rest
api
apache-kafka
kafka-consumer-api
【解决方案1】:
Confluent 平台具有 REST 代理,可通过 REST 公开 Kafka 主题。它允许您使用 REST 为主题生成消费数据。见例子here
从主题中消费 json 数据的示例
为 JSON 数据创建消费者,从主题的开头开始
登录并订阅主题。然后使用第一个响应中的基本 URL 使用一些数据。
最后,用 DELETE 关闭消费者,使其离开组并清理
它的资源。
$ 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