Spring Batch之读数据—读带有头文件的文件(三十一)
一、项目实例
1.项目框架
2.代码实现
BatchMain.java:
package com.xj.demo26;import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @Author : xjfu* @Date : 2021/10/26 20:01* @Description : demo26 读带有头文件的文件*/
public class BatchMain {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("demo26/job/demo26-job.xml");//Spring Batch的作业启动器,JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");//在batch.xml中配置的一个作业Job job = (Job)context.getBean("billJob");try{//开始执行这个作业,获得处理结果(要运行的job,job参数对象)JobExecution result = launcher.run(job, new JobParameters());System.out.println(result.toString());}catch (Exception e){e.printStackTrace();}}
}
CopyHeaderLineCallbackHandler.java:
package com.xj.demo26;import org.springframework.batch.item.file.FlatFileHeaderCallback;
import org.springframework.batch.item.file.LineCallbackHandler;
import java.io.IOException;
import java.io.Writer;public class CopyHeaderLineCallbackHandler implements LineCallbackHandler,FlatFileHeaderCallback {private String header = "";public void handleLine(String line) {this.header = line;}public void writeHeader(Writer writer) throws IOException {writer.write(header);}
}
CreditBill.java:
package com.xj.demo26;/*** @Author : xjfu* @Date : 2021/10/26 19:27* @Description :*/
public class CreditBill {//银行卡账户IDprivate String accountID = "";//持卡人姓名private String name = "";//消费金额private double amount = 0;//消费日期private String date = "";//消费场所private String address = "";public String getAccountID() {return accountID;}public void setAccountID(String accountID) {this.accountID = accountID;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getAmount() {return amount;}public void setAmount(double amount) {this.amount = amount;}public String getDate() {return date;}public void setDate(String date) {this.date = date;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return this.accountID + "," + this.name + "," + this.amount + "," + this.date + "," + this.address;}
}
CreditBillFieldSetMapper.java:
package com.xj.demo26;import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;public class CreditBillFieldSetMapper implements FieldSetMapper<CreditBill> {public CreditBill mapFieldSet(FieldSet fieldSet) throws BindException {CreditBill result = new CreditBill();result.setAccountID(fieldSet.readString("accountID"));result.setName(fieldSet.readString("name"));result.setAmount(fieldSet.readDouble("amount"));result.setDate(fieldSet.readString("date"));result.setAddress(fieldSet.readString("address"));return result;}
}
CreditBillProcessor.java:
package com.xj.demo26;import org.springframework.batch.item.ItemProcessor;public class CreditBillProcessor implements ItemProcessor<CreditBill, CreditBill> {public CreditBill process(CreditBill bill) throws Exception {System.out.println(bill.toString());return bill;}
}
demo26-job.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:batch="http://www.springframework.org/schema/batch"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd"><!--导入文件--><import resource="classpath:demo26/job/demo26-jobContext.xml"/><!--定义名字为billJob的作业--><batch:job id="billJob"><!--定义名字为billStep的作业步--><batch:step id="billStep"><batch:tasklet transaction-manager="transactionManager"><!--定义读、处理、写操作,规定每处理两条数据,进行一次写入操作,这样可以提高写的效率--><batch:chunk reader="copyHeaderItemReader" processor="creditBillProcessor" writer="csvItemWriter" commit-interval="2"></batch:chunk></batch:tasklet></batch:step></batch:job>
</beans>
demo26-jobContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"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.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"><!--定义作业仓库 Job执行期间的元数据存储在内存中--><bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"/><!--定义作业调度器,用来启动job--><bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"><!--注入jobRepository--><property name="jobRepository" ref="jobRepository"/></bean><!--定义事务管理器,用于Spring Batch框架中对数据操作提供事务能力--><bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/><!--文件读取类--><bean id="copyHeaderItemReader" scope="step" class="org.springframework.batch.item.file.FlatFileItemReader"><property name="resource" value="classpath:demo26/data/demo26-copyHeadFile.csv"/><!--将一行文件记录转换为Java对象--><property name="lineMapper" ref="lineMapper" /><!--表示从文件头跳过指定行--><property name="linesToSkip" value="1"/><!--定义文件中记录跳过时执行的回调操作--><property name="skippedLinesCallback" ref="copyHeaderLineCallbackHandler"/><property name="comments"><list><value>##</value><value>$$</value></list></property></bean><bean id="lineMapper" class="org.springframework.batch.item.file.mapping.DefaultLineMapper" ><property name="lineTokenizer" ref="delimitedLineTokenizer" /><property name="fieldSetMapper" ref="creditBillFieldSetMapper"/></bean><bean id="delimitedLineTokenizer" class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"><property name="delimiter" value=","/><property name="names" value="accountID,name,amount,date,address" /></bean><bean id="creditBillFieldSetMapper" class="com.xj.demo26.CreditBillFieldSetMapper"/><!--自定义实现LineCallbackHandler--><bean id="copyHeaderLineCallbackHandler" class="com.xj.demo26.CopyHeaderLineCallbackHandler"/><!--处理类--><bean id="creditBillProcessor" scope="step" class="com.xj.demo26.CreditBillProcessor"/><!--写入类--><bean id="csvItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step"><property name="resource" value="file:src/main/resources/demo26/data/demo26-outputFile.csv"/><property name="headerCallback" ref="copyHeaderLineCallbackHandler"/><property name="lineAggregator"><bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator"><property name="delimiter" value=","/><property name="fieldExtractor"><bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor"><property name="names" value="accountID,name,amount,date,address"/></bean></property></bean></property></bean><!--注入实体类--><bean id="creditBill" class="com.xj.demo26.CreditBill" scope="prototype"/></beans>
demo26-copyHeadFile.csv:
accountID,name,amount,date,address
## this line is first comment
4047390012345678,tom,100.00,2013-2-2 12:00:08,Lu Jia Zui road
## maybe address is not right
4047390012345678,tom,320.00,2013-2-3 10:35:21,Lu Jia Zui road
$$ this line is comment, begin with &&
4047390012345678,tom,674.70,2013-2-6 16:26:49,South Linyi road
4047390012345678,tom,793.20,2013-2-9 15:15:37,Longyang road
4047390012345678,tom,360.00,2013-2-11 11:12:38,Longyang road
## maybe time is not right
4047390012345678,tom,893.00,2013-2-28 20:34:19,Hunan road
3.运行结果
Spring Batch之读数据—读带有头文件的文件(三十一)相关推荐
- Spring Batch之读数据—读混合记录文件(三十)
一.读混合记录文件 Spring Batch之读数据-Flat格式文件(二十四)_人--杰的博客-CSDN博客_flat文件 二.项目实例 1.项目框架 2.代码实现 (1)BatchMain.jav ...
- Spring Batch之读数据—读JSON文件(二十八)
一.JSON文件 Spring Batch之读数据-Flat格式文件(二十四)_人--杰的博客-CSDN博客_flat文件 二.项目实例 1.项目框架 2.代码实现 (1)BatchMain.java ...
- Spring Batch之读数据—读分隔符文件(二十六)
一.分隔符文件 Spring Batch之读数据-Flat格式文件(二十四)_人--杰的博客-CSDN博客_flat文件 二.项目实例 1.项目框架 2.代码实现 (1)启动类BatchMain.ja ...
- Java读带有BOM的UTF-8文件乱码原因及解决方法(转)
转载:http://www.linuxidc.com/Linux/2012-12/76707.htm 最近在处理文件时发现了同样类型的文件使用的编码可能是不同的.所以想将文件的格式统一一下(因为UTF ...
- Java读取UTF-8格式txt文件第一行出现乱码及解决;Java读带有BOM的UTF-8文件乱码原因及解决方法(转载)...
原文地址:http://blog.csdn.net/jackpk/article/details/5702964/ Java读取UTF-8的txt文件第一行出现乱码"?"及解决 t ...
- Java读带有BOM的UTF-8文件乱码原因及解决方法
Java读带有BOM的UTF-8文件乱码原因及解决方法 Java读带有BOM的UTF-8文件乱码原因及解决方法 - daimojingdeyu - ITeye技术网站 Java读带有BOM的UTF-8 ...
- Spring Batch之读数据—FlatFileItemReader(二十五)
FlatFileItemReader实现ItemReader接口,核心作用将Flat文件中的记录转换为Java对象. 一.FlatFileItemReader中关键属性 属性 类型 说明 buffer ...
- Spring Batch之读数据库(三十一)
一.Spring Batch框架对数据库的支持 Spring Batch框架对数据库提供了非常好的支持,包括基于JDBC和ORM(Object-Relational Mapping)的读取方式:基于游 ...
- 使用 Spring Batch 构建企业级批处理应用
https://www.ibm.com/developerworks/cn/java/j-lo-springbatch1/index.html https://www.ibm.com/develope ...
最新文章
- 权限的继承,取消继承,强制继承
- explain都不懂,还好意思说会SQL调优?
- c++矩阵作为函数输入变量_C++实现矩阵乘法
- 浙江理工大学电信宽带校园网访问添加路由表命令(2020.10)(Windows和Liunx)
- 复习--SQL Server (一) -系统数据库
- Ajax--WebService返回复杂二维数组
- 无需安装Oracle,用PL/SQL直接连接Oracle服务器
- cin.ignore()函数的使用
- Linux firefox2.0自动升级后启动不了
- Java使用ffmpeg和mencoder实现视频转码
- 认证杯网络挑战赛C题破局共享汽车
- ubnt ER-4添加PON stick模块替换光猫实战
- 让 snoop 支持 .NET Core WPF 调试
- inventor 波纹阵列_Inventor装配零部件阵列功能详解
- 每日一支TED——Ethan Nadelmann:为什么我们应该终止禁毒战争
- 一图看懂自然资源资金监测监管系统
- 【《Real-Time Rendering 3rd》 提炼总结】(三) 第三章 · GPU渲染管线与可编程着色器 The Graphics Processing Unit
- Java-项目1-家庭收支记账软件
- python3d_Power BI将超越python和D3,成为数据可视化的福音、定性数据分析的未来?...
- 大数据蓝皮书:解读中国大数据发展十大趋势
热门文章
- 学校旧机房电脑如何改造升级成云教室客户端 旧电脑改造成x86云终端
- 重新认识 Kubernetes 的核心组件
- 一个n人搜索的论坛精华帖子→网络(转)
- cisco 2960 VLAN MAC_苹果宣布 Mac 和 iOS 端消费将互通,开发者可以进行适配
- 场馆“智慧化”是否有必要?
- Codefi基于区块链的开发框架
- VS 打开项目就提示报错,提示退出或者进行调试 问题解决
- 苏彤,你的 Python Flask 编写生成二维码接口写完了
- Android Studio创建的模拟器无法root问题解决
- UVA10566 Crossed Ladders(计算几何+二分)