弹簧批处理结果展平 [英] Flatten processing result in spring batch

查看:54
本文介绍了弹簧批处理结果展平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何在spring-batch(3.0.7)中放平返回实体列表的处理器的结果吗?示例:

Does anyone know how in spring-batch (3.0.7) can I flat a result of processor that returns list of entities? Example:

我有一个返回列表的处理器

I got a processor that returns List

public class MyProcessor implements ItemProcessor < Long , List <Entity>> {
    public List<Entity> process ( Long id )
}

现在,以下所有处理器/编写器都需要处理List<实体>.有什么方法可以将结果简化为简单实体,以便给定步骤中的其他处理器可以在单个实体上工作?

Now all following processors / writers need to work on List < Entity >. Is there any way to flat the result to simply Entity so the further processors in given step can work on single Entities?

唯一的方法是与编写者以某种方式保留列表,然后创建一个单独的步骤,以从保留的数据中进行读取.

The only way is to persist the list somehow with a writer and then create a separate step that would read from the persisted data.

提前谢谢!

推荐答案

如您所知,spring-batch中的处理器可以与复合处理器链接.在链中,您可以在处理器之间更改处理类型,但是两个邻居"处理器的输入和输出类型当然必须匹配.

As you know, processors in spring-batch can be chained with a composite processor. Within the chain, you can change the processing type from processor to processor, but of course input and output type of two "neighbour"-processors have to match.

但是,输入输出输出类型始终被视为一项.因此,如果处理器的输出类型为列表,则将该列表重新列出为一项.因此,后面的处理器需要具有InputType"List",如果写入器紧随其后,则Writer需要具有列表列表作为其写入方法的类型.而且,处理器不能将其元素相乘.每个输入元素只能有一个输出项.

However, Input out Output type is always treated as one item. Therefore, if the output type of a processor ist a List, this list is regared as one item. Hence, the following processor needs to have an InputType "List", resp., if a writer follows, the Writer needs to have a List-of-List as type its write-method. Moreover, a processor can not multiply its element. There can only be one output item for every input element.

基本上,拥有像这样的链并没有错

Basically, there is nothing wrong with having a chain like

Reader<Integer>
ProcessorA<Integer,List<Integer>>
ProcessorB<List<Integer>,List<Integer>>
Writer<List<Integer>> (which leads to a write-method write(List<List<Integer>> items)

根据上下文,可能会有更好的解决方案.您可以使用包装器处理器和包装器编写器来减轻影响(例如,可重用性),例如以下代码示例:

Depending on the context, there could be a better solution. You could mitigate the impact (for instance reuseability) by using wrapper-processors and a wrapper-writer like the following code examples:

public class ListWrapperProcessor<I,O> implements ItemProcessor<List<I>, List<O>> {

    ItemProcessor<I,O> delegate;

    public void setDelegate(ItemProcessor<I,O> delegate) {
        this.delegate = delegate;
    }


    public List<O> process(List<I> itemList) {
        List<O> outputList = new ArrayList<>();

        for (I item : itemList){    
           O outputItem = delegate.process(item);
           if (outputItem!=null) {
               outputList.add(outputItem);
           }
        }

        if (outputList.isEmpty()) {
            return null;
        }

        return outputList;
    }

}


public class ListOfListItemWriter<T> implements InitializingBean, ItemStreamWriter<List<T>> {

    private ItemStreamWriter<T> itemWriter;

    @Override
    public void write(List<? extends List<T>> listOfLists) throws Exception {
        if (listOfLists.isEmpty()) {
            return;
        }

        List<T> all = listOfLists.stream().flatMap(Collection::stream).collect(Collectors.toList());

        itemWriter.write(all);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.notNull(itemWriter, "The 'itemWriter' may not be null");
    }

    public void setItemWriter(ItemStreamWriter<T> itemWriter) {
        this.itemWriter = itemWriter;
    }

    @Override
    public void close() {
        this.itemWriter.close();
    }

    @Override
    public void open(ExecutionContext executionContext) {
        this.itemWriter.open(executionContext);
    }

    @Override
    public void update(ExecutionContext executionContext) {
        this.itemWriter.update(executionContext);
    }
}

使用此类包装程序,您仍然可以实现常规"处理器和编写器,然后使用此类包装程序将列表"处理移出它们.

Using such wrappers, you could still implement "normal" processor and writers and then use such wrappers in order to move the "List"-handling out of them.

这篇关于弹簧批处理结果展平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆