Module kafka::producer [] [src]

Kafka Producer - A higher-level API for sending messages to kafka topics.

A multi-topic capable producer for a Kafka cluster providing a convenient API for sending messages. So far the producer has only synchronous capabilities.

In Kafka, each message is basically a key/value pair. A Record is all the data necessary to produce such a message to Kafka.

Example

use std::fmt::Write;
use kafka::producer::{Producer, Record};

let mut producer =
    Producer::from_hosts(vec!("localhost:9092".to_owned()))
        .with_ack_timeout(1000)
        .with_required_acks(1)
        .create()
        .unwrap();

let mut buf = String::with_capacity(2);
for i in 0..10 {
  let _ = write!(&mut buf, "{}", i); // some computation of the message data to be sent
  producer.send(&Record::from_value("my-topic", buf.as_bytes())).unwrap();
  buf.clear();
}

In this example, when the method call to producer.send returns successfully, we are guaranteed the message is delivered to Kafka and persisted by at least one Kafka broker. However, when sending multiple messages just like this example, it is more efficient to send them in batches using Producer::send_all.

Structs

Builder

A Kafka Producer builder easing the process of setting up various configuration settings.

DefaultPartitioner

As its name implies DefaultPartitioner is the default partitioner for Producer.

Producer

The Kafka Producer

Record

A structure representing a message to be sent to Kafka through the Producer API. Such a message is basically a key/value pair specifying the target topic and optionally the topic's partition.

Topics

A description of available topics and their available partitions.

Enums

Compression

Compression types supported by kafka. The numeral values of this enumeration correspond to the compression encoding in the attributes of a Message in the protocol.

Constants

DEFAULT_ACK_TIMEOUT

The default value for Builder::with_ack_timeout.

DEFAULT_REQUIRED_ACKS

The default value for Builder::with_required_acks.

Traits

AsBytes

A trait used by Producer to obtain the bytes Record::key and Record::value represent. This leaves the choice of the types for key and value with the client.

Partitioner

A partitioner is given a chance to choose/redefine a partition for a message to be sent to Kafka. See also Record#with_partition.