-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer.js
37 lines (28 loc) · 912 Bytes
/
consumer.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
const { Kafka } = require("kafkajs");
const topicName = process.argv[2] || "Log2";
getMessages();
async function getMessages() {
try {
const kafka = new Kafka({
clientId: "kafka_client_1",
brokers: ["10.11.0.96:9092"]
});
const kafkaConsumer = kafka.consumer({
groupId: "consumer_group_1"
});
console.log("Connecting Apache Kafka Consumer...");
await kafkaConsumer.connect();
console.log("Connected to Apache Kafka Consumer");
await kafkaConsumer.subscribe({
topic: topicName,
fromBeginning: true
});
await kafkaConsumer.run({
eachMessage: async result => {
console.log(`Message: ${result.message.value} Partition => ${result.partition}`);
}
});
} catch (error) {
console.log(error.message);
}
}