/** * @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('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);
/** * testMetadataRequest * * @access public * @return void */ public function testMetadataRequest() { $encoder = new \Kafka\Protocol\Encoder($this->stream); $data = array(); try { $encoder->metadataRequest(null); } catch (\Kafka\Exception\Protocol $e) { $this->assertSame('request metadata topic array have invalid value. ', $e->getMessage()); } $data = array('test', 'test1'); $len = $encoder->metadataRequest($data); $this->assertEquals('00000024000300000000000000096b61666b612d7068700000000200047465737400057465737431', $this->getData($len)); $this->stream->rewind(); $data = 'test1'; $len = $encoder->metadataRequest($data); $this->assertEquals('0000001e000300000000000000096b61666b612d7068700000000100057465737431', $this->getData($len)); }