-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproducer.js
47 lines (42 loc) · 1.23 KB
/
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
44
45
46
47
var kafka = require('kafka-node');
var HighLevelProducer = kafka.HighLevelProducer;
var KeyedMessage = kafka.KeyedMessage;
var Client = kafka.Client;
var client = new Client('localhost:2181', 'my-client-id', {
sessionTimeout: 300,
spinDelay: 100,
retries: 2
});
// For this demo we just log client errors to the console.
client.on('error', function(error) {
console.error(error);
});
var producer = new HighLevelProducer(client);
producer.on('ready', function() {
// Create message and encode to Avro buffer
var messageBuffer = type.toBuffer({
enumField: 'sym1',
id: '3e0c63c4-956a-4378-8a6d-2de636d191de',
timestamp: Date.now()
});
// Create a new payload
var payload = [{
topic: 'node-test',
messages: messageBuffer,
attributes: 1 /* Use GZip compression for the payload */
}];
//Send payload to Kafka and log result/error
producer.send(payload, function(error, result) {
console.info('Sent payload to Kafka: ', payload);
if (error) {
console.error(error);
} else {
var formattedResult = result[0];
console.log('result: ', result)
}
});
});
// For this demo we just log producer errors to the console.
producer.on('error', function(error) {
console.error(error);
});