These are chat archives for arnaud-lb/php-rdkafka

17th
Oct 2018
Steven Lloyd
@slloyd88
Oct 17 2018 15:15
Can anyone show me how to get a list of available topics?
Here is my failed attempt:
$md = new RdKafka\Metadata();
$md->addBrokers("somehost:9092");
$topics=$md->getTopics();
Steven Lloyd
@slloyd88
Oct 17 2018 16:51
OK, figured it out. Here is a function to get 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;
}