How to get a list of topics

95 views
Skip to first unread message

steve...@gmail.com

unread,
Oct 17, 2018, 11:17:51 AM10/17/18
to php-rdkafka
Can anyone show me how to get a list of topics?  Here is my failed attempt:

$md = new RdKafka\Metadata();
 $md
->addBrokers("somehost:9092");
 $topics
=$md->getTopics();

steve...@gmail.com

unread,
Oct 17, 2018, 12:57:17 PM10/17/18
to php-rdkafka
OK, I figured it out. Here is a function that gets the topics.
function kafkaGetTopics(){
    $pk
= new RdKafka\Producer();
    $pk
->addBrokers("servername:9092");
    $conf
= new RdKafka\Conf();
   
// Set the group id. This is required when storing offsets on the broker
    $conf
->set('group.id', 'yourspecialidgoeshere');
    $rk
= new RdKafka\Consumer($conf);
    $rk
->addBrokers("servername:9092");
    $sources
= [$pk,$rk];
    $topics
= [];
   
foreach ($sources as $rdk) {
      $rdk
->setLogLevel(LOG_INFO);
      $meta
= $rdk->getMetadata(TRUE, null, 100);
      $rkTopics
= $meta->getTopics();
     
foreach ($rkTopics as $topic) {
        $topics
[$topic->getTopic()] = TRUE;
     
}
   
}
    $keys
= array_keys($topics);
    $topics
=array();
   
foreach($keys as $i=>$topic){
       
if(preg_match('/^\_\_/',$topic)){continue;}
        $topics
[]=$topic;
   
}
    sort
($topics);
   
return $topics;
}

Reply all
Reply to author
Forward
0 new messages