Home > OS >  How can I create topic with kafka-php programming library?
How can I create topic with kafka-php programming library?

Time:03-17

I want create a topic with kafka-php programming library.

https://github.com/weiboad/kafka-php

How can I do?

If topic doesn't exist in kafka server, executing this code, it doesn't work. It doesn't insert 10 messages "test1....message." because topic isn't create.

date_default_timezone_set('PRC');
// Create the logger
$logger = new Logger('my_logger');
// Now add some handlers
$logger->pushHandler(new StreamHandler(DIR . '/app.log', Logger::DEBUG));

$config = \Kafka\ProducerConfig::getInstance();
$config->setMetadataRefreshIntervalMs(10000);
$config->setMetadataBrokerList('127.0.0.1:9092');
$config->setBrokerVersion('1.0.0');
$config->setRequiredAck(1);
$config->setIsAsyn(false);
$config->setProduceInterval(500);
$producer = new \Kafka\Producer();
$producer->setLogger($logger);

for($i = 0; $i < 10; $i  ) {
$result = $producer->send([
[
'topic' => 'test1',
'value' => 'test1....message.',
'key' => ''
]
]);
}

CodePudding user response:

I don't think there is support for this in the available PHP libraries.

The functionality is implemented in the Admin API in the Java client library for Kafka which will enable you to create a topic. I found this github issue against another PHP Kafka client regarding implementing the Admin API but it has not been done.

  • Related