Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5e4db55

Browse files
committed
add metadata example
1 parent b6a8ff7 commit 5e4db55

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

‎src/ext-php-kafka/pure-php/metadata.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
use Kafka\Configuration;
4+
use Kafka\Producer;
5+
6+
error_reporting(E_ALL);
7+
8+
$conf = new Configuration();
9+
// will be visible in broker logs
10+
$conf->set('client.id', 'pure-php-producer');
11+
// set broker
12+
$conf->set('metadata.broker.list', 'kafka:9096');
13+
// set compression (supported are: none,gzip,lz4,snappy,zstd)
14+
15+
// SASL Authentication
16+
// can be SASL_PLAINTEXT, SASL_SSL
17+
// conf->set('security.protocol', '');
18+
// can be GSSAPI, PLAIN, SCRAM-SHA-256, SCRAM-SHA-512, OAUTHBEARER
19+
// $conf->set('sasl.mechanisms', '');
20+
// $conf->set('sasl.username', '');
21+
// $conf->set('sasl.password', '');
22+
// default is none
23+
// $conf->set('ssl.endpoint.identification.algorithm', 'https');
24+
25+
26+
// SSL Authentication
27+
//$conf->set('security.protocol', 'ssl');
28+
//$conf->set('ssl.ca.location', __DIR__.'/../keys/ca.pem');
29+
//$conf->set('ssl.certificate.location', __DIR__.'/../keys/kafka.cert');
30+
//$conf->set('ssl.key.location', __DIR__.'/../keys/kafka.key');
31+
32+
// Add additional output if you need to debug a problem
33+
// $conf->set('log_level', (string) LOG_DEBUG);
34+
// $conf->set('debug', 'all');
35+
36+
$producer = new Producer($conf);
37+
// get metadata
38+
$metadata = $producer->getMetadata(false, 10000);
39+
echo sprintf('Broker id: %d', $metadata->getOrigBrokerId()) . PHP_EOL;
40+
echo sprintf('Broker name: %s', $metadata->getOrigBrokerName()) . PHP_EOL;
41+
42+
echo 'Info about full broker list' . PHP_EOL;
43+
$brokers = $metadata->getBrokers();
44+
while ($brokers->valid()) {
45+
echo sprintf('Broker id: %d', $brokers->current()->getId()) . PHP_EOL;
46+
echo sprintf('Broker host: %s', $brokers->current()->getHost()) . PHP_EOL;
47+
echo sprintf('Broker port: %d', $brokers->current()->getPort()) . PHP_EOL;
48+
$brokers->next();
49+
}
50+
51+
echo 'Info about topics' . PHP_EOL;
52+
$topics = $metadata->getTopics();
53+
while ($topics->valid()) {
54+
echo sprintf('Topic name: %s', $topics->current()->getTopic()) . PHP_EOL;
55+
echo sprintf('Topic error: %d', $topics->current()->getErr()) . PHP_EOL;
56+
$partitions = $topics->current()->getPartitions();
57+
while ($partitions->valid()) {
58+
echo sprintf(' Topic partition id: %d', $partitions->current()->getId()) . PHP_EOL;
59+
echo sprintf(' Topic partition err: %d', $partitions->current()->getErr()) . PHP_EOL;
60+
echo sprintf(' Topic partition leader id: %d', $partitions->current()->getLeader()) . PHP_EOL;
61+
$replicas = $partitions->current()->getReplicas();
62+
while ($replicas->valid()) {
63+
echo sprintf(' Replicas id: %d', $replicas->current()) . PHP_EOL;
64+
$replicas->next();
65+
}
66+
$inSyncReplicas = $partitions->current()->getIsrs();
67+
while ($inSyncReplicas->valid()) {
68+
echo sprintf(' Insync Replicas id: %d', $inSyncReplicas->current()) . PHP_EOL;
69+
$inSyncReplicas->next();
70+
}
71+
$partitions->next();
72+
}
73+
74+
$topics->next();
75+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /