Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Apache Kafka KafkaAdminClient CreateTopics

From Leeroopedia


Knowledge Sources
Domains Administration, Data_Management
Last Updated 2026-02-09 12:00 GMT

Overview

Concrete tool for creating Kafka topics via the AdminClient API provided by KafkaAdminClient.

Description

The KafkaAdminClient.createTopics method sends a CreateTopicsRequest to the Kafka controller to create one or more topics. It validates topic names, converts NewTopic objects to protocol requests, handles quota throttling with configurable retry, and returns futures that complete when the controller responds.

Usage

Use via kafka-topics.sh --create or programmatically through the Admin interface when creating topics from application code.

Code Reference

Source Location

  • Repository: Apache Kafka
  • File: clients/src/main/java/org/apache/kafka/clients/admin/KafkaAdminClient.java
  • Lines: L1770-1895

Signature

public CreateTopicsResult createTopics(
    final Collection<NewTopic> newTopics,
    final CreateTopicsOptions options
)

Import

import org.apache.kafka.clients.admin.Admin;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.clients.admin.CreateTopicsResult;

I/O Contract

Inputs

Name Type Required Description
newTopics Collection<NewTopic> Yes Topics to create with partitions, RF, and configs
options CreateTopicsOptions No Options including retryOnQuotaViolation, validateOnly

Outputs

Name Type Description
CreateTopicsResult CreateTopicsResult Contains KafkaFuture per topic with TopicMetadataAndConfig (topic ID, partition count, RF, config)

Usage Examples

Create Topic via CLI

bin/kafka-topics.sh --bootstrap-server localhost:9092 \
  --create --topic my-topic \
  --partitions 6 --replication-factor 3 \
  --config retention.ms=86400000

Create Topic Programmatically

import org.apache.kafka.clients.admin.Admin;
import org.apache.kafka.clients.admin.NewTopic;
import java.util.Collections;
import java.util.Properties;

Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");

try (Admin admin = Admin.create(props)) {
    NewTopic topic = new NewTopic("my-topic", 6, (short) 3);
    topic.configs(Map.of("retention.ms", "86400000"));
    admin.createTopics(Collections.singleton(topic)).all().get();
}

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment