1、Kafka 生产者(Producer)示例
#include <iostream>
#include <string>
#include <librdkafka/rdkafkacpp.h>
class ExampleDeliveryReportCb : public RdKafka::DeliveryReportCb {
public:
void dr_cb (RdKafka::Message &message) {
std::cout << "Message delivery for (" << message.len() << " bytes): " <<
message.errstr() << std::endl;
}
};
int main() {
std::string brokers = "localhost:9092"; // Kafka brokers
std::string errstr;
RdKafka::Conf *conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);
RdKafka::Conf *tconf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC);
conf->set("bootstrap.servers", brokers, errstr);
ExampleDeliveryReportCb ex_dr_cb;
conf->set("dr_cb", &ex_dr_cb, errstr);
RdKafka::Producer *producer = RdKafka::Producer::create(conf, errstr);
if (!producer) {
std::cerr << "Failed to create producer: " << errstr << std::endl;
delete tconf;
delete conf;
return 1;
}
std::string topic_str = "test_topic";
RdKafka::Topic *topic = RdKafka::Topic::create(producer, topic_str,
tconf, errstr);
if (!topic) {
std::cerr << "Failed to create topic: " << errstr << std::endl;
delete producer;
delete tconf;
delete conf;
return 1;
}
std::string key;
std::string payload = "Hello, Kafka!";
RdKafka::ErrorCode resp = producer->produce(topic, RdKafka::Topic::PARTITION_UA,
RdKafka::Producer::RK_MSG_COPY /* Copy payload */,
const_cast<char *>(payload.c_str()), payload.size(),
&key, NULL);
if (resp != RdKafka::ERR_NO_ERROR) {
std::cerr << "Failed to produce message: " <<
RdKafka::err2str(resp) << std::endl;
} else {
std::cout << "Produced message (" << payload.size() << " bytes)" <<
std::endl;
producer->poll(0); // Non-blocking poll
}
producer->flush(10000); // Wait for up to 10 seconds to flush messages
delete topic;
delete producer;
delete tconf;
delete conf;
return 0;
}
2、 Kafka 消费者(Consumer)示例
#include <iostream>
#include <string>
#include <librdkafka/rdkafkacpp.h>
class ExampleConsumeCb : public RdKafka::ConsumeCb {
public:
void consume_cb (RdKafka::Message &message, void *opaque) {
if (message.err() == RdKafka::ERR_NO_ERROR) {
std::cout << "Message received (" << message.len() << " bytes)" << std::endl;
if (message.key()) {
std::cout << "Key: " << *message.key() << std::endl;
}
std::cout << "Payload: " << std::string(static_cast<char *>(message.payload()), message.len()) << std::endl;
} else {
std::cerr << "Error while consuming message: " << message.errstr() << std::endl;
}
}
};
int main() {
std::string brokers = "localhost:9092"; // Kafka brokers
std::string errstr;
RdKafka::Conf *conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);
conf->set("bootstrap.servers", brokers, errstr);
RdKafka::Conf *tconf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC);
RdKafka::Consumer *consumer = RdKafka::Consumer::create(conf, errstr);
if (!consumer) {
std::cerr << "Failed to create consumer: " << errstr << std::endl;
delete tconf;
delete conf;
return 1;
}
std::string topic_str = "test_topic";
RdKafka::Topic *topic = RdKafka::Topic::create(consumer, topic_str,
tconf, errstr);
if (!topic) {
std::cerr << "Failed to create topic: " << errstr << std::endl;
delete consumer;
delete tconf;
delete conf;
return 1;
}
ExampleConsumeCb ex_consume_cb;
RdKafka::ErrorCode resp = consumer->start(topic, 0, RdKafka::Topic::OFFSET_BEGINNING);
if (resp != RdKafka::ERR_NO_ERROR) {
std::cerr << "Failed to start consumer: " <<
RdKafka::err2str(resp) << std::endl;
delete topic;
delete consumer;
delete tconf;
delete conf;
return 1;
}
while (true) {
RdKafka::Message *msg = consumer->consume(topic, 0, 1000);
ex_consume_cb.consume_cb(*msg, NULL);
delete msg;
}
consumer->stop(topic, 0);
consumer->poll(1000); // Final cleanup
delete topic;
delete consumer;
delete tconf;
delete conf;
return 0;
}