如何从多个数据库中读取项目?我已经知道文件可以了 以下示例适用于从多个文件中读取
...
<job id="readMultiFileJob" xmlns="http://www.springframework.org/schema/batch">
<step id="step1">
<tasklet>
<chunk reader="multiResourceReader" writer="flatFileItemWriter"
commit-interval="1" />
</tasklet>
</step>
</job>
...
<bean id="multiResourceReader"
class=" org.springframework.batch.item.file.MultiResourceItemReader">
<property name="resources" value="file:csv/inputs/domain-*.csv" />
<property name="delegate" ref="flatFileItemReader" />
</bean>
...
<bean id="database2" class="org.springframework.batch.item.database.JdbcCursorItemReader">
<property name="name" value="database2Reader" />
<property name="dataSource" ref="dataSource2" />
<property name="sql" value="select image from object where image like '%/images/%'" />
<property name="rowMapper">
<bean class="sym.batch.ImagesRowMapper2" />
</property>
</bean>
答案 0 :(得分:12)
没有一个随时可用的组件可以执行您的要求;唯一的解决方案是编写一份自定义ItemReader<>
,代表JdbcCursorItemReader
(或HibernateCursorItemReader
或任何通用ItemReader
实施)。
您需要准备所有必要的东西(数据源,会话,真实的数据库阅读器)并将所有委派的读者绑定到您的自定义阅读器。
编辑:
您需要使用ItemReader.read()
和mantain reader的重复来模拟循环,并在作业重新启动时委托状态。
class MyItemReader<T> implements ItemReader<T>, ItemStream {
private ItemReader[] delegates;
private int delegateIndex;
private ItemReader<T> currentDelegate;
private ExecutionContext stepExecutionContext;
public void setDelegates(ItemReader[] delegates) {
this.delegates = delegates;
}
@BeforeStep
private void beforeStep(StepExecution stepExecution) {
this.stepExecutionContext = stepExecution.getExecutionContext();
}
public T read() {
T item = null;
if(null != currentDelegate) {
item = currentDelegate.read();
if(null == item) {
((ItemStream)this.currentDelegate).close();
this.currentDelegate = null;
}
}
// Move to next delegate if previous was exhausted!
if(null == item && this.delegateIndex< this.delegates.length) {
this.currentDelegate = this.delegates[this.currentIndex++];
((ItemStream)this.currentDelegate).open(this.stepExecutionContext);
update(this.stepExecutionContext);
// Recurse to read() to simulate loop through delegates
item = read();
}
return item;
}
public void open(ExecutionContext ctx) {
// During open restore last active reader and restore its state
if(ctx.containsKey("index")) {
this.delegateIndex = ctx.getInt("index");
this.currentDelegate = this.delegates[this.delegateIndex];
((ItemStream)this.currentDelegate ).open(ctx);
}
}
public void update(ExecutionContext ctx) {
// Update current delegate index and state
ctx.putInt("index", this.delegateIndex);
if(null != this.currentDelegate) {
((ItemStream)this.currentDelegate).update(ctx);
}
}
public void close(ExecutionContext ctx) {
if(null != this.currentDelegate) {
((ItemStream)this.currentDelegate).close();
}
}
<bean id="myItemReader" class=path.to.MyItemReader>
<property name="delegates">
<array>
<ref bean="itemReader1"/>
<ref bean="itemReader2"/>
<ref bean="itemReader3"/>
</array>
</property>
</bean>
EDIT2:记得设置属性名称;让MyItemReader.read()正常工作是必要的
<bean id="itemReader1" class="JdbcCursorItemReader">
<property name="name" value="itemReader1" />
<!-- Set other properties -->
</bean>
答案 1 :(得分:1)
我建议一个简单的解决方法可能不适用于所有情况,但在许多情况下会有用:
只需定义:
这两个步骤几乎完全相同,它们引用相同的处理器和写入器,但它们有不同的读者。它们将被连续调用。
此设置是否有效取决于处理器和编写器(在不同步骤中调用时它们是否仍能正常工作)。就我而言,将appendAllowed=true
设置为编写器就足够了,这样两个步骤都可以写入同一个文件。
答案 2 :(得分:0)
我建议一个棘手的方法。如果我们假设一个是您的mysql数据源的表是基础的,并且该表中的每一行都对应于其他mysql数据源表的行(就像位于不同数据源中的连接表),您可以在批处理作业项目读取器中执行此操作。出于这种方式;
Spring DataSource配置;
<bean id="mySqlDataSource1" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${database1.driverClassName}"/>
<property name="url" value="${database1.url}"/>
<property name="username" value="${database1.username}"/>
<property name="password" value="${database1.password}"/>
<property name="validationQuery" value="${database1.validationQuery}"/>
</bean>
<bean id="mySqlDataSource2" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${database2.driverClassName}"/>
<property name="url" value="${database2.url}"/>
<property name="username" value="${database2.username}"/>
<property name="password" value="${database2.password}"/>
<property name="validationQuery" value="${database2.validationQuery}"/>
</bean>
您的batch-job.xml
<bean id="multiDatasorceReader" class="org.springframework.batch.item.database.JdbcCursorItemReader" scope="step">
<property name="dataSource" ref="mySqlDataSource1" />
<property name="rowMapper" ref="multiDatasourceRowMapper" />
<property name="sql">
<value>
SELECT * FROM xyz
</value>
</property>
</bean>
<bean id="multiDatasourceRowMapper" class="yourpackage.MultiDatasourceRowMapper" scope="step">
<property name="secondDataSource" ref="mySqlDataSource2" />
<property name="secondSql">
<value>
SELECT * FROM abc
</value>
</property>
</bean>
你的RowMapper看起来像;
public class MultiDatasourceRowMapper implements RowMapper<String> {
private DataSource secondDataSource;
private String secondSql;
public String mapRow(ResultSet rs, int arg1) throws SQLException {
Connection conn = secondDataSource.getConnection();
PreparedStatement prep = conn.prepareStatement(secondSql);
// Do Something
return "";
}
public void setSecondDataSource(DataSource secondDataSource) {
this.secondDataSource = secondDataSource;
}
public void setSecondSql(String secondSql) {
this.secondSql = secondSql;
}
}