def create_topic():
    global topic

    # Create the kafka topic
    tries = 30
    exit = False
    while not exit:
        try:
            try:
                topic = admin.NewTopic(name="slice",
                                       num_partitions=1,
                                       replication_factor=1)
                broker = KafkaAdminClient(bootstrap_servers="kafka:19092")
                broker.create_topics([topic])
            except errors.TopicAlreadyExistsError:
                logger.warning("Topic exists already")
            else:
                logger.info("New topic")
        except errors.NoBrokersAvailable as KafkaError:
            if tries > 0:
                tries -= 1
                logger.warning(
                    "Kafka not ready yet. Tries remaining: {0}".format(tries))
                time.sleep(5)
            else:
                logger.error(KafkaError)
        else:
            exit = True
            tries = 30
Beispiel #2
0
def setup_environment(conf: Config):
    admin = k.KafkaAdminClient(bootstrap_servers=conf["brokers"])

    log.debug("Creating %s topic", conf["topic"])

    try:
        admin.create_topics([
            kadmin.NewTopic(conf["topic"],
                            num_partitions=conf["num_partitions"],
                            replication_factor=1)
        ])
    except Exception as e:
        pass
    admin.close()
import kafka.admin as k_admin

admin_client = k_admin.KafkaAdminClient(bootstrap_servers="localhost:9092")

new_topic = k_admin.NewTopic(name="test2",
                             num_partitions=1,
                             replication_factor=1)

admin_client.create_topics(new_topics=[new_topic])

print(admin_client.list_topics())