简体   繁体   English

Nifi 自定义 Kafka 处理器代码在有限的时间内工作

[英]Nifi custom Kafka processor code works for a limited time

This is the code for my custom kafka processor that simply consumes from a kafka topic and produces some data这是我的自定义 kafka 处理器的代码,它只是从 kafka 主题中消费并产生一些数据

ConsumerRecords<byte[],byte[]> records = consumer.poll(1000);
records.forEach(record -> {
    FlowFile flowFile = session.create();
    if (flowFile == null) {
       return;
    }
    try {
       byte[] outputBytes = (record == null) ? EMPTY_JSON_OBJECT : 
       genericData.toString(record.value()).getBytes(StandardCharsets.UTF_8);
       flowFile = session.write(flowFile, rawOut -> {
           rawOut.write(outputBytes);
           consumer.commitSync();
           });
    } catch (ProcessException pe) {
       getLogger().error("Failed to deserialize {}", new Object[]{flowFile, pe});
       session.transfer(flowFile, REL_FAILURE);
       return;
    }
    flowFile = session.putAttribute(flowFile, "topic", record.topic());
    flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), "application/json");
    getLogger().info("flowFile id " + flowFile.getId());
    session.transfer(flowFile, REL_SUCCESS);
});

This code takes a batch of around 500 kakfa messages and produces some flowFile for output.这段代码需要一批大约 500 条 kakfa 消息,并为 output 生成一些流文件。 What I need is obviously to put it inside a while loop that does the same thing over and over again.我需要的显然是将它放在一个while循环中,一遍又一遍地做同样的事情。 When I do that though, nothing gets out of the processor.但是,当我这样做时,处理器中没有任何东西。 While still, the info log shows the flowFile ids are incremented, and seems the actual flowFile is produced.虽然仍然,信息日志显示 flowFile ids 增加了,并且似乎产生了实际的 flowFile。 One thing I tested is this happens only in infinite while loops.我测试的一件事是这只发生在无限的while循环中。 When I use a limited for loops the processor works fine.当我使用有限的 for 循环时,处理器工作正常。 I am wondering there might be something about nifi flow internal that I am not aware of.我想知道可能有一些关于 nifi flow internal 我不知道的东西。

The problem is I wasn't committing the session manually.问题是我没有手动提交 session 。 So it got committed only when the method returned which never happened in the case of an infinite while loop.所以它只有在方法返回时才被提交,这在无限循环的情况下从未发生过。 The contrived solution ended up being something like this.人为的解决方案最终变成了这样。

while(true)
    ConsumerRecords<byte[],byte[]> records = consumer.poll(Duration.ofMillis(1000));
    records.forEach(record -> {
        FlowFile flowFile = session.create();
        if (flowFile == null) {
           return;
        }
        try {
           byte[] outputBytes = (record == null) ? EMPTY_JSON_OBJECT : 
           genericData.toString(record.value()).getBytes(StandardCharsets.UTF_8);
           flowFile = session.write(flowFile, rawOut -> {
               rawOut.write(outputBytes);
               consumer.commitSync();
               });
        } catch (ProcessException pe) {
           getLogger().error("Failed to deserialize {}", new Object[]{flowFile, pe});
           session.transfer(flowFile, REL_FAILURE);
           return;
        }
        flowFile = session.putAttribute(flowFile, "topic", record.topic());
        flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), "application/json");
        getLogger().info("flowFile id " + flowFile.getId());
        session.transfer(flowFile, REL_SUCCESS);
        session.commit();
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM