kafka0.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-beginning
91
92
93
94
95
96
97
98
99
100