-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer.js
43 lines (36 loc) · 947 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
37
38
39
40
41
42
43
const {Kafka} = require("kafkajs")
const msg = process.argv[2];
run();
async function run(){
try
{
const kafka = new Kafka({
"clientId": "myapp",
"brokers" :["localhost:29092"]
})
const producer = kafka.producer();
console.log("Connecting.....")
await producer.connect()
console.log("Connected!")
//A-M 0 , N-Z 1
const partition = msg[0] < "N" ? 0 : 1;
const result = await producer.send({
"topic": "Users",
"messages": [
{
"value": msg,
"partition": partition
}
]
})
console.log(`Send Successfully! ${JSON.stringify(result)}`)
await producer.disconnect();
}
catch(ex)
{
console.error(`Something bad happened ${ex}`)
}
finally{
process.exit(0);
}
}