[英]How to get only latest records in php kafka
I am using arnaud-lb/php-rdkafka as PHP kafka client.我正在使用arnaud-lb/php-rdkafka作为 PHP kafka 客户端。 I am using Windows 10 and php 7.4.我正在使用 Windows 10 和 php 7.4。 I am trying to get only the latest records via consumer, but it is returning all records.我试图通过消费者仅获取最新记录,但它正在返回所有记录。 I am not sure whether it is not committing offset as per my following code or there is any other reason.我不确定它是否没有按照我的以下代码提交偏移量或者是否有任何其他原因。 I was also trying to find how to commit offset if it is not automatically happening but could not figure out anything.我还试图找到如何提交偏移量,如果它不是自动发生但无法弄清楚任何事情。
$conf = new RdKafka\Conf();
//myConsumerGroup
$conf->set('group.id', 'myConsumerGroup'.date('Ymdhis'));
$rk = new RdKafka\Consumer($conf);
$rk->addBrokers("127.0.0.1");
$topicConf = new RdKafka\TopicConf();
$topicConf->set('auto.commit.interval.ms', 100);
$topicConf->set('offset.store.method', 'broker');
$topicConf->set('auto.offset.reset', 'earliest');
$topic = $rk->newTopic("TestTopic", $topicConf);
//RD_KAFKA_OFFSET_BEGINNING RD_KAFKA_OFFSET_STORED
$topic->consumeStart(0, RD_KAFKA_OFFSET_STORED);
echo '<pre>';
while (true) {
$message = $topic->consume(0, 2*10000);
switch ($message->err) {
case RD_KAFKA_RESP_ERR_NO_ERROR:
print_r($message);
break;
case RD_KAFKA_RESP_ERR__PARTITION_EOF:
echo "No more messages; will wait for more\n";
break;
case RD_KAFKA_RESP_ERR__TIMED_OUT:
echo "Timed out\n";
break;
default:
throw new \Exception($message->errstr(), $message->err);
break;
}
}
I was also trying to find how to commit offset我也试图找到如何提交偏移量
Set $conf->set('enable.auto.commit', 'true');
设置$conf->set('enable.auto.commit', 'true');
or use an explicit $rk->commit($message);
或者使用明确的$rk->commit($message);
RdKafka\KafkaConsumer::commit . RdKafka\KafkaConsumer::提交。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.