-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (57 loc) · 1.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const { Kafka } = require('kafkajs');
const util = require('util');
// - intantiating kafkajs client
const kafka = new Kafka({
clientId: 'anuj',
brokers: ['localhost:9092'],
});
async function listTopics() {
const admin = kafka.admin();
admin.connect();
const aTopics = await admin.listTopics();
console.log(`Topics :: ${aTopics}`);
admin.disconnect();
}
async function createTopics(aTopic) {
const admin = kafka.admin();
admin.connect();
const bIsCreated = await admin.createTopics({
validateOnly: false,
waitForLeaders: true,
timeout: 5000,
topics: aTopic,
});
console.log(`Topics creation :: ${bIsCreated}`);
admin.disconnect();
}
async function describeTopics(aTopic = []) {
const admin = kafka.admin();
admin.connect();
const aTopicMetaData = await admin.fetchTopicMetadata({ topics: aTopic });
console.log(`Describe Topics: ${util.inspect(aTopicMetaData, { depth: null, colors: true })}`);
admin.disconnect();
}
async function deleteTopics(aTopic = []) {
const admin = kafka.admin();
admin.connect();
await admin.deleteTopics({ topics: aTopic });
admin.disconnect();
}
async function test() {
/* uncomment to create topic */
// await createTopics([
// {
// topic: 'topic_1',
// },
// {
// topic: 'topic_2',
// },
// ]);
/* uncomment to describe topic */
// await describeTopics();
/* uncomment to delete topic */
// await deleteTopics(['topic_1']);
/* uncomment to list topic */
// await listTopics();
}
test();