You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.0 KiB
40 lines
1.0 KiB
'use strict'; |
|
|
|
var kafka = require('..'); |
|
var Consumer = kafka.Consumer; |
|
var Offset = kafka.Offset; |
|
var Client = kafka.Client; |
|
var argv = require('optimist').argv; |
|
var topic = argv.topic || 'topic1'; |
|
|
|
var client = new Client('localhost:2181'); |
|
var topics = [ |
|
{topic: topic, partition: 1}, |
|
{topic: topic, partition: 0} |
|
]; |
|
var options = { autoCommit: false, fetchMaxWaitMs: 1000, fetchMaxBytes: 1024 * 1024 }; |
|
|
|
var consumer = new Consumer(client, topics, options); |
|
var offset = new Offset(client); |
|
|
|
consumer.on('message', function (message) { |
|
console.log(message); |
|
}); |
|
|
|
consumer.on('error', function (err) { |
|
console.log('error', err); |
|
}); |
|
|
|
/* |
|
* If consumer get `offsetOutOfRange` event, fetch data from the smallest(oldest) offset |
|
*/ |
|
consumer.on('offsetOutOfRange', function (topic) { |
|
topic.maxNum = 2; |
|
offset.fetch([topic], function (err, offsets) { |
|
if (err) { |
|
return console.error(err); |
|
} |
|
var min = Math.min(offsets[topic.topic][topic.partition]); |
|
consumer.setOffset(topic.topic, topic.partition, min); |
|
}); |
|
});
|
|
|