-
Notifications
You must be signed in to change notification settings - Fork 0
/
kafkaController.js
85 lines (78 loc) · 2.09 KB
/
kafkaController.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
76
77
78
79
80
81
82
83
84
85
import { Kafka } from 'kafkajs'
import dotenv from 'dotenv'
dotenv.config()
class KafkaController {
constructor() {
this.kafka = new Kafka({
clientId: process.env.CLIENT,
brokers: [ process.env.BROKER_1 ]
})
}
/**
* Create topic with multiple partitions
*/
async createTopic (topicName, numPartitions) {
try {
//Create admin client to manage kafka topic
const admin = this.kafka.admin()
await admin.connect()
await admin.createTopics({
topics: [{
topic: topicName.toString(),
numPartitions: parseInt(numPartitions),
replicationFactor: 1
}]
})
await admin.disconnect()
} catch(e) {
console.log(e)
throw e
}
}
/**
* publish message to the topic
*/
async publishMessage(topicName, messages) {
const producer = this.kafka.producer()
try {
await producer.connect()
await producer.send({
topic: topicName,
messages
})
} catch(e) {
console.log(e)
throw e
} finally {
producer.disconnect()
}
}
/**
* Consume message from the topic
*/
async consumeMessage(topicName, callback) {
const consumer = this.kafka.consumer({groupId: 'my-group'})
try {
await consumer.connect()
await consumer.subscribe({
topic: topicName,
fromBeginning: true
})
await consumer.run({
eachMessage: async ({
topic,
partition,
message
}) => {
const value = `Received message: ${message.value.toLocaleString()}
from partition ${partition} $ topic ${topic} `;
callback(value)
}
})
} catch(e) {
console.log(e)
throw e
}
}
}
export default KafkaController