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.
댓글
댓글 쓰기