Search code examples
c++kafka-producer-api

Getting null values on consumer while pushing from cppkafka to kafka topic?


Im using cppkafka connected to multiple brokers to produce and send message to the kafka queue. Although the msg get sent to the consumer, but the consumer receives null at all times. I used the produce message methods exactly as cppkafka examples, but the consumer always receives a null msg. Please see code snippet below.

Ive tried playing around with the config settings as you see above. I've also tried using the exact type for the function payload. Even though the examples passed an std::string, Ive also tried using the exact signature type to no avail i.e: CppKafka::Buffer cppkafka::Buffer msg(json_msg.c_str(), json_msg.length() );

Configuration config = {
{"metadata.broker.list", "brokers:<portno>"},
{"debug", "all"},
{"client.id", "myapp"},
{"message.timeout.ms", 300000},
{"session.timeout.ms", 600000},
{"enable.auto.commit", false}
};

//Create the producer
Producer producer(config);
int partition = 0;
const string json_msg = "{"name":"john smith", "age":"25", "city":"NYC"}";
// Produce a message!

producer.produce(MessageBuilder("mytopic").partition(0).payload(json_msg)); producer.flush();

Expected Result: "{"name":"john smith", "age":"25", "city":"NYC"}"

Actual Result: null


Solution

  • Please note that this issue is now resolved. We have 3 brokers, with 12 partitions. So the producer client was producing messages to partition 0 only. But at that point in time, partition 0 was not on the lead-broker, so the messages where not getting replicated to the other partitions. The solution we deployed was to produce without specifying a partition and the lead broker would receive the msgs and decide for itself which partition to use - Thanks.