2017 © Pedro Peláez
 

library kafka-php

Kafka client for php

image

nmred/kafka-php

Kafka client for php

  • Saturday, March 31, 2018
  • by nmred
  • Repository
  • 68 Watchers
  • 803 Stars
  • 407,290 Installations
  • PHP
  • 11 Dependents
  • 3 Suggesters
  • 260 Forks
  • 58 Open issues
  • 23 Versions
  • 9 % Grown

The README.md

Kafka-php

中文文档, (*1)

QQ Group Build Status Packagist Packagist Packagist GitHub issues GitHub forks GitHub stars GitHub license, (*2)

Kafka-php is a pure PHP kafka client that currently supports greater than 0.8.x version of Kafka, this project v0.2.x and v0.1.x are incompatible if using the original v0.1.x You can refer to the document Kafka PHP v0.1.x Document, but it is recommended to switch to v0.2.x . v0.2.x use PHP asynchronous implementation and kafka broker interaction, more stable than v0.1.x efficient, because the use of PHP language so do not compile any expansion can be used to reduce the access and maintenance costs, (*3)

Requirements

  • Minimum PHP version: 7.1
  • Kafka version greater than 0.8
  • The consumer module needs kafka broker version greater than 0.9.0

Installation

Add the lib directory to the PHP include_path and use an autoloader like the one in the examples directory (the code follows the PEAR/Zend one-class-per-file convention)., (*4)

Composer Install

Simply add a dependency nmred/kafka-php to your project if you use Composer to manage the dependencies of your project., (*5)

$ composer require nmred/kafka-php, (*6)

Here is a minimal example of a composer.json file :, (*7)

{
    "require": {
        "nmred/kafka-php": "0.2.*"
    }
}

Configuration

Configuration properties are documented in Configuration, (*8)

Producer

Asynchronous mode

<?php
require '../vendor/autoload.php';
date_default_timezone_set('PRC');
use Monolog\Logger;
use Monolog\Handler\StdoutHandler;
// Create the logger
$logger = new Logger('my_logger');
// Now add some handlers
$logger->pushHandler(new StdoutHandler());

$config = \Kafka\ProducerConfig::getInstance();
$config->setMetadataRefreshIntervalMs(10000);
$config->setMetadataBrokerList('10.13.4.159:9192');
$config->setBrokerVersion('1.0.0');
$config->setRequiredAck(1);
$config->setIsAsyn(false);
$config->setProduceInterval(500);
$producer = new \Kafka\Producer(
    function() {
        return [
            [
                'topic' => 'test',
                'value' => 'test....message.',
                'key' => 'testkey',
            ],
        ];
    }
);
$producer->setLogger($logger);
$producer->success(function($result) {
    var_dump($result);
});
$producer->error(function($errorCode) {
        var_dump($errorCode);
});
$producer->send(true);

Synchronous mode

<?php
require '../vendor/autoload.php';
date_default_timezone_set('PRC');
use Monolog\Logger;
use Monolog\Handler\StdoutHandler;
// Create the logger
$logger = new Logger('my_logger');
// Now add some handlers
$logger->pushHandler(new StdoutHandler());

$config = \Kafka\ProducerConfig::getInstance();
$config->setMetadataRefreshIntervalMs(10000);
$config->setMetadataBrokerList('127.0.0.1:9192');
$config->setBrokerVersion('1.0.0');
$config->setRequiredAck(1);
$config->setIsAsyn(false);
$config->setProduceInterval(500);
$producer = new \Kafka\Producer();
$producer->setLogger($logger);

for($i = 0; $i < 100; $i++) {
    $producer->send([
        [
            'topic' => 'test1',
            'value' => 'test1....message.',
            'key' => '',
        ],
    ]);
}

Consumer

<?php
require '../vendor/autoload.php';
date_default_timezone_set('PRC');
use Monolog\Logger;
use Monolog\Handler\StdoutHandler;
// Create the logger
$logger = new Logger('my_logger');
// Now add some handlers
$logger->pushHandler(new StdoutHandler());

$config = \Kafka\ConsumerConfig::getInstance();
$config->setMetadataRefreshIntervalMs(10000);
$config->setMetadataBrokerList('10.13.4.159:9192');
$config->setGroupId('test');
$config->setBrokerVersion('1.0.0');
$config->setTopics(['test']);
//$config->setOffsetReset('earliest');
$consumer = new \Kafka\Consumer();
$consumer->setLogger($logger);
$consumer->start(function($topic, $part, $message) {
    var_dump($message);
});

Low-Level API

Refer Example, (*9)

QQ Group

Group 1: 531522091 Group 2: 657517955 QQ Group, (*10)

The Versions

31/01 2018
20/04 2017

v0.1.6.x-dev

0.1.6.9999999-dev http://www.swanlinux.net

Kafka client for php

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.3

 

The Development Requires

client kafka

18/02 2017

v0.1.6

0.1.6.0 http://www.swanlinux.net

Kafka client for php

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.3

 

The Development Requires

client kafka

02/12 2015

v0.1.5

0.1.5.0 http://www.swanlinux.net

Kafka client for php

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.3

 

The Development Requires

client kafka

06/09 2015

v0.1.4

0.1.4.0 http://www.swanlinux.net

Kafka client for php

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.3

 

The Development Requires

client kafka

05/08 2015

v0.1.3

0.1.3.0 http://wwww.swanlinux.net

Kafka client for php

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.3

 

The Development Requires

client kafka

30/10 2014

v0.1.2

0.1.2.0 http://wwww.swanlinux.net

Kafka client for php

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.3

 

The Development Requires

client kafka

29/09 2014

v0.1.1

0.1.1.0 http://wwww.swanlinux.net

Kafka client for php

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.3

 

The Development Requires

client kafka