如何在不删除主题的情况下删除/清理 Kafka 排队消息 [英] How do I delete/clean Kafka queued messages without deleting Topic

查看:36
本文介绍了如何在不删除主题的情况下删除/清理 Kafka 排队消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以在不删除Kafka主题的情况下删除队列消息吗?
我想在激活消费者时删除队列消息.

我知道有几种方法:

  1. 重置保留时间

    $ ./bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic MyTopic --config reserved.ms=1000

  2. 删除kafka文件

    $ rm -rf/data/kafka-logs/

解决方案

在 0.11 或更高版本中,您可以运行 bin/kafka-delete-records.sh 命令来标记要删除的消息.>

https://github.com/apache/kafka/blob/trunk/bin/kafka-delete-records.sh

比如发布100条消息

seq 100 |./bin/kafka-console-producer.sh --broker-list localhost:9092 --topic mytest

然后使用新的 kafka-delete-records.sh 删除这 100 条消息中的 90 条命令行工具

./bin/kafka-delete-records.sh --bootstrap-server localhost:9092 --offset-json-file ./offsetfile.json

其中 offsetfile.json 包含

 {"partitions": [{"topic": "mytest", "partition": 0, "offset": 90}], "version":1}

然后从头开始消费消息以验证 100 条消息中的 90 条确实被标记为已删除.

./bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic mytest --from-beginning919293949596979899100

Is there any way to delete queue messages without deleting Kafka topics?
I want to delete queue messages when activating the consumer.

I know there are several ways like:

  1. Resetting retention time

    $ ./bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic MyTopic --config retention.ms=1000

  2. Deleting kafka files

    $ rm -rf /data/kafka-logs/<topic/Partition_name>

解决方案

In 0.11 or higher you can run the bin/kafka-delete-records.sh command to mark messages for deletion.

https://github.com/apache/kafka/blob/trunk/bin/kafka-delete-records.sh

For example, publish 100 messages

seq 100 | ./bin/kafka-console-producer.sh --broker-list localhost:9092 --topic mytest

then delete 90 of those 100 messages with the new kafka-delete-records.sh command line tool

./bin/kafka-delete-records.sh --bootstrap-server localhost:9092 --offset-json-file ./offsetfile.json

where offsetfile.json contains

 {"partitions": [{"topic": "mytest", "partition": 0, "offset": 90}], "version":1 }

and then consume the messages from the beginning to verify that 90 of the 100 messages are indeed marked as deleted.

./bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic mytest --from-beginning
91
92
93
94
95
96
97
98
99
100

这篇关于如何在不删除主题的情况下删除/清理 Kafka 排队消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆