一、JSON文件
Spring Batch之读数据—Flat格式文件(二十四)_人……杰的博客-CSDN博客_flat文件
二、项目实例
1.项目框架
2.代码实现
(1)BatchMain.java:
package com.xj.demo21;
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 : demo21 读JSON文件的启动类
*/
public class BatchMain {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("demo21/job/demo21-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();
}
}
}
(2)CreditBill.java:
package com.xj.demo21;
/**
* @Author : xjfu
* @Date : 2021/10/26 19:27
* @Description :
*/
public class CreditBill {
//银行卡账户ID
private 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;
}
@Override
public String toString() {
return this.accountID + "," + this.name + "," + this.amount + "," + this.date + "," + this.address;
}
}
(3)CreditBillProcessor.java:
package com.xj.demo21;
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;
}
}
(4)WrappedJsonLineMapper.java:
package com.xj.demo21;
import org.springframework.batch.item.file.LineMapper;
import org.springframework.batch.item.file.mapping.JsonLineMapper;
import java.util.Map;
/**
* WrappedJsonLineMapper: 扩展实现,用于代理JsonLineMapper,负责将一条记录直接转换为领域对象CreditBill。
* WrappedJsonLineMapper将转换工作委托给JsonLineMapper,并将Map结果转换为CreditBill。
*/
public class WrappedJsonLineMapper implements LineMapper<CreditBill> {
private JsonLineMapper delegate;
public CreditBill mapLine(String line, int lineNumber) throws Exception {
CreditBill result = new CreditBill();
Map<String, Object> creditBillMap = delegate.mapLine(line, lineNumber);
result.setAccountID((String)creditBillMap.get("accountID"));
result.setName((String)creditBillMap.get("name"));
result.setAmount((Double)creditBillMap.get("amount"));
result.setDate((String)creditBillMap.get("date"));
result.setAddress((String)creditBillMap.get("address"));
return result;
}
public JsonLineMapper getDelegate() {
return delegate;
}
public void setDelegate(JsonLineMapper delegate) {
this.delegate = delegate;
}
}
(5)demo21-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:demo21/job/demo21-jobContext.xml"/>
<!--定义名字为billJob的作业-->
<batch:job id="billJob">
<!--定义名字为billStep的作业步-->
<batch:step id="billStep">
<batch:tasklet transaction-manager="transactionManager">
<!--定义读、处理、写操作,规定每处理两条数据,进行一次写入操作,这样可以提高写的效率-->
<batch:chunk reader="jsonItemReader" processor="creditBillProcessor" writer="csvItemWriter" commit-interval="2">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
</beans>
(6)demo21-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--定义作业仓库 Job执行期间的元数据存储在内存中-->
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
</bean>
<!--定义作业调度器,用来启动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"/>
<!--使用FlatFileItemReader读文本文件-->
<bean id="jsonItemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<!--指定读取的资源文件-->
<property name="resource" value="classpath:demo21/data/demo21-jsonFile.json"/>
<!--JSON的记录分隔策略-->
<property name="recordSeparatorPolicy" ref="jsonRecordSeparatorPolicy"/>
<!--行匹配策略-->
<property name="lineMapper" ref="wrappedJsonLineMapper"/>
</bean>
<!--定义JSON格式的记录分割策略-->
<bean id="jsonRecordSeparatorPolicy" class="org.springframework.batch.item.file.separator.JsonRecordSeparatorPolicy"/>
<!--声明包装的行匹配策略-->
<bean id="wrappedJsonLineMapper" class="com.xj.demo21.WrappedJsonLineMapper">
<property name="delegate" ref="jsonLineMapper"/>
</bean>
<!--数据转换由Spring Batch框架提供的jsonLineMapper来实现-->
<bean id="jsonLineMapper" class="org.springframework.batch.item.file.mapping.JsonLineMapper"/>
<!--注入实体类-->
<bean id="creditBill" class="com.xj.demo21.CreditBill" scope="prototype"/>
<bean id="creditBillProcessor" class="com.xj.demo21.CreditBillProcessor" scope="step"/>
<!--写信用卡账单文件,CSV格式-->
<bean id="csvItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
<!--要写入的文件位置,因为[classpath:]不是一个具体的目录,这里应当用[file:](从项目根目录开始)指明输出位置-->
<property name="resource" value="file:src/main/resources/demo21/data/demo21-outputFile.csv"/>
<!--[lineAggregator成员]指明行聚合器,用来将对象输出到文件时构造文件中的每行的格式-->
<property name="lineAggregator">
<!--这里使用Spring Batch自带的DelimitedLineAggregator来作为行聚合器(可以拼接一个个属性形成行)-->
<bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<!--使用","拼接-->
<property name="delimiter" value=","/>
<!--fieldExtractor成员用来将Java类的属性组成的数组拼接成行字符串-->
<property name="fieldExtractor">
<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="accountID,name,amount,date,address">
</property>
</bean>
</property>
</bean>
</property>
</bean>
</beans>
(7)demo21-jsonFile.json:
{ "accountID": "4047390012345678",
"name": "tom",
"amount": 100.00,
"date": "2013-2-2 12:00:08",
"address": "Lu Jia Zui road"}
{ "accountID": "4047390012345678",
"name": "tom",
"amount": 320.00,
"date": "2013-2-3 10:35",
"address": "Lu Jia Zui road"}
{ "accountID": "4047390012345678",
"name": "tom",
"amount": 674.70,
"date": "2013-2-6 16:26:49",
"address": "South Linyi road"}
{ "accountID": "4047390012345678",
"name": "tom",
"amount": 793.20,
"date": "2013-2-9 15:15:37",
"address": "Longyang road"}
{ "accountID": "4047390012345678",
"name": "tom",
"amount": 360.00,
"date": "2013-2-11 11:12:38",
"address": "Longyang road"}
{ "accountID": "4047390012345678",
"name": "tom",
"amount": 893.00,
"date": "2013-2-28 20:34:19",
"address": "Hunan road"}
(8)pom.xml新增支持JSON的jar包
<!--读JSON文件所需jar包-->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>com.springsource.org.codehaus.jackson.mapper</artifactId>
<version>1.4.3</version>
<type>jar</type>
</dependency>
3.运行结果
?
|