如何使用 confluent-kafka-python 删除主题

fre*_*czj 1 python apache-kafka confluent-platform

我正在使用 Kafka 使多个微服务相互通信。服务是用 Python 编写的,我使用 Confluent 库来处理 Kafka。在某些时候,我知道某些主题刚刚“结束”,因此我可以自动清理它们。

由于 Confluent 库,有没有办法删除“主题”?我找不到任何关于此的文档...

谢谢

小智 5

您可以使用融合的 Admin Api 来删除主题

例子

接受一个 AdminClient 实例和一个主题列表

def example_delete_topics(a, topics):
    """ delete topics """

    # Call delete_topics to asynchronously delete topics, a future is returned.
    # By default this operation on the broker returns immediately while
    # topics are deleted in the background. But here we give it some time (30s)
    # to propagate in the cluster before returning.
    #
    # Returns a dict of <topic,future>.
    fs = a.delete_topics(topics, operation_timeout=30)

    # Wait for operation to finish.
    for topic, f in fs.items():
        try:
            f.result()  # The result itself is None
            print("Topic {} deleted".format(topic))
        except Exception as e:
            print("Failed to delete topic {}: {}".format(topic, e))
Run Code Online (Sandbox Code Playgroud)