Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 节点rdkafka:查找结束并获取位置_Node.js_Apache Kafka - Fatal编程技术网

Node.js 节点rdkafka:查找结束并获取位置

Node.js 节点rdkafka:查找结束并获取位置,node.js,apache-kafka,Node.js,Apache Kafka,我正在尝试构建一个消费者,它会阻止某些操作,直到它从头到尾读取所有当前消息 我找不到任何方法来查找某个主题的邮件数。我目前的想法是寻找最新的检查位置,然后返回最早的 我当前的实现看起来像 class KafkaReader { ... private _connectConsumer() { this._logger.debug('Starting consumer with ', this._config.brokerList, this._config.groupId);

我正在尝试构建一个消费者,它会阻止某些操作,直到它从头到尾读取所有当前消息

我找不到任何方法来查找某个主题的邮件数。我目前的想法是寻找
最新的
检查位置,然后返回
最早的

我当前的实现看起来像

class KafkaReader {
...
private _connectConsumer() {
        this._logger.debug('Starting consumer with ', this._config.brokerList, this._config.groupId);
        const conf = {
            'metadata.broker.list': this._config.brokerList,
            'group.id': this._config.groupId,
        };

        if (this._config.debug) {
            conf['debug'] = this._config.debug;
        }

        this._consumer = new KafkaConsumer(conf, {
            'auto.offset.reset': 'earliest' // consume from the start
        });


        this._consumer.on('data', (message: ConsumerStreamMessage) => {
            if (this._config.debug) {
                this._logger.debug('Received message', message.topic, message.value.toString('utf8'));
            }

            this._subject.next(message.value.toString('utf8'));
        });

        this._consumer.on('ready', (arg) => { this._onConsumerReady(arg); });

        this._consumer.connect();
    }

    private _onConsumerReady(arg: any) {
        this._logger.debug('Kafka consumer ready.' + JSON.stringify(arg));

        this._consumer.subscribe([this._topic.topicId]);

        this._logger.debug('before seek end', this._consumer.position(null));
        this._consumer.seek({'topic': this._topic.topicId, 'offset': 'latest', 'partition': 0}, 0, (b) => {
            const end = this._consumer.position(null);
            this._logger.debug('end', end, b);
            this._seekToEarliestAndConsume();
        });
    }

    private _seekToEarliestAndConsume() {
        this._consumer.seek({'topic': this._topic.topicId, 'offset': 'earliest', 'partition': 0}, 0, () => {
            this._consumer.consume();
            this._startUpSequence.next(true);
            this._startUpSequence.complete();
        });
    }
}
我得到输出:

[2019-08-01T13:13:15.156] [DEBUG] kafka-reader - before seek end []
[2019-08-01T13:13:15.157] [DEBUG] kafka-reader - end [] { Error: Local: Erroneous state
    at Function.createLibrdkafkaError [as create] (node_modules/node-rdkafka/lib/error.js:334:10)
    at node_modules/node-rdkafka/lib/kafka-consumer.js:229:26
  message: 'Local: Erroneous state',
  code: -172,
  errno: -172,
  origin: 'kafka' }
看起来第二个seek工作正常,但第一个seek给了我
Local:error状态

  • 有人知道如何获取最后一条消息的位置吗
  • 为什么在尝试查找最新版本时抛出
    Local:error state
Seek要求指定分区,是否有其他分区


谢谢。

不确定为什么会出现此错误,但您不应该改用:

queryWatermarkOffsets(topic, partition, timeout, cb)
Query offsets from the broker. This function makes a call to the broker to get the current low (oldest/beginning) and high (newest/end) offsets for a topic partition.
从这里开始:


Yannick

您需要在所有分区上都有消息才能搜索,否则您将得到此错误。我经历了同样的错误,后来从seek中删除了没有消息的分区,它解决了这个错误。不确定,如果有帮助的话,但考虑将其放在这里。

不太了解node.js实现,但标准使用者API既有
公共映射起始偏移量(集合分区,持续时间超时)
也有
公共映射内偏移量(集合分区)
方法,不需要更改使用者位置。看见