To check a Kafka topic

To check a Kafka topic, you can use the Kafka command-line tools or programmatically access the topic using a client library. Below are methods for checking Kafka topics.


1. Using Kafka Command-Line Tools

Kafka includes a built-in script kafka-topics.sh to manage and check topics.

Check All Topics:

Run the following command to list all topics:

kafka-topics.sh --bootstrap-server <broker_host>:<broker_port> --list

Example:

kafka-topics.sh --bootstrap-server localhost:9092 --list

Describe a Specific Topic:

To get details about a specific topic, including partition count, replication factor, and partition leader:

kafka-topics.sh --bootstrap-server <broker_host>:<broker_port> --describe --topic <topic_name>

Example:

kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic test-topic

Output example:

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

2. Programmatic Approach

If you prefer to programmatically check a topic, you can use Kafka client libraries. Here’s an example in Node.js using kafkajs.

Install kafkajs:

npm install kafkajs

Example Code:

const { Kafka } = require('kafkajs');

const kafka = new Kafka({
  clientId: 'my-app',
  brokers: ['localhost:9092'], // Replace with your Kafka broker addresses
});

const checkTopic = async (topicName) => {
  const admin = kafka.admin();
  await admin.connect();

  // List all topics
  const topics = await admin.listTopics();
  console.log('Available topics:', topics);

  // Describe the specific topic
  if (topics.includes(topicName)) {
    const metadata = await admin.fetchTopicMetadata({ topics: [topicName] });
    console.log(`Metadata for topic "${topicName}":`, metadata);
  } else {
    console.log(`Topic "${topicName}" does not exist.`);
  }

  await admin.disconnect();
};

checkTopic('test-topic').catch(console.error);

3. Using Kafka UI Tools

Several tools provide a GUI to check Kafka topics:

  • Conduktor: A powerful Kafka GUI client for managing topics, consumers, and producers.
  • AKHQ: A lightweight, open-source web-based tool for viewing and managing Kafka clusters.
  • Kafka Manager: A web-based tool for administering Kafka topics and clusters.

Key Points

  • Kafka CLI is best for quick checks.
  • Programmatic access is ideal for integration or automation.
  • GUI tools offer a user-friendly way to explore and monitor topics.

댓글

이 블로그의 인기 게시물

To switch to a specific tag in a Git repository

How to checkout branch of remote git, 깃 리모트 브랜치 체크아웃

Using the MinIO API via curl

To download a file from MinIO using Spring Boot, 스프링부트 Minio 사용하기

리눅스의 부팅과정 (프로세스, 서비스 관리)

Chromium 개발 환경 세팅, 크로미움 개발 준비하기

Joining an additional control plane node to an existing Kubernetes cluster

urllib3 with proxy settings

CDPEvents in puppeteer

Avro + Grpc in python