I want to switch the javascript kafka
library from kafka node
to kafkajs
. I want to find a way to wrap the below code which is the offset module
originally from kafka node. How can I rewrite the whole function
and also the consumer.on("offsetOutOfRange") function
?
Code:
const kafkaClient = new kafka.KafkaClient({kafkaHost: '127.0.0.1'});
const kafka_offset = new kafka.Offset(kafkaClient);
kafka_offset.fetch(
[{ topic: "testTopic", partition: 0, time: -1, maxNum: 1 }],
function(err, kafka_data) {
if (err) {
console.log(err);
return;
}
const latest_offset = kafka_data["testTopic"]['0'][0];
consumer = new kafka.Consumer(
kafkaClient,
[{ topic: "testTopic", partition: 0, offset: latest_offset -1}],
{
groupId: 'testing',
autoCommit: true,
fromOffset: true,
}
);
consumer.on('offsetOutOfRange', obj => {});
consumer.on('error', err => {logger.error('error: ', err);});
consumer.on('message', message => {});
});