javascript
全网最详细SpringBatch读(Reader)混合文件讲解
文章列表
- 一、读混合记录文件
- 1、FieldSetMapper实现
- 2、LineTokenizer实现
- 3、混合读reader实现类
- 4、读混合文件job配置
- 5、读混合文件processor
- 6、读混合文件writer
写在前面:
我是「境里婆娑」。我还是从前那个少年,没有一丝丝改变,时间只不过是考验,种在心中信念丝毫未减,眼前这个少年,还是最初那张脸,面前再多艰险不退却。
写博客的目的就是分享给大家一起学习交流,如果您对 Java感兴趣,可以关注我,我们一起学习。
前言:在工作中可能会遇到一个文件存在多种不同的记录需要处理,不要慌SpringBatch已经给我们预留好接口处理这类文件。下面让我们来一探究竟把。
一、读混合记录文件
一般情况下读文件中的记录格式是一致的,在特殊情况下一个文件中存在多种不同的记录格式,通过特定的开头可以区分不同的记录。例如下面文件,以41开头代表信用卡消费记录,以31开头代表借记卡消费记录。
SpringBatch框架对文件中存在不同记录格式同样有友好的支持,通过复杂数据转换类PatternMatchingCompositeLineMapper,可以为不同记录定义不同的LineTokenizer和FieldSetMapper进行数据转换。
混合记录读主要类图如下:
PatternMatchingCompositeLineMapper:为不同的记录转变不同的LineTokenizer和FieldSetMapper实现数据转换
1、FieldSetMapper实现
1、CreditBillFieldSetMapper借记卡数据转换类,将3*格式的记录转换为领域对象CreditBill
/*** @author shuliangzhao* @date 2020/12/6 14:22*/ public class CreditBillFieldSetMapper implements FieldSetMapper<CreditAndDebitBill> {@Overridepublic CreditAndDebitBill mapFieldSet(FieldSet fieldSet) throws BindException {CreditAndDebitBill creaditAndDebitBill = new CreditAndDebitBill();CreditBill creditBill = new CreditBill();creditBill.setName(fieldSet.readString("name"));creditBill.setDate(fieldSet.readDate("date"));creditBill.setAmout(fieldSet.readInt("amout"));creditBill.setAddress(fieldSet.readString("address"));creditBill.setAcctid(fieldSet.readString("acctid"));creaditAndDebitBill.setCreditBill(creditBill);return creaditAndDebitBill;} }2、DebitBillFieldSetMapper贷记卡数据转换类,将4*格式的记录转换为领域对象DebitBill
/*** @author shuliangzhao* @date 2020/12/6 14:26*/ public class DebitBillFieldSetMapper implements FieldSetMapper<CreditAndDebitBill> {@Overridepublic CreditAndDebitBill mapFieldSet(FieldSet fieldSet) throws BindException {CreditAndDebitBill creditAndDebitBill = new CreditAndDebitBill();DebitBill debitBill = new DebitBill();debitBill.setAcctid(fieldSet.readString("acctid"));debitBill.setAmout(fieldSet.readInt("amout"));debitBill.setDate(fieldSet.readDate("date"));debitBill.setName(fieldSet.readString("name"));creditAndDebitBill.setDebitBill(debitBill);return creditAndDebitBill;} }2、LineTokenizer实现
1、CreditBillTokenizer借记卡分隔器处理类
/*** @author shuliangzhao* @date 2020/12/6 14:03*/ public class CreditBillTokenizer extends DelimitedLineTokenizer {@Overridepublic FieldSet tokenize(String line) {return super.tokenize(line);} }2、DebitBillTokenizer贷记卡分隔器处理类
/*** @author shuliangzhao* @Title: DebitBillTokenizer* @ProjectName spring-boot-learn* @Description: TODO* @date 2020/12/6 14:06*/ public class DebitBillTokenizer extends DelimitedLineTokenizer {@Overridepublic FieldSet tokenize(String line) {return super.tokenize(line);} }3、混合读reader实现类
混合读文件PatternMatchFileReader封装
/*** 读取混合文件reader* @author shuliangzhao* @date 2020/12/6 13:55*/ public class PatternMatchFileReader extends FlatFileItemReader {public PatternMatchFileReader(Class clz,Class clz1) {setResource(CommonUtil.createResource("D:\\aplus\\muliti\\pattern.csv"));PatternMatchingCompositeLineMapper patternMatchingCompositeLineMapper = new PatternMatchingCompositeLineMapper();Map<String, LineTokenizer> map = new HashMap<>();createTokenizer(map,clz,clz1);patternMatchingCompositeLineMapper.setTokenizers(map);Map<String, FieldSetMapper> map1 = new HashMap<>();createFieldSetMapper(map1,clz,clz1);patternMatchingCompositeLineMapper.setFieldSetMappers(map1);setLineMapper(patternMatchingCompositeLineMapper);}private void createFieldSetMapper(Map<String, FieldSetMapper> map1,Class clz,Class clz1) {DebitBillFieldSetMapper debitBillFieldSetMapper = new DebitBillFieldSetMapper();CreditBillFieldSetMapper creditBillFieldSetMapper = new CreditBillFieldSetMapper();map1.put("4*",creditBillFieldSetMapper);map1.put("3*",debitBillFieldSetMapper);}private void createTokenizer(Map<String, LineTokenizer> map,Class clz,Class clz1) {CreditBillTokenizer creditBillTokenizer = new CreditBillTokenizer();creditBillTokenizer.setNames(CommonUtil.names(clz));creditBillTokenizer.setDelimiter(",");DebitBillTokenizer debitBillTokenizer = new DebitBillTokenizer();debitBillTokenizer.setNames(CommonUtil.names(clz1));debitBillTokenizer.setDelimiter(",");map.put("4*",creditBillTokenizer);map.put("3*",debitBillTokenizer);} }注意:map存放key为4和3。至于为什么要这样存放请看PatternMatcher这个match方法
4、读混合文件job配置
读混合文件job基于javabean配置如下
/*** @author shuliangzhao* @date 2020/12/6 14:18*/ @Configuration @EnableBatchProcessing public class PatternMatchConfigruation {@Autowiredprivate JobBuilderFactory jobBuilderFactory;@Autowiredprivate StepBuilderFactory stepBuilderFactory;@Autowiredprivate PatternMatchProcessor patternMatchProcessor;@Autowiredprivate PatternMatchWriter patternMatchWriter;@Beanpublic Job patternMatchJob() {return jobBuilderFactory.get("patternMatchJob").start(patternMatchStep()).build();}@Beanpublic Step patternMatchStep() {return stepBuilderFactory.get("patternMatchStep").<CreditAndDebitBill,CreditAndDebitBill>chunk(2).reader(patternMatchFileReader()).processor(patternMatchProcessor).writer(patternMatchWriter).build();}@Bean@StepScopepublic PatternMatchFileReader patternMatchFileReader() {return new PatternMatchFileReader(CreditBill.class, DebitBill.class);} }5、读混合文件processor
PatternMatchProcessor实现如下:
/*** @author shuliangzhao* @date 2020/12/6 14:32*/ @Component @StepScope public class PatternMatchProcessor implements ItemProcessor<CreditAndDebitBill,CreditAndDebitBill> {@Overridepublic CreditAndDebitBill process(CreditAndDebitBill item) throws Exception {CreditAndDebitBill creditAndDebitBill = new CreditAndDebitBill();if (item.getCreditBill() != null) {CreditBill creditBill = new CreditBill();BeanUtils.copyProperties(item.getCreditBill(),creditBill);creditAndDebitBill.setCreditBill(creditBill);}if (item.getDebitBill() != null) {DebitBill debitBill = new DebitBill();BeanUtils.copyProperties(item.getDebitBill(),debitBill);creditAndDebitBill.setDebitBill(debitBill);}return creditAndDebitBill;} }6、读混合文件writer
PatternMatchWriter 实现如下:
/*** @author shuliangzhao* @date 2020/12/6 14:35*/ @Component @StepScope public class PatternMatchWriter implements ItemWriter<CreditAndDebitBill> {@Autowiredprivate CreditBillMapper creditBillMapper;@Autowiredprivate DebitBillMapper debitBillMapper;@Overridepublic void write(List<? extends CreditAndDebitBill> items) throws Exception {for (CreditAndDebitBill creditAndDebitBill:items) {if (creditAndDebitBill.getDebitBill() != null) {debitBillMapper.insert(creditAndDebitBill.getDebitBill());}if (creditAndDebitBill.getCreditBill() != null) {creditBillMapper.insert(creditAndDebitBill.getCreditBill());}}} }至此,我们完成了读混合文件的处理。
如果想更详细查看以上所有代码请移步到github:读混合文件
总结
以上是生活随笔为你收集整理的全网最详细SpringBatch读(Reader)混合文件讲解的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 全网最详细SpringBatch读(Re
- 下一篇: 开源调度框架xxl-job集成Sprin