![](/img/trans.png)
[英]How to determine if a kafka topic exists using confluent-kafka-python
[英]Setting Topic log retention in confluent-kafka-python
我在文档中找不到如何在使用 confluent-kafka 创建生产者时设置保留时间。
如果我只指定“bootstrap-servers”,则默认保留时间为 1 天。 我希望能够改变这一点。
(我想在 python API 中而不是在命令行中执行此操作。)
保留时间在您创建主题时设置,而不是在生产者配置上设置。
如果您的server.properties
允许自动创建主题,那么您将在那里获得默认设置。
否则,您可以使用AdminClient API发送支持dict<str,str>
config
属性的NewTopic
请求
from confluent_kafka.admin import AdminClient, NewTopic
# a = AdminClient(...)
topics = list()
t = NewTopic(topic, num_partitions=3, replication_factor=1, config={'log.retention.hours': '168'})
topics.append(t)
# Call create_topics to asynchronously create topics, a dict
# of <topic,future> is returned.
fs = a.create_topics(topics)
# Wait for operation to finish.
# Timeouts are preferably controlled by passing request_timeout=15.0
# to the create_topics() call.
# All futures will finish at the same time.
for topic, f in fs.items():
try:
f.result() # The result itself is None
print("Topic {} created".format(topic))
except Exception as e:
print("Failed to create topic {}: {}".format(topic, e))
在同一个链接中,您可以找到一个更改主题请求
保留时间不是生产者的财产。 默认保留时间在代理配置文件server.properties和log.retention.hours等属性中设置,例如 /etc/kafka/server.properties ...取决于您的安装。
您可以通过例如更改每个主题库的保留时间
$ <path-to-kafka>/bin/kafka-topics.sh --zookeeper <zookeeper-quorum> --alter --topic <topic-name> --config retention.ms=<your-desired-retention-in-ms>
哈……
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.