본문 바로가기

카테고리 없음

how to describe kafka topics, 카프카 토픽 정보 확인하기

To describe Kafka topics, you can use the kafka-topics.sh script, which is included in the Kafka distribution, or Kafka Admin APIs to programmatically fetch topic information. Here are the ways to describe Kafka topics:

1. Using kafka-topics.sh Script

The kafka-topics.sh command-line tool allows you to describe topics on the Kafka cluster. You can use the --describe flag to get details like the number of partitions, replicas, leader, and ISR (in-sync replicas).

Command Syntax:

bin/kafka-topics.sh --describe --topic <topic-name> --bootstrap-server <broker-address>

Example:

bin/kafka-topics.sh --describe --topic my-topic --bootstrap-server localhost:9092

Output Example:

Topic: my-topic  PartitionCount: 3  ReplicationFactor: 2  Configs: segment.bytes=1073741824
    Topic: my-topic  Partition: 0  Leader: 1  Replicas: 1,2  Isr: 1,2
    Topic: my-topic  Partition: 1  Leader: 2  Replicas: 2,1  Isr: 2,1
    Topic: my-topic  Partition: 2  Leader: 1  Replicas: 1,2  Isr: 1,2

Explanation:

  • PartitionCount: The number of partitions for the topic.
  • ReplicationFactor: The replication factor for the topic.
  • Leader: The broker currently leading the partition.
  • Replicas: The list of brokers that host replicas of the partition.
  • Isr: The list of in-sync replicas (replicas that are fully caught up).

2. Using kafka-topics.sh for All Topics

If you want to describe all topics in your Kafka cluster, omit the --topic flag:

bin/kafka-topics.sh --describe --bootstrap-server localhost:9092

This will output details for all the topics on the cluster.

3. Using Kafka Admin API (Programmatically)

You can also describe Kafka topics programmatically using the Kafka Admin API in Java. Here's an example that describes a specific Kafka topic:

Maven Dependencies:

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka-clients</artifactId>
    <version>3.0.0</version>
</dependency>

Example Code:

import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.Config;
import org.apache.kafka.clients.admin.ConfigEntry;
import org.apache.kafka.clients.admin.ConfigResource;
import org.apache.kafka.clients.admin.DescribeTopicsResult;
import org.apache.kafka.clients.admin.TopicDescription;
import org.apache.kafka.common.config.ConfigResource.Type;

import java.util.Collections;
import java.util.Properties;
import java.util.concurrent.ExecutionException;

public class DescribeKafkaTopic {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Properties props = new Properties();
        props.put("bootstrap.servers", "localhost:9092");

        // Create AdminClient
        try (AdminClient adminClient = AdminClient.create(props)) {
            // Describe the topic
            DescribeTopicsResult result = adminClient.describeTopics(Collections.singletonList("my-topic"));
            TopicDescription description = result.all().get().get("my-topic");

            System.out.println("Topic name: " + description.name());
            System.out.println("Partitions: " + description.partitions().size());
            System.out.println("Replication factor: " + description.partitions().get(0).replicas().size());

            // Describe topic configs
            ConfigResource resource = new ConfigResource(Type.TOPIC, "my-topic");
            Config config = adminClient.describeConfigs(Collections.singleton(resource)).all().get().get(resource);
            for (ConfigEntry entry : config.entries()) {
                System.out.println(entry.name() + " = " + entry.value());
            }
        }
    }
}

Explanation:

  • This code uses Kafka’s AdminClient to describe a specific topic (my-topic).
  • It fetches the partition information, replication factor, and configurations like segment size, retention, etc.

4. Using Confluent Kafka CLI (if you're using Confluent Platform)

If you're using the Confluent Platform, you can describe topics with the Confluent CLI:

confluent kafka topic describe my-topic --cluster <cluster-id>

Conclusion

  • CLI (kafka-topics.sh): A simple way to describe Kafka topics and view details like partitions, replicas, and leaders.
  • Admin API: A programmatic way to describe Kafka topics and get detailed configurations.