简体   繁体   English

用N个分区创建Kafka主题

[英]Create Kafka topic with N partitions

I'm using Confluent Kafka DotNet lib to create and produce to topics: 我正在使用Confluent Kafka DotNet lib创建和生成主题:

producer.ProduceAsync(topic, key, message);

When this topic doesn't exist, the topic is automatically created with one partition. 当该主题不存在时,将自动使用一个分区创建该主题。

But for testing purposes, I want to be able to create a topic with N partitions programmatically. 但是出于测试目的,我希望能够以编程方式创建一个包含N个分区的主题。

I'm unable to find any examples here (branch 0.11.5), how can I create a topic with multiple partitions? 我在这里找不到任何示例(分支0.11.5),如何创建具有多个分区的主题? https://github.com/confluentinc/confluent-kafka-dotnet https://github.com/confluentinc/confluent-kafka-dotnet

Since I was already using Docker I found out it was easiest to simply create a test topic (with N partitions) in the docker-compose: 由于我已经在使用Docker,所以我发现最简单的方法是在docker-compose中创建一个测试主题(具有N个分区):

  zookeeper:
    image: wurstmeister/zookeeper
    ports:
      - "2181:2181"

  kafka:
    image: wurstmeister/kafka
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: kafka
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_CREATE_TOPICS: "PartitionsTest:3:1"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    links:
      - zookeeper

Which creates topic "PartitionsTest" with 3 partitions. 这将创建带有3个分区的主题“ PartitionsTest”。

Warning: Your tests may execute before the topic has time to create itself. 警告:您的测试可能在主题有时间创建其之前执行。

Once the Admin feature is available in a release version of the Confluent Kafka library, I'll probably update it to use that approach. 一旦Confluent Kafka库的发行版中提供了Admin功能,我可能会对其进行更新以使用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM