1

一个简单的问题,我正在冒险使用 php-rdkafka ( https://github.com/arnaud-lb/php-rdkafka ) 进入 Kafka。

我浏览了文档,但我找不到要生成到现有主题的语法,除非语法 newTopic 将插入到我怀疑的现有主题中。我不断抛出 Java 错误,而且我不能很好地调试 Java 错误。我正在向那些一直在使用该框架的人寻求帮助,它的语法是否正确?请指教

<?php

$conf = new RdKafka\Conf();
$conf->set('metadata.broker.list', 'localhost:9092');

//If you need to produce exactly once and want to keep the original produce order, uncomment the line below
//$conf->set('enable.idempotence', 'true');

$producer = new RdKafka\Producer($conf);

$topic = $producer->newTopic("test"); // Is this a correct syntax to consume existing topic?

for ($i = 0; $i < 10; $i++) {
    $topic->produce(RD_KAFKA_PARTITION_UA, 0, "Message $i");
    $producer->poll(0);
}

for ($flushRetries = 0; $flushRetries < 10; $flushRetries++) {
    $result = $producer->flush(10000);
    if (RD_KAFKA_RESP_ERR_NO_ERROR === $result) {
        break;
    }
}

if (RD_KAFKA_RESP_ERR_NO_ERROR !== $result) {
    throw new \RuntimeException('Was unable to flush, messages might be lost!');
}
4

1 回答 1

0

根据与开发商的讨论,

以下语法可用于创建新主题并将数据添加到以下/现有主题

$topic = $producer->newTopic("test"); /
于 2020-07-06T10:35:20.083 回答