程序员笔记 (四十五)使用librdkafka库来进行kafka的简单的消费
克里斯蒂亚诺诺
2024-01-12 14:08:13
66596
赞同 0
反对 0
程序员笔记 (四十五)使用librdkafka库来进行kafka的简单的消费
关于librdkafka库的介绍,可以参考kafka的c/c++高性能客户端librdkafka简介,本文使用librdkafka库来进行kafka的简单的消费
librdkafka在c语言的基础上封装了一层c++的API,可以实现kafka的消费操作,基本操作步骤如下
RdKafka::Conf *conf = nullptr;
conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);
conf->set("bootstrap.servers", brokers_, errstr);
conf->set("group.id", groupid_, errstr);
conf->set("max.partition.fetch.bytes", strfetch_num, errstr);
RdKafka::Conf *tconf = nullptr;
tconf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC);
if(tconf->set("auto.offset.reset", "smallest", errstr)
kafka_consumer_ = RdKafka::Consumer::create(conf, errstr);
RdKafka::Topic::create(kafka_consumer_, topics_, tconf, errstr);
RdKafka::ErrorCode resp = kafka_consumer_->start(topic_, partition_, offset_);
kafka_consumer_->consume(topic_, partition_, timeout_ms);
kafka_consumer_->poll(0);
kafka_consumer_->stop(topic_, partition_);
RdKafka::wait_destroyed(5000);
g++ my_consumer.cpp -o my_consumer_cpp -std=c++11 -lrdkafka++ -lz -lpthread -lrt
在运行my_producer或my_consumer时可能会报错"error while loading shared libraries xxx.so", 此时需要在/etc/ld.so.conf中加入xxx.so所在的目录
如果您发现该资源为电子书等存在侵权的资源或对该资源描述不正确等,可点击“私信”按钮向作者进行反馈;如作者无回复可进行平台仲裁,我们会在第一时间进行处理!