-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer.js
36 lines (31 loc) · 868 Bytes
/
producer.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
const Kafka = require("kafkajs").Kafka;
const message = process.argv[2];
run();
async function run() {
try {
const kafka = new Kafka({
clientId: "mynodeapp",
brokers: ["192.168.255.193:9092"],
});
const producer = kafka.producer();
console.log("Connecting Producer.....");
await producer.connect();
console.log("Connected Producer!!!");
const partition = message[0] < "N" ? 0 : 1; // First letter A-M par0, N-Z par1
const result = await producer.send({
topic: "Users",
messages: [
{
value: message,
partition: partition,
},
],
});
console.log(`Send successfully... ${JSON.stringify(result)}`);
await producer.disconnect();
} catch (error) {
console.log(`Some error occured in producer file: ${error}`);
} finally {
process.exit(0);
}
}