【转】c++(11)使用librdkafka库实现kafka的消费实例

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处连接和本声明。
本文连接:https://blog.csdn.net/lijinqi1987/article/details/76691170c++

 

librdkafka在c语言的基础上封装了一层c++的API,能够实现kafka的消费操做,基本操做步骤以下bootstrap


一、建立kafka 配置fetch

RdKafka::Conf *conf = nullptr;
conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);.net

 

二、设置kafka各项参数server


/*设置broker list*/
conf->set("bootstrap.servers", brokers_, errstr);
blog

/*设置consumer group*/
conf->set("group.id", groupid_, errstr);
kafka

/*每次从单个分区中拉取消息的最大尺寸*/
conf->set("max.partition.fetch.bytes", strfetch_num, errstr);it


三、建立kafka topic配置
RdKafka::Conf *tconf = nullptr;
tconf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC);io


四、设置kafka topic参数class

if(tconf->set("auto.offset.reset", "smallest", errstr)


五、建立kafka consumer实例

kafka_consumer_ = RdKafka::Consumer::create(conf, errstr);


六、建立kafka topic

RdKafka::Topic::create(kafka_consumer_, topics_, tconf, errstr);


七、启动kafka consumer实例

RdKafka::ErrorCode resp = kafka_consumer_->start(topic_, partition_, offset_);


八、消费kafka

kafka_consumer_->consume(topic_, partition_, timeout_ms);


九、阻塞等待消息

kafka_consumer_->poll(0);


十、中止消费

kafka_consumer_->stop(topic_, partition_);


十一、销毁consumer实例

RdKafka::wait_destroyed(5000);

相关文章
相关标签/搜索