29 lines
552 B
JavaScript
29 lines
552 B
JavaScript
|
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();
|