Spring Batch和Spring集成 [英] Spring Batch and Spring Integration
问题描述
我想使用Spring Batch和Spring Integration从数据库导入数据,并将其写入文件,然后通过ftp将其传输到远程服务器.
I want to use Spring Batch and Spring Integration to import data from database and write them into a file and ftp them to a remote server.
但是我想我的问题是我不想为我的表创建域对象.我的查询是随机的,我需要一些可以读取数据并将其写入文件并进行传输的东西.
But I guess my problem is I don't want to create Domain Object for my table. My queries are random and I want something that just reads the data and writes it to files and transfer.
我可以在不创建各自的域对象的情况下使用Spring Batch和Integration吗?
Can I use Spring Batch and Integration without creating respective domain objects?
推荐答案
绝对.您可以将JDBC ItemReader
或JPA ItemReader
与ColumnMapRowMapper
一起使用,以检索结果集的Map
.您可以非常简单地使用FlatFileItemWriter
以任意格式输出数据(使用提供的类很容易定界;固定宽度表示编写一个类来将Map
转换为固定宽度字符串).
Absolutely. You can use either of the JDBC ItemReader
s or the JPA ItemReader
with a ColumnMapRowMapper
to retrieve a Map
of the result set. You can use the FlatFileItemWriter
pretty simply to output the data in whatever format you like (delimited being very easy with the provided classes; fixed width means writing one class to translate the Map
to your fixed width string).
我经常在Spring Batch中执行此操作,这几乎只是将事情连接起来的问题.
I do this pretty often with Spring Batch and it's pretty much just a matter of wiring things up.
除了定义资源,数据源和提供SQL之外,这种(未经测试的)配置几乎可以完全满足您的要求:
Aside from defining a resource, data source, and providing the SQL, this (untested) configuration would pretty much do exactly what you're asking:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/batch"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<job-repository id="jobRepository"
data-source="jobDataSource"/>
<beans:bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="jobDataSource" />
<beans:bean id="extractReader" scope="step"
class="org.springframework.batch.item.database.JdbcCursorItemReader">
<beans:property name="dataSource" ref="appDataSource" />
<beans:property name="rowMapper">
<beans:bean
class="org.springframework.jdbc.core.ColumnMapRowMapper" />
</beans:property>
<beans:property name="sql">
<beans:value>
. . .
</beans:value>
</beans:property>
</beans:bean>
<beans:bean id="extractWriter"
class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
<beans:property name="resource" ref="fileResource" />
<beans:property name="lineAggregator">
<beans:bean
class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<beans:property name="delimiter">
<util:constant
static-field="org.springframework.batch.item.file.transform.DelimitedLineTokenizer.DELIMITER_TAB" />
</beans:property>
<beans:property name="fieldExtractor">
<beans:bean
class="org.springframework.batch.item.file.transform.PassThroughFieldExtractor" />
</beans:property>
</beans:bean>
</beans:property>
</beans:bean>
<job id="extractJob" restartable="true">
<step id="extractStep" >
<tasklet>
<chunk reader="extractReader" writer="extractWriter"
commit-interval="100" />
</tasklet>
</step>
</job>
</beans:beans>
这篇关于Spring Batch和Spring集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!