README.md 4.0 KB
Newer Older
1 2 3
librdkafka - Apache Kafka C client library
==========================================

4
Copyright (c) 2012-2013, [Magnus Edenhill](http://www.edenhill.se/).
5 6 7

[https://github.com/edenhill/librdkafka](https://github.com/edenhill/librdkafka)

M
Magnus Edenhill 已提交
8
**librdkafka** is a C library implementation of the
9
[Apache Kafka](http://kafka.apache.org/) protocol, containing both
M
Magnus Edenhill 已提交
10 11 12
Producer and Consumer support. It was designed with high performance
(current figures around 150000 msgs/second) and message delivery
reliability in mind.
13

14
**librdkafka** is licensed under the 2-clause BSD license.
M
Magnus Edenhill 已提交
15 16


17
**Apache Kafka 0.8 support:**
M
Magnus Edenhill 已提交
18

19 20 21
  * Branch: master
  * Producer: supported
  * Consumer: not yet implemented
22
  * Compression: snappy and gzip
23
  * Debian package: work in progress (separate "debian" branch)
24 25 26
  * ZooKeeper: not supported
  * API: not backwards compatible
  * Status: Testing, APIs subject to change.
M
Magnus Edenhill 已提交
27

28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
**Apache Kafka 0.7 support:**

  * Branch: 0.7
  * Producer: supported
  * Consumer: supported
  * Compression: not supported
  * ZooKeeper: not supported
  * API: backwards compatible with 0.6
  * Status: Stable


**Apache Kafka 0.6 support:**

  * Branch: 0.6
  * Producer: supported
  * Consumer: supported
  * Compression: not supported
  * ZooKeeper: not supported
  * Status: Testing




**Applications using librdkafka:**

54
  * [Wikimedia's varnishkafka](https://github.com/wikimedia/varnishkafka)
55
  * *Let me know if you are using librdkafka*
56

M
Magnus Edenhill 已提交
57

58 59 60 61 62 63 64 65 66 67 68 69 70

# Usage

## Requirements
	The GNU toolchain
   	pthreads
	zlib

## Instructions

### Building

      make all
M
Magnus Edenhill 已提交
71 72 73
      sudo make install
      # or to install in another location than /usr/local, set DESTDIR env
      # to the filesystem root of your choice.
74
      sudo make DESTDIR=/usr make install
75 76 77 78


### Usage in code

M
Magnus Edenhill 已提交
79
See `examples/rdkafka_performance.c` for an example producer
80 81 82 83 84 85 86


      #include <librdkafka/rdkafka.h>

      ..

      rd_kafka_t *rk;
M
Magnus Edenhill 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
      rd_kafka_conf_t conf;
      rd_kafka_topic_t *rkt;
      rd_kafka_topic_conf_t topic_conf;
      char errstr[512];

      /* Base our Kafka configuration on the default configuration */
      rd_kafka_defaultconf_set(&conf);
      conf.error_cb       = error_cb;
      conf.producer.dr_cb = msg_delivered_cb;

      if (!(rk = rd_kafka_new(RD_KAFKA_PRODUCER, &conf,
                              errstr, sizeof(errstr)))) {
            printf("Kafka initialization failed: %s\n", errstr);
            exit(1);
      }
102

M
Magnus Edenhill 已提交
103 104 105 106
      /* Base our topic configuration on the default topic configuration */
      rd_kafka_topic_defaultconf_set(&topic_conf);
      topic_conf.required_acks      = 2;
      topic_conf.message_timeout_ms = 1000*5*60; /* 5 minutes */
107

M
Magnus Edenhill 已提交
108 109 110 111
      /* Create local handle for topic "testtopic" */
      if (!(rkt = rd_kafka_topic_new(rk, "testtopic", &topic_conf))) {
       	    printf("Failed to add topic: %s\n", strerror(errno));
	    exit(1);
112 113
      }

M
Magnus Edenhill 已提交
114 115 116 117 118 119 120 121 122 123 124
      /* Add list of initial brokers. All brokers will eventually be
       * discovered by quering these brokers for the full list of brokers. */
      if (!rd_kafka_brokers_add(rk, "localhost,remotehost1,remotehost2:9099")) {
            printf("No valid brokers specified\n");
            exit(1);
      }

      ...

      /* Produce message */
      if (rd_kafka_produce(rkt, RD_KAFKA_PARTITION_UA /* random partition */,
M
Magnus Edenhill 已提交
125 126 127 128 129 130 131 132 133
                           RD_KAFKA_MSG_F_COPY,
                           mydata, mydata_len,
                           mykey, mykey_len,
                           per_message_opaque) == -1) {
            /* Try again in a short while if queue is full, or drop,
	     * decision is left to the application. /
            if (errno == ENOBUFS)
               ... retry message later ...
      }
M
Magnus Edenhill 已提交
134 135 136 137 138 139 140 141 142 143

 
      ...

      /* Serve kafka events (error and delivery report callbacks) */
      rd_kafka_poll(rk, 1000/*ms*/);
      
      ...

      /* Decommission kafka handle */
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
      rd_kafka_destroy(rk);

    

Link your program with `-lrdkafka -lz -lpthread -lrt`.


## Documentation

The API is documented in `rdkafka.h`

## Examples

See the `examples/`sub-directory.

M
Magnus Edenhill 已提交
159