我是 Spring Batch 的新手,并尝试实现一个批处理作业,我
- 从 MySQL 数据库读取
- 将结果写入 CSV 文件
- 对 MySQL 结果集进行一些处理并写入另一个数据库。
我浏览过this question on StackOverflow ,但主要接受的答案本质上是实现从数据库读取两次的两个步骤:
<job id="myJob">
<step id="step1" next="step2">
<tasklet>
<chunk reader="reader" writer="typeAwriter"/>
</tasklet>
</step>
<step id="step2">
<tasklet>
<chunk reader="reader" processor="processor" writer="typeBwriter"/>
</tasklet>
</step>
</job>
难道没有比从 MySQL 数据库读取两次更有效的方法吗?例如,如果您的查询非常大并拖累系统性能怎么办?
最佳答案
你需要的是一个 block 策略而不是tasklet。 ItemReader 将从数据库中读取 block ,处理器将处理您的数据,然后您可以将每个项目将它们发送到可以写入数据库和文件的 ItemWriter。这是许多可能的策略之一,我不知道您的业务逻辑的详细信息,但我认为这些信息足以让您继续自己的想法。
<?xml version="1.0" encoding="UTF-8"?>
<job id="customerJob" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/jobXML_1_0.xsd"
version="1.0">
<step id="step1">
<chunk item-count="5">
<reader ref="itemReader"/>
<processor ref="itemProcessor"/>
<writer ref="itemWriter"/>
</chunk>
</step>
</job>
这是JSR-352 XML类型,对于Spring你有相应的方法。
关于Spring Batch从数据库读取一次,写入另一个数据库并写入CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44006824/