kafkajs/create-topic.js

29 lines
552 B
JavaScript
Raw Normal View History

2023-04-12 17:42:55 +00:00
const { Kafka } = require('kafkajs');
const kafka = new Kafka({
clientId: 'my-app',
brokers: ['localhost:9092']
});
const admin = kafka.admin();
async function createTopic() {
try {
await admin.connect();
await admin.createTopics({
topics: [{
topic: 'my-topic-1',
numPartitions: 2,
replicationFactor: 1
}]
});
console.log('Topic created successfully!');
} catch (error) {
console.error('Error creating topic:', error);
} finally {
await admin.disconnect();
}
}
createTopic();