cppkafka

repository·master·Indexed 20 days ago

https://github.com/mfontanini/cppkafka

A high-level C++ wrapper for librdkafka providing a modern API for producing and consuming Apache Kafka messages with minimal performance overhead. It requires librdkafka >= 0.9.4, CMake >= 3.9.2, a C++11 compatible compiler, and the Boost library.

Tokens
946
Snippets
5
Records
5
Agent score
21%

What's inside cppkafka

  1. Install and compile cppkafka

    master

    To compile cppkafka from source, ensure you have the following dependencies installed:

    • librdkafka >= 0.9.4
    • CMake >= 3.9.2
    • A C++11 compatible compiler (e.g., gcc >= 4.8)
    • The Boost library (specifically for boost::optional)

    Follow these steps to build and install:

    mkdir build
    cd build
    cmake <OPTIONS> ..
    make
    make install
  2. Integrate cppkafka into a CMake project

    master

    To use cppkafka in your own application, you must link against both cppkafka and rdkafka. If you are using CMake, you can simplify this by using find_package and linking to the CppKafka::cppkafka target.

    find_package(CppKafka REQUIRED)
    
    target_link_libraries(<YourLibrary> CppKafka::cppkafka)
  3. Configure CMake options for building cppkafka

    master

    When running cmake to build cppkafka, you can specify several options to customize the build process:

    OptionDescription
    RDKAFKA_ROOTSpecify a different librdkafka install directory.
    RDKAFKA_DIRSpecify a different directory where RdKafkaConfig.cmake is installed.
    BOOST_ROOTSpecify a different Boost install directory.
    CPPKAFKA_CMAKE_VERBOSEGenerate verbose output. Default is OFF.
    CPPKAFKA_BUILD_SHAREDBuild cppkafka as a shared library. Default is ON.
    CPPKAFKA_DISABLE_TESTSDisable build of cppkafka tests. Default is OFF.
    CPPKAFKA_DISABLE_EXAMPLESDisable build of cppkafka examples. Default is OFF.
    CPPKAFKA_BOOST_STATIC_LIBSLink with Boost static libraries. Default is ON.
    CPPKAFKA_BOOST_USE_MULTITHREADEDUse Boost multi-threaded libraries. Default is ON.
    CPPKAFKA_RDKAFKA_STATIC_LIBLink to Rdkafka static library. Default is OFF.
    CPPKAFKA_CONFIG_DIRInstall location of the cmake configuration files. Default is lib/cmake/cppkafka.
    CPPKAFKA_PKGCONFIG_DIRInstall location of the .pc file. Default is share/pkgconfig.
    CPPKAFKA_EXPORT_PKGCONFIGGenerate cppkafka.pc file. Default is ON.
    CPPKAFKA_EXPORT_CMAKE_CONFIGGenerate CMake config, target and version files. Default is ON.

    Example usage:

    cmake -DRDKAFKA_ROOT=/some/other/dir -DCPPKAFKA_BUILD_SHARED=OFF ...
  4. Produce messages with the Producer API

    master

    To send messages to a Kafka topic, use the Configuration class to set broker details and the Producer class to send the data. The MessageBuilder is used to construct the message, allowing you to specify the topic, partition, and payload.

    #include <cppkafka/cppkafka.h>
    
    using namespace std;
    using namespace cppkafka;
    
    int main() {
        // Create the config
        Configuration config = {
            { "metadata.broker.list", "127.0.0.1:9092" }
        };
    
        // Create the producer
        Producer producer(config);
    
        // Produce a message!
        string message = "hey there!";
        producer.produce(MessageBuilder("my_topic").partition(0).payload(message));
        producer.flush();
    }