<?php require 'autoloader.php'; $data = array('test1'); $conn = new \Kafka\Socket('192.168.1.115', '9092'); $conn->connect(); $encoder = new \Kafka\Protocol\Encoder($conn); $encoder->metadataRequest($data); $decoder = new \Kafka\Protocol\Decoder($conn); $result = $decoder->metadataResponse(); var_dump($result);
/** * send message to broker * * @access public * @return void */ public function send() { $data = $this->_formatPayload(); if (empty($data)) { return false; } $responseData = array(); foreach ($data as $host => $requestData) { $stream = $this->client->getStream($host); $conn = $stream['stream']; $encoder = new \Kafka\Protocol\Encoder($conn); $encoder->produceRequest($requestData); if ((int) $this->requiredAck !== 0) { // get broker response $decoder = new \Kafka\Protocol\Decoder($conn); $response = $decoder->produceResponse(); foreach ($response as $topicName => $info) { if (!isset($responseData[$topicName])) { $responseData[$topicName] = $info; } else { $responseData[$topicName] = array_merge($info, $responseData[$topicName]); } } } $this->client->freeStream($stream['key']); } $this->payload = array(); return $responseData; }
<?php require 'autoloader.php'; $data = array('group_id' => 'testgroup1', 'data' => array(array('topic_name' => 'test6', 'partitions' => array(array('partition_id' => 2, 'offset' => 2))))); $conn = new \Kafka\Socket('hadoop11', '9092'); $conn->connect(); $encoder = new \Kafka\Protocol\Encoder($conn); $encoder->commitOffsetRequest($data); $decoder = new \Kafka\Protocol\Decoder($conn); $result = $decoder->commitOffsetResponse(); var_dump($result);
<?php require 'autoloader.php'; $data = array('group_id' => 'testgroup1', 'data' => array(array('topic_name' => 'test6', 'partitions' => array(array('partition_id' => 2))))); $conn = new \Kafka\Socket('hadoop11', '9092'); $conn->connect(); $encoder = new \Kafka\Protocol\Encoder($conn); $encoder->fetchOffsetRequest($data); $decoder = new \Kafka\Protocol\Decoder($conn); $result = $decoder->fetchOffsetResponse(); var_dump($result);
/** * @param array $topics */ private function loadTopicDetail(array $topics) { if ($this->client === null) { throw new \Kafka\Exception('client was not provided'); } $response = null; foreach ($this->hostList as $host) { try { $response = null; $stream = $this->client->getStream($host); $conn = $stream['stream']; $encoder = new \Kafka\Protocol\Encoder($conn); $encoder->metadataRequest($topics); $decoder = new \Kafka\Protocol\Decoder($conn); $response = $decoder->metadataResponse(); $this->client->freeStream($stream['key']); break; } catch (\Kafka\Exception $e) { // keep trying } } if ($response) { // Merge arrays using "+" operator to preserve key (which are broker IDs) // instead of array_merge (which reindex numeric keys) $this->brokers = $response['brokers'] + $this->brokers; $this->topics = array_merge($response['topics'], $this->topics); } else { throw new \Kafka\Exception('Could not connect to any kafka brokers'); } }
<?php require 'autoloader.php'; $data = array('required_ack' => 1, 'timeout' => 1000, 'data' => array(array('topic_name' => 'test', 'partitions' => array(array('partition_id' => 0, 'offset' => 34, 'max_bytes' => 1024))))); $conn = new \Kafka\Socket('192.168.1.115', '9092'); $conn->connect(); $encoder = new \Kafka\Protocol\Encoder($conn); $encoder->fetchRequest($data); $decoder = new \Kafka\Protocol\Decoder($conn); $topic = $decoder->fetchResponse(); foreach ($topic as $topic_name => $partition) { foreach ($partition as $partId => $messageSet) { var_dump($partition->getHighOffset()); foreach ($messageSet as $message) { var_dump((string) $message); } } }
<?php require 'autoloader.php'; $data = array('data' => array(array('topic_name' => 'test', 'partitions' => array(array('partition_id' => 0, 'max_offset' => 1, 'time' => -1))))); $conn = new \Kafka\Socket('localhost', '9092'); $conn->connect(); $encoder = new \Kafka\Protocol\Encoder($conn); $encoder->offsetRequest($data); $decoder = new \Kafka\Protocol\Decoder($conn); $result = $decoder->offsetResponse(); var_dump($result);
<?php require 'autoloader.php'; $msg = '/weibo/indexrightrecom?cuid=1738550761&ouid=1738550761&lang=zh-cn&gender=f&version=5&province=100&city=1000&ip=27.37.36.86&url=http%253A%252F%252Fweibo.com%252Fpls%252Fcommonapi%253Frequest_group%253D5' . time(); $data = array('required_ack' => 1, 'timeout' => 1000, 'data' => array(array('topic_name' => 'test', 'partitions' => array(array('partition_id' => 0, 'messages' => array($msg)))))); echo $msg; $conn = new \Kafka\Socket('192.168.1.115', '9092'); $conn->connect(); $encoder = new \Kafka\Protocol\Encoder($conn); $encoder->produceRequest($data); $decoder = new \Kafka\Protocol\Decoder($conn); $result = $decoder->produceResponse(); var_dump($result);