-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
62 lines (49 loc) · 1.44 KB
/
index.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
require('dotenv').config()
const Kafka = require("node-rdkafka");
const GoogleSheets = require('./google-sheets');
const kafkaConf = {
'group.id': process.env.CLOUDKARAFKA_GROUP_ID,
'metadata.broker.list': process.env.CLOUDKARAFKA_BROKERS.split(","),
'socket.keepalive.enable': true,
'security.protocol': 'SASL_SSL',
'sasl.mechanisms': 'SCRAM-SHA-256',
'sasl.username': process.env.CLOUDKARAFKA_USERNAME,
'sasl.password': process.env.CLOUDKARAFKA_PASSWORD,
debug: 'generic,broker,security'
};
const prefix = process.env.CLOUDKARAFKA_TOPIC_PREFIX;
const topics = [`${prefix}-default`];
const consumer = new Kafka.KafkaConsumer(kafkaConf, {
'auto.offset.reset': 'beginning',
});
const numMessages = 5;
let counter = 0;
consumer.on('error', (err) => {
console.log(err)
})
consumer.on('ready', (arg) => {
console.log(`Consumer ${arg.name} - ${topics} ready`);
consumer.subscribe(topics);
consumer.consume();
})
consumer.on('data', (m) => {
counter++;
const value = m.value.toString();
const parsed = JSON.parse(value);
GoogleSheets.writeToSheets(m.key.toString(), parsed.feedback, parsed.username);
console.log(m.value.toString())
})
consumer.on("disconnected", function(arg) {
process.exit();
});
consumer.on('event.error', function(err) {
console.error(err);
process.exit(1);
});
consumer.on('event.log', function(log) {
// console.log(log);
});
consumer.connect();
process.on('exit', () => {
consumer.disconnect();
})