mfontanini / cppkafka

Modern C++ Apache Kafka client library (wrapper for librdkafka)
BSD 2-Clause "Simplified" License
593 stars 206 forks source link

How can i get the data from buffer_producer? #201

Open peijl1998 opened 5 years ago

peijl1998 commented 5 years ago

Now I use buffered producer and add_message to write data to kafka. But before flushing, I want to measure something on each data from producer's buffer. So, how can I read each message from producer's buffer?

My code is like this: ` BufferedProducer producer(config); producer.add_message(MessageBuilder( kafka_topic ).payload(mydata));

if (producer.get_buffer_size() > buffer_threshold) { // get each data from producer's buffer <----- HERE! producer.flush(); } `

Thanks! :)

accelerated commented 5 years ago

It's not possible at the moment and I'm not sure it's a good idea to expose the internal buffer to the application. Also the messages are serialized so you would have to deserialize them if you need to print/access anything inside them. Perhaps some iterator could be provided to the internal queue. Maybe @mfontanini can comment.

mfontanini commented 5 years ago

As @accelerated, it's not a good idea to start exposing internals. Also, you can implement your logic yourself: accumulate whatever data you have, when you call flush export it or do whatever you need and then clear your current stats.