-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.js
60 lines (51 loc) · 1.58 KB
/
admin.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
process.env.NODE_ENV = process.env.NODE_ENV || 'dev';
const Kafka = require("node-rdkafka");
const Config = require('config')
const kafkaConf = {
"client.id": "cloudkarafka-client-id",
"metadata.broker.list": Config.CLOUDKARAFKA_BROKERS.split(","),
"socket.keepalive.enable": true,
"security.protocol": "SASL_SSL",
"sasl.mechanisms": "SCRAM-SHA-256",
"sasl.username": Config.CLOUDKARAFKA_USERNAME,
"sasl.password": Config.CLOUDKARAFKA_PASSWORD,
"debug": Config.DEBUG
};
const p = new Kafka.Producer(kafkaConf);
p.on('ready', function (arg) {
p.getMetadata({}, (err, data) => {
if (data) {
let current_topics = [];
data.topics.forEach((o) => {
console.log(o.name);
current_topics.push(o.name);
//console.log(o.partitions)
})
console.log('backup of current topics:', current_topics.join("','"));
p.disconnect()
}
})
});
p.connect();
const create_topics = Config.TOPIC.CREATE_LIST; // list of topic to be created
const delete_topics = Config.TOPIC.DELETE_LIST; // list of topic to be deleted
const client = Kafka.AdminClient.create(kafkaConf);
create_topics.forEach((topic) => {
client.createTopic({
topic,
num_partitions: Config.TOPIC.PARTITIONS,
replication_factor: Config.TOPIC.REPLICATION
}, function (err) {
if (err) {
console.log(`Error occured while creating topic ${topic} :`, err)
}
})
});
delete_topics.forEach((topic) => {
client.deleteTopic(topic, function (err) {
if (err) {
console.log(`Error occured while deleting topic ${topic} :`, err)
}
})
});
client.disconnect()