apache点燃datastreamer如何将数据设置为ignitefuture?

时间:2018-03-29 10:52:46

标签: future ignite data-stream

我在apache点火中创建一个批量数据流,需要控制数据接收后发生的事情。 我的批次有一个结构:

public class Batch implements Binarylizable, Serializable {

    private String eventKey;
    private byte[] bytes;

    etc..

然后我尝试流式传输数据:

 try (IgniteDataStreamer<Integer, Batch> streamer = serviceGrid.getIgnite().dataStreamer(cacheName);
             StreamBatcher batcher = StreamBatcherFactory.create(event) ){
            streamer.receiver(StreamTransformer.from(new BatchDataProcessor(event)));
            streamer.autoFlushFrequency(1000);
            streamer.allowOverwrite(true);
            statusService.updateStatus(event.getKey(), StatusType.EXECUTING);
            int counter = 0;
            Batch batch = null;
            IgniteFuture<?> future = null;
            while ((batch = batcher.batch()) != null) {
                future = streamer.addData(counter++, batch);
            }
            Object getted = future.get();

仅供测试使用,让我们只获取最后的未来,并尝试分析此对象。在上面的代码中,我使用的是BatchDataProcessor,如下所示:

public class BatchDataProcessor implements CacheEntryProcessor<Integer, Batch, Object> {

    private final Event event;
    private final String eventKey;

    public BatchDataProcessor(Event event) {
        this.event = event;
        this.eventKey = event.getKey();
    }

    @Override
    public Object process(MutableEntry<Integer, Batch> mutableEntry, Object... objects) throws EntryProcessorException {
        Node node = NodeIgniter.node(Ignition.localIgnite().cluster().localNode().id());
        ServiceGridContainer container = (ServiceGridContainer) node.getEnvironmentContainer().getContainerObject(ServiceGridContainer.class);
        ProcessMarshaller marshaller = (ProcessMarshaller) container.getService(ProcessMarshaller.class);
        LocalProcess localProcess = marshaller.intoProccessing(event.getLambdaExecutionKey());
        try {
            localProcess.addBatch(mutableEntry);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return new String("111");
        }
    }
}

所以在localProcess.addBatch(mutableEntry)之后我想发回一个关于这个特定批次状态的信息,所以我认为我应该在IgniteFuture对象中做这个,但我没有找到任何有关如何控制的信息在addData函数中收到的future对象。

任何人都可以帮助理解,在哪里可以控制在addData函数中接收的未来或者其他一些方法来实现对流批处理的回调?

1 个答案:

答案 0 :(得分:2)

执行StreamTransformer.from()后,您将丧失BatchDataProcessor的结果,因为

for (Map.Entry<K, V> entry : entries)
    cache.invoke(entry.getKey(), this, entry.getValue());
//  ^ result of cache.invoke() is discarded here

DataStreamer用于单向数据流。据我所知,它不应该返回值。

如果您依赖cache.invoke()的结果,我建议您直接调用它而不是依赖DataStreamer

BTW,小心fut.get()。你应该先dataStreamer.flush(),否则DataStreamer的期货会无限期等待。