Spring Batch 之 Sample(复合格式文件的读、多文件的写)(七)

本文介绍了如何使用Spring Batch处理复合格式文件,将学生和商品信息分类后分别写入两个文件。通过PatternMatchingCompositeLineMapper配置不同tokenizer和fieldSetMapper,根据记录前缀匹配进行解析映射。学生信息写入固定长度格式文件,商品信息写入CSV格式文件,未涉及Processor配置。
摘要由CSDN通过智能技术生成

 前面关于Spring Batch的文章,讲述了SpringBatch对CSV文件的读写操作对XML文件的操作,以及对固定长格式文件的操作。这些事例,同一个Reader读取的都是相同格式的数据,最终写入一个文件。如果遇到下面这样的数据,并想将学生信息和商品信息分类后写入两个文件,应该如何处理呢?

student,200001,ZhangSan,18,78
goodsPNH001011000200.1zhangshana2011/12/18 01:12:36
student,200002,LiSi,19,79
goodsPNH001022000300.1zhangshanb2011/12/19 01:12:36
student,200003,WangWu,20,80
goodsPNH001033000400.1zhangshanc2011/12/20 01:12:36

  * 以student开头的数据代表学生信息,以goods开头代表商品信息

这次将和大家一起探讨Spring Batch读取复合格式的数据,然后写入不同的文件的处理方式。

      工程结构如下图:

       applicationContext.xml和log4j.xml前文已经叙述过,在此不做赘述。

      本实例的核心配置文件batch.mxl内容如下:

复制代码
  1<?xml version="1.0" encoding="UTF-8"?>
2<bean:beans xmlns="http://www.springframework.org/schema/batch"
3 xmlns:bean="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
6 xmlns:util="http://www.springframework.org/schema/util"
7 xsi:schemaLocation="http://www.springframework.org/schema/beans
8http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
9http://www.springframework.org/schema/tx
10http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
11http://www.springframework.org/schema/aop
12http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
13http://www.springframework.org/schema/context
14http://www.springframework.org/schema/context/spring-context-2.5.xsd
15http://www.springframework.org/schema/batch
16http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
17http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
18
19<bean:import resource="applicationContext.xml"/>
20<!-- Job的配置信息 -->
21<job id="multiTypeSingleFileJob">
22<step id="xmlFileReadAndWriterStep">
23<tasklet>
24<chunk reader="multiTypesItemReader" writer="multiTypesItemWriter"
25 commit-interval="1">
26<streams>
27<stream ref="studentWriter"/>
28<stream ref="goodsWriter"/>
29</streams>
30</chunk>
31</tasklet>
32</step>
33
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值