Home

Awesome

NAME

Build Status

Net::Kafka - High-performant Perl client for Apache Kafka

SYNOPSIS

use Net::Kafka::Producer;
use Net::Kafka::Consumer;
use AnyEvent;

# Produce 1 message into "my_topic"
my $condvar     = AnyEvent->condvar;
my $producer    = Net::Kafka::Producer->new(
    'bootstrap.servers' => 'localhost:9092'
);
$producer->produce(
    payload => "message",
    topic   => "my_topic"
)->then(sub {
    my $delivery_report = shift;
    $condvar->send;
    print "Message successfully delivered with offset " . $delivery_report->{offset};
}, sub {
    my $error = shift;
    $condvar->send;
    die "Unable to produce a message: " . $error->{error} . ", code: " . $error->{code};
});
$condvar->recv;

# Consume message from "my_topic"
my $consumer    = Net::Kafka::Consumer->new(
    'bootstrap.servers'     => 'localhost:9092',
    'group.id'              => 'my_consumer_group',
    'enable.auto.commit'    => 'true',
);

$consumer->subscribe( [ "my_topic" ] );
while (1) {
    my $msg = $kafka->consumer_poll(1000);
    if ($msg) {
        if ( $msg->err ) {
            say "Error: ", Net::Kafka::Error::to_string($err);
        }
        else {
            say $msg->payload;
        }
    }
}

DESCRIPTION

This module provides Perl bindings to librdkafka C client library. It is heavily inspired by Kafka::Librd module originally developed by Pavel Shaydo.

Please refer to the following modules documentation in order to understand how to use it:

REQUIREMENTS

INSTALLATION

First install librdkafka (https://github.com/edenhill/librdkafka#installation).

BUILD FROM CPAN

cpanm install Net::Kafka

BUILD FROM SOURCE

Sources are available on Github: https://github.com/bookingcom/perl-Net-Kafka.

perl Makefile.pl
make
make test
make install

Net::Kafka::Producer

The Net::Kafka::Producer module provides interface to librdkafka's producer methods. It utilizes signal pipes, AnyEvent watcher and AnyEvent::XSPromises to make its behaviour asynchronous. Taking that into consideration you need to make sure to properly create condvar and send/recv in order to collect all outstanding promises. It is highly suggested to familirize yourself with both AnyEvent and AnyEvent::XSPromises modules. See "SYNOPSIS" for example.

METHODS

Net::Kafka::Consumer

The Net::Kafka::Consumer class provides interface to librdkafka's consumer functionality. It supports both "distributed" (subscription based) and "simple" (manual partition assignment) modes of work.

METHODS

Net::Kafka::Message

This class maps to rd_kafka_message_t structure from librdkafka and represents message or event. Objects of this class have the following methods:

Net::Kafka::Headers

This class contains a list of Kafka headers (it allows duplicates). Objects of this class have the following methods:

Net::Kafka::Err

This class provides static methods to convert error codes into names and descriptions.

CAVEATS

Message offset is truncated to 32 bit if perl is compiled without support for 64 bit integers.

SEE ALSO

LICENSE AND COPYRIGHT

Copyright (C) 2016, 2017 Pavel Shaydo

Copyright (C) 2018, 2019 Booking.com

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.