如何使用Python confluent_kafka管理员NewTopic * args或** kwargs来微调主题配置

时间:2019-12-01 08:51:40

标签: python apache-kafka confluent-kafka

如何使用Python confluent_kafka管理员NewTopic * args或** kwargs来微调主题配置?

这是我发现的基座字符串:

confluent_kafka.cimpl.NewTopic def __init__(self,
         topic: str,
         num_partitions: int,
         replication_factor: int = None,
         *args: Any,
         **kwargs: Any) -> None
NewTopic specifies per-topic settings for passing to passed to AdminClient.create_topics().
Params:
topic – Topic name
num_partitions – Number of partitions to create
replication_factor – Replication factor of partitions, or -1 if replica_assignment is used.

在源代码中,我找到了c文件代码和文档:

typedef struct {
    PyObject_HEAD
    char *topic;
    int   num_partitions;
    int   replication_factor;
    PyObject *replica_assignment;  /**< list<int> */
    PyObject *config;              /**< dict<str,str> */
} NewTopic;

文档字符串:

    "  :param dict config: Dict (str:str) of topic configuration. See http://kafka.apache.org/documentation.html#topicconfigs\n"
    "  :rtype: NewTopic\n"

1 个答案:

答案 0 :(得分:0)

配置最大消息字节数的示例

import traceback

from confluent_kafka.admin import AdminClient, NewTopic
from confluent_kafka.cimpl import KafkaError

admin_client = AdminClient({"bootstrap.servers": "bootstrap.kafka:9092"})

topic_list = []

# topic: str,
# num_partitions: int,
# replication_factor: int = None,
# *args: Any,
# **kwargs: Any) -> None


replica_assignment = []
topic_config = {"max.message.bytes": 15048576}

topic_list.append(NewTopic(
    topic="example_topic",
    num_partitions=1,
    replication_factor=1,
    # replica_assignment=replica_assignment,
    config=topic_config
)
)

futures = admin_client.create_topics(topic_list)

try:
    record_metadata = []
    for k, future in futures.items():
        # f = i.get(timeout=10)
        print(f"type(k): {type(k)}")
        print(f"type(v): {type(future)}")
        print(future.result())

except KafkaError:
    # Decide what to do if produce request failed...
    print(traceback.format_exc())
    result = 'Fail'
finally:
    print("finally")