-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.js
52 lines (48 loc) · 1.22 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var typeDescription = {
name: 'MyAwesomeType',
type: 'record',
fields: [{
name: 'enumField',
type: {
name: 'EnumField',
type: 'enum',
symbols: ['sym1', 'sym2', 'sym3']
}
}, {
name: 'id',
type: 'string'
}, {
name: 'timestamp',
type: 'double'
}]
};
var avro = require('avsc');
var type = avro.parse(typeDescription);
var kafka = require('kafka-node');
var HighLevelConsumer = kafka.HighLevelConsumer;
var Client = kafka.Client;
var client = new Client('localhost:2181');
var topics = [{
topic: 'node-test',
topic: 'Group'
}];
var options = {
autoCommit: false,
fetchMaxWaitMs: 1000,
fetchMaxBytes: 1024 * 1024,
encoding: 'buffer'
};
var consumer = new HighLevelConsumer(client, topics, options);
consumer.on('message', function(message) {
var buf = new Buffer(message.value, 'binary'); // Read string into a buffer.
// var decodedMessage = type.fromBuffer(buf.slice(0)); // Skip prefix.
console.log(buf.toString());
});
consumer.on('error', function(err) {
console.log('error', err);
});
process.on('SIGINT', function() {
consumer.close(true, function() {
process.exit();
});
});