I am trying to assign a consumer on single partition of a topic using kafkajs client library in TypeScript. I am using partitionAssigners in the consumer group as provided in documentation. I have created my SinglePartitionAssigner but it doesn't seem working.
import {Kafka, PartitionAssigner, EachMessagePayload, AssignerProtocol, KafkaMessage} from 'kafkajs'
import {Buffer} from 'buffer'
const kafka = new Kafka({
clientId: 'my-app',
brokers: ['localhost:9092']
})
const topics = ['testTopic'];
export const SinglePartitionAssigner: PartitionAssigner = () => ({
name: 'SinglePartitionAssigner',
version: 1,
async assign() {
return [
{
memberId: 'what',
memberAssignment: AssignerProtocol.MemberAssignment.encode({
version: this.version,
assignment: {
'testTopic': [0]
},
userData: Buffer.from([]) // no idea what this is for
})
}
]
},
protocol({topics}) {
return {
name: this.name,
metadata: AssignerProtocol.MemberMetadata.encode({
version: this.version,
topics,
userData: Buffer.from([]) // no idea what this is for
}),
}
}
});
const consumer = kafka.consumer({groupId: 'ashutosh', partitionAssigners: [SinglePartitionAssigner]})
// Use the partitionAssigner when you subscribe to a topic
// @ts-ignore
async function sendEvent(message: any) {
console.log({
key: message.key.toString(),
value: message.value.toString(),
headers: message.headers,
});
}
// @ts-ignore
async function main() {
await consumer.connect();
await consumer.subscribe({
topic: 'testTopic',
fromBeginning: true
})
await consumer.run({
eachMessage: async ({message}) => await sendEvent(message),
});
}
main().catch(error => {
console.error(error);
})
Although I am getting following output -
Events are not received, Can any one help me out. Thanks.