使用 Spring Batch 读取一条记录/项目并写入多条记录/项目 [英] Read one record/item and write multiple records/items using spring batch

查看:53
本文介绍了使用 Spring Batch 读取一条记录/项目并写入多条记录/项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了一些搜索,但找不到任何示例/示例.

I did some searching but couldn't find any sample/example.

我要求读取一张表(输入)中的地理坐标,并对其进行处理以生成与坐标关联的 POI.因此,一个地理坐标将导致一个或多个 POI 需要插入到另一个表中(输出).

I've a requirement where geo coordinates from one table (input) are read, processed to generate POI's associated to coordinate. So one geo coordinate will result in one or more POI's that needs to be inserted into another table (output).

我目前正在使用 JdbcCursorItemReader 和 JdbcBatchItemWriter 读取一项/记录并写入一项/记录.还有一个 ItemProcessor 可以为给定的地理坐标生成 POI.

I'm currently using a JdbcCursorItemReader and JdbcBatchItemWriter to read one item/record and write one item/record. There is also an ItemProcessor that generates the POI's for a give geo coordinate.

自定义 JdbcBatchItemWriter 是否可以帮助我实现这一目标?

Does a custom JdbcBatchItemWriter help me achieve this?

有什么想法吗?TIA.

Any ideas? TIA.

推荐答案

如果您只想将项目传播给不同的作者(读取重复输出),您可以使用现有的 CompositeItemWriter

if you just want to spread the items to different writers (read duplicate output) you can use the existing CompositeItemWriter

但我不确定您的处理器是否会生成不同的项目类型,或者您是否想将一种复杂项目类型的内容传播给多个作者,对于这些情况,您可以为 多行记录-作者问题

but i'm not sure if your processor will produce different item types or if you want to spread the content of one complex item type to multiple writers, for those cases you can use a slightly changed version for a multiline-record-writer question

public class MultiOutputItemWriter implements ItemWriter<Object> {

private JdbcBatchItemWriter<ClassFoo> delegateFoo;
private JdbcBatchItemWriter<ClassBar> delegateBar;

public void write(List<? extends Object> items) throws Exception {
       // if you have different types of items
       // check Object Class
       // add to new List<Classfoo>
       // call delegate e.g. delegateFoo.write(List with ClassFoo);
       //
       // or if you have complex objects
       // same procedure as above, but with
       // add to new List<Classfoo> with item.getClassFoo
 }
}

如果您使用 FlatFileItemWriter,请不要忘记 注册委托为 ItemStreams(因此春季批次将为您打开/关闭它们)

if you use FlatFileItemWriter, do not forget to register the delegates as ItemStreams (so spring batch will open/close them for you)

这篇关于使用 Spring Batch 读取一条记录/项目并写入多条记录/项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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