4 [![Build Status](https://travis-ci.org/nmred/kafka-php.svg?branch=master)](https://travis-ci.org/nmred/Kafka-php)
6 Kafka-php is a php client with Zookeeper integration for apache Kafka. It only supports the latest version of Kafka 0.8 which is still under development, so this module is _not production ready_ so far.
8 The Zookeeper integration does the following jobs:
10 * Loads broker metadata from Zookeeper before we can communicate with the Kafka server
11 * Watches broker state, if broker changes, the client will refresh broker and topic metadata stored in the client
15 * Minimum PHP version: 5.3.3.
17 * You need to have access to your Kafka instance and be able to connect through TCP. You can obtain a copy and instructions on how to setup kafka at https://github.com/kafka-dev/kafka [kafka-08-quick-start](https://cwiki.apache.org/KAFKA/kafka-08-quick-start.html)
18 * The [PHP Zookeeper extension](https://github.com/andreiz/php-zookeeper) is required if you want to use the Zookeeper-based consumer.
19 * Productor can not dependency zookeeper
22 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).
26 Simply add a dependency on nmred/kafka-php to your project's composer.json file if you use Composer to manage the dependencies of your project. Here is a minimal example of a composer.json file :
31 "nmred/kafka-php": "0.1.*"
38 ### \Kafka\Produce::getInstance($hostList, $timeout)
40 * `hostList` : zookeeper host list , example 127.0.0.1:2181,192.168.1.114:2181
41 * `timeout` : zookeeper timeout
43 ### \Kafka\Produce::setRequireAck($ack = -1)
45 * `ack`: This field indicates how many acknowledgements the servers should receive before responding to the request.
47 ### \Kafka\Produce::setMessages($topicName, $partitionId, $messages)
49 * `topicName` : The topic that data is being published to.
50 * `partitionId` : The partition that data is being published to.
51 * `messages` : [Array] publish message.
53 ### \Kafka\Produce::send()
55 send message sets to the server.
60 $produce = \Kafka\Produce::getInstance('localhost:2181', 3000);
62 $produce->setRequireAck(-1);
63 $produce->setMessages('test', 0, array('test1111111'));
64 $produce->setMessages('test6', 0, array('test1111111'));
65 $produce->setMessages('test6', 2, array('test1111111'));
66 $produce->setMessages('test6', 1, array('test111111111133'));
67 $result = $produce->send();
74 ### \Kafka\Consumer::getInstance($hostList, $timeout)
76 * `hostList` : zookeeper host list , example 127.0.0.1:2181,192.168.1.114:2181
77 * `timeout` : zookeeper timeout
79 ### \Kafka\Consumer::setGroup($groupName)
81 * `groupName` : Specify consumer group.
83 ### \Kafka\Consumer::setPartition($topicName, $partitionId, $offset = 0)
85 * `topicName` : The topic that data is being fetch to.
86 * `partitionId` : The partition that data is being fetch to.
87 * `offset`: set fetch offset. default `0`.
89 ### \Kafka\Consumer::fetch()
91 return fetch message Iterator. `\Kafka\Protocol\Fetch\Topic`
93 ### \Kafka\Protocol\Fetch\Topic
95 this object is iterator
98 `value`: `\Kafka\Protocol\Fetch\Partition`
100 ### \Kafka\Protocol\Fetch\Partition
102 this object is iterator.
105 `value`: messageSet object
107 #### \Kafka\Protocol\Fetch\Partition::getErrCode()
109 return partition fetch errcode.
111 #### \Kafka\Protocol\Fetch\Partition::getHighOffset()
113 return partition fetch offset.
115 ### \Kafka\Protocol\Fetch\MessageSet
117 this object is iterator. `\Kafka\Protocol\Fetch\Message`
122 $consumer = \Kafka\Consumer::getInstance('localhost:2181');
124 $consumer->setGroup('testgroup');
125 $consumer->setPartition('test', 0);
126 $consumer->setPartition('test6', 2, 10);
127 $result = $consumer->fetch();
128 foreach ($result as $topicName => $topic) {
129 foreach ($topic as $partId => $partition) {
130 var_dump($partition->getHighOffset());
131 foreach ($partition as $message) {
132 var_dump((string)$message);
141 The produce API is used to send message sets to the server. For efficiency it allows sending message sets intended for many topic partitions in a single request.
143 \Kafka\Protocol\Encoder::produceRequest
149 // This field indicates how many acknowledgements the servers should receive before responding to the request. default `0`
150 // If it is 0 the server will not send any response
151 // If it is -1 the server will block until the message is committed by all in sync replicas before sending a response
152 // For any number > 1 the server will block waiting for this number of acknowledgements to occur
154 // This provides a maximum time in milliseconds the server can await the receipt of the number of acknowledgements in RequiredAcks.
157 'topic_name' => 'testtopic',
158 // The topic that data is being published to.[String]
159 'partitions' => array(
162 // The partition that data is being published to.
187 'topic_name' => 'test',
188 'partitions' => array(
201 $conn = new \Kafka\Socket('localhost', '9092');
203 $encoder = new \Kafka\Protocol\Encoder($conn);
204 $encoder->produceRequest($data);
206 $decoder = new \Kafka\Protocol\Decoder($conn);
207 $result = $decoder->produceResponse();
213 The fetch API is used to fetch a chunk of one or more logs for some topic-partitions. Logically one specifies the topics, partitions, and starting offset at which to begin the fetch and gets back a chunk of messages
215 \Kafka\Protocol\Encoder::fetchRequest
221 // The replica id indicates the node id of the replica initiating this request. default `-1`
222 'max_wait_time' => 100,
223 // The max wait time is the maximum amount of time in milliseconds to block waiting if insufficient data is available at the time the request is issued. default 100 ms.
224 'min_bytes' => 64 * 1024 // 64k
225 // This is the minimum number of bytes of messages that must be available to give a response. default 64k.
228 'topic_name' => 'testtopic',
229 // The topic that data is being published to.[String]
230 'partitions' => array(
233 // The partition that data is being published to.
235 // The offset to begin this fetch from. default 0
236 'max_bytes' => 100 * 1024 * 1024,
237 // This is the minimum number of bytes of messages that must be available to give a response. default 100Mb
247 \Kafka\Protocol\Fetch\Topic iterator
255 'topic_name' => 'test',
256 'partitions' => array(
266 $conn = new \Kafka\Socket('localhost', '9092');
268 $encoder = new \Kafka\Protocol\Encoder($conn);
269 $encoder->fetchRequest($data);
271 $decoder = new \Kafka\Protocol\Decoder($conn);
272 $result = $decoder->fetchResponse();
278 This API describes the valid offset range available for a set of topic-partitions. As with the produce and fetch APIs requests must be directed to the broker that is currently the leader for the partitions in question. This can be determined using the metadata API.
280 \Kafka\Protocol\Encoder::offsetRequest
286 // The replica id indicates the node id of the replica initiating this request. default `-1`
289 'topic_name' => 'testtopic',
290 // The topic that data is being published to.[String]
291 'partitions' => array(
294 // The partition that get offset .
296 // Used to ask for all messages before a certain time (ms).
297 // Specify -1 to receive the latest offsets
298 // Specify -2 to receive the earliest available offset.
300 // max return offset element. default 10000.
319 'topic_name' => 'test',
320 'partitions' => array(
331 $conn = new \Kafka\Socket('localhost', '9092');
333 $encoder = new \Kafka\Protocol\Encoder($conn);
334 $encoder->offsetRequest($data);
336 $decoder = new \Kafka\Protocol\Decoder($conn);
337 $result = $decoder->offsetResponse();
343 The metdata returned is at the partition level, but grouped together by topic for convenience and to avoid redundancy. For each partition the metadata contains the information for the leader as well as for all the replicas and the list of replicas that are currently in-sync.
345 \Kafka\Protocol\Encoder::metadataRequest
350 'topic_name1', // topic name
366 $conn = new \Kafka\Socket('localhost', '9092');
368 $encoder = new \Kafka\Protocol\Encoder($conn);
369 $encoder->metadataRequest($data);
371 $decoder = new \Kafka\Protocol\Decoder($conn);
372 $result = $decoder->metadataResponse();
376 ### Offset Commit API
378 These APIs allow for centralized management of offsets.
380 \Kafka\Protocol\Encoder::commitOffsetRequest
385 'group_id' => 'testgroup',
389 'topic_name' => 'testtopic',
390 // The topic that data is being published to.[String]
391 'partitions' => array(
394 // The partition that get offset .
396 // The offset to begin this fetch from.
398 // If the time stamp field is set to -1, then the broker sets the time stamp to the receive time before committing the offset.
414 'group_id' => 'testgroup',
417 'topic_name' => 'test',
418 'partitions' => array(
429 $conn = new \Kafka\Socket('localhost', '9092');
431 $encoder = new \Kafka\Protocol\Encoder($conn);
432 $encoder->commitOffsetRequest($data);
434 $decoder = new \Kafka\Protocol\Decoder($conn);
435 $result = $decoder->commitOffsetResponse();
441 These APIs allow for centralized management of offsets.
443 \Kafka\Protocol\Encoder::fetchOffsetRequest
448 'group_id' => 'testgroup',
452 'topic_name' => 'testtopic',
453 // The topic that data is being published to.[String]
454 'partitions' => array(
457 // The partition that get offset .
473 'group_id' => 'testgroup',
476 'topic_name' => 'test',
477 'partitions' => array(
487 $conn = new \Kafka\Socket('localhost', '9092');
489 $encoder = new \Kafka\Protocol\Encoder($conn);
490 $encoder->fetchOffsetRequest($data);
492 $decoder = new \Kafka\Protocol\Decoder($conn);
493 $result = $decoder->fetchOffsetResponse();