`

Spring batch实现数据仓库ETL 框架搭建(一)

 
阅读更多
暂时还没完成,只供自己参考
参考链接:
http://www.yihaomen.com/article/java/433.htm
http://www.zuidaima.com/share/1732772811131904.htm
http://13146489.iteye.com/blog/1412295

实现的具体功能:
step1 : 从 A 文件夹中读取csv 文件,处理之后,写到数据库中保存

App.java
package yihaomen;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class App {
	public static void main(String[] args) {

		String[] springConfig  = 
			{	
				"spring/batch/jobs/job-hello-world.xml" 
			};
		
		ApplicationContext context = 
				new ClassPathXmlApplicationContext(springConfig);
		
		JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
		Job job = (Job) context.getBean("helloWorldJob");

		try {
       
			Map<String,JobParameter> parameters = new HashMap<String,JobParameter>();
			parameters.put("RUN_MONTH_KEY", new JobParameter(Math.random()));//因为一个job parameter只能用一次,所以为了多次测试使用,所以我直接用随机数
			JobExecution execution = jobLauncher.run(job, new JobParameters(parameters));
			System.out.println("Exit Status : " + execution.getStatus());

		} catch (Exception e) {
			e.printStackTrace();
		}

		System.out.println("Done");

	}
}

CustomItemProcessor.java
package yihaomen;

import org.springframework.batch.item.ItemProcessor;

import yihaomen.model.Report;




public class CustomItemProcessor implements ItemProcessor<Report, Report> {

	@Override
	public Report process(Report item) throws Exception {
		
		System.out.println("Processing..." + item);
		return item;
	}

}

ReportFieldSetMapper.java
package yihaomen;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;

import yihaomen.model.Report;

public class ReportFieldSetMapper implements FieldSetMapper<Report> {

	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	
	@Override
	public Report mapFieldSet(FieldSet fieldSet) throws BindException {
		
		Report report = new Report();
		report.setId(fieldSet.readInt(0));
		report.setSales(fieldSet.readBigDecimal(1));
		report.setQty(fieldSet.readInt(2));
		report.setStaffName(fieldSet.readString(3));
		
		//default format yyyy-MM-dd
		//fieldSet.readDate(4);
		String date = fieldSet.readString(4);
		try {
			report.setDate(dateFormat.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return report;
		
	}

}

ReportWrite2Database.java
package yihaomen;

import java.util.List;

import org.springframework.batch.item.ItemWriter;

import yihaomen.model.Report;

public class ReportWrite2Database  implements ItemWriter<Report>{

	@Override
	public void write(List<? extends Report> items) throws Exception {
		System.out.println("开始存储到数据库report表中.......");
			for (Report r : items ){
				System.out.println(r.toString());
			}
			System.out.println("存储完毕!.......");
	}
	
	
	

}

Report.java
package yihaomen.model;

import java.math.BigDecimal;
import java.util.Date;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "record")
public class Report {

	private int id;
	private BigDecimal sales;
	private int qty;
	private String staffName;
	private Date date;

	@XmlAttribute(name = "id")
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@XmlElement(name = "sales")
	public BigDecimal getSales() {
		return sales;
	}

	public void setSales(BigDecimal sales) {
		this.sales = sales;
	}

	@XmlElement(name = "qty")
	public int getQty() {
		return qty;
	}

	public void setQty(int qty) {
		this.qty = qty;
	}

	@XmlElement(name = "staffName")
	public String getStaffName() {
		return staffName;
	}

	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	@Override
	public String toString() {
		return "Report [id=" + id + ", sales=" + sales + ", qty=" + qty + ", staffName=" + staffName + "]";
	}

}

report.csv
1001,"213,100",980,yihaomen, 2013-01-01
1002,"320,200",1080,staff 1, 2013-01-01
1003,"342,197",1200,staff 2, 2013-01-01


context.xml
<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-3.1.xsd">

	<!-- stored job-meta in memory -->
	<!--  
	<bean id="jobRepository"
		class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
		<property name="transactionManager" ref="transactionManager" />
	</bean>
 	 -->
 	 
 	 <!-- stored job-meta in database -->
	<bean id="jobRepository"
		class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="transactionManager" ref="transactionManager" />
		<property name="databaseType" value="oracle" />
	</bean>
	
	<bean id="transactionManager"
		class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
	 
	<bean id="jobLauncher"
		class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
		<property name="jobRepository" ref="jobRepository" />
	</bean>

</beans>

database.xml
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
	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-3.1.xsd
		http://www.springframework.org/schema/jdbc 
		http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">

    <!-- connect to database -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
		<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
		<property name="username" value="evan" />
		<property name="password" value="880823" />
	</bean>

	<bean id="transactionManager"
		class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
	
	<!-- create job-meta tables automatically -->
	<!-- spring batch 2.2.3 里面的语句有点问题,在后面两个需要加"," 号,自己可以拷贝出来,修改执行.
	<jdbc:initialize-database data-source="dataSource">	    
		<jdbc:script location="org/springframework/batch/core/schema-drop-oracle10g.sql" />
		<jdbc:script location="org/springframework/batch/core/schema-oracle10g.sql" />
	</jdbc:initialize-database>
	 -->
	
</beans>

job-hello-world.xml
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:batch="http://www.springframework.org/schema/batch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/batch
		http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	">

	<import resource="../config/context.xml" />
	<import resource="../config/database.xml" />

	<bean id="report" class="yihaomen.model.Report" scope="prototype" />
	<bean id="itemProcessor" class="yihaomen.CustomItemProcessor" />

	<batch:job id="helloWorldJob">
		<batch:step id="step1">
			<batch:tasklet>
				<batch:chunk reader="cvsFileItemReader" writer="xmlItemWriter" processor="itemProcessor"
					commit-interval="10">
				</batch:chunk>
			</batch:tasklet>
		</batch:step>
	</batch:job>

	<bean id="cvsFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">

		<property name="resource" value="classpath:cvs/input/report.csv" />

		<property name="lineMapper">
			<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
				<property name="lineTokenizer">
					<bean
						class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
						<property name="names" value="id,sales,qty,staffName,date" />
					</bean>
				</property>
				<property name="fieldSetMapper">
				    <bean class="yihaomen.ReportFieldSetMapper" />
				    
				    <!-- if no data type conversion, use BeanWrapperFieldSetMapper to map by name
					<bean
						class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
						<property name="prototypeBeanName" value="report" />
					</bean>
					 -->
				</property>
			</bean>
		</property>

	</bean><!--

	<bean id="xmlItemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
		<property name="resource" value="file:xml/outputs/report.xml" />
		<property name="marshaller" ref="reportMarshaller" />
		<property name="rootTagName" value="report" />
	</bean>

	-->

	
	<bean id="xmlItemWriter" class="yihaomen.ReportWrite2Database">
	</bean>
	
	<bean id="reportMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
		<property name="classesToBeBound">
			<list>
				<value>yihaomen.model.Report</value>
			</list>
		</property>
	</bean>


</beans>

框架结构:




引用的jar包











运行结果:




  • 大小: 15.1 KB
  • 大小: 14.6 KB
  • 大小: 16.6 KB
  • 大小: 13.2 KB
  • 大小: 5.5 KB
  • 大小: 18.8 KB
分享到:
评论

相关推荐

    spring Batch实现数据库大数据量读写

    Spring Batch 是一个强大的、可扩展的Java框架,专门用于处理批量数据处理任务,包括大量数据库数据的读取、处理和写入。它被设计为在企业级应用中处理大规模、高吞吐量的工作负载。本篇文章将深入探讨如何利用...

    基于Spring Batch的大数据量并行处理

    Spring Batch是一款用于高效处理大量数据的开源框架,特别适用于批处理任务。它由Spring Source与Accenture合作开发,结合了双方在批处理架构和技术上的优势,旨在简化大规模数据处理任务中的并发、事务管理、监控...

    Spring Batch批处理框架

    Spring Batch是一个开源的轻量级批处理框架,它提供了一整套可复用的组件,用于构建健壮且高效的批处理应用程序。由于信息给定的【部分内容】并没有提供实际的技术细节,因此我将基于Spring Batch框架本身,详细介绍...

    SpringBatch-DataMigration SpringBatch数据迁移项目

    1.本项目运行在tomcat容器中,主要功能为从spring_batch_left库的user_from表抓取数据,之后批量插入到spring_batch_right库的user_to表 2.应用quartz对job进行定时触发(目前设置的定时为每隔一分钟执行一次,目前...

    Spring Boot整合Spring Batch,实现批处理

    Spring Boot以其简洁的配置和快速的启动能力深受开发者喜爱,而Spring Batch作为Spring框架的一部分,专注于批量处理任务,提供了强大的数据处理能力和事务管理功能。下面我们将深入探讨这个主题。 首先,**Spring ...

    springbatch 详解PDF附加 全书源码 压缩包

    4. **ETL 过程**: 在数据仓库项目中,使用 Spring Batch 进行数据抽取、转换和加载。 通过阅读《Spring.Batch批处理框架.pdf》和源码,你将能够掌握 Spring Batch 的核心概念和实践技巧,为你的企业级应用开发带来...

    quartz整合springbatch动态集群定时实现mysql参考

    Spring Batch是一个用于处理大量数据的批处理框架。它提供了诸如事务管理、错误处理、读写器和处理器抽象等功能,简化了复杂的数据处理任务。Spring Batch可以通过配置文件或编程方式定义批处理作业的步骤和流程。 ...

    springBoot+springBatch批量处理数据demo

    SpringBoot和SpringBatch是两个非常重要的Java开发框架,它们在企业级应用开发中扮演着重要角色。SpringBoot简化了Spring应用程序的配置和启动过程,而SpringBatch则专注于批处理任务,提供了一套全面且可扩展的解决...

    Spring Batch in Action英文pdf版

    Spring Batch是一个开源的轻量级、全面的批处理框架,它是为了解决企业应用中的大规模数据处理需求而设计的。Spring Batch in Action是一本专注于Spring Batch框架的书籍,由Arnaud Cogoluègnes、Thierry Templier...

    SpringBatch+Spring+Mybatis+MySql (spring batch 使用jar)

    Spring Batch是一个轻量级的,完全面向Spring的批处理框架,可以应用于企业级大量的数据处理系统。Spring Batch以POJO和大家熟知的Spring框架为基础,使开发者更容易的访问和利用企业级服务。Spring Batch可以提供...

    spring-batch分区处理示例

    Spring Batch 是一个强大的Java框架,专门用于处理批量数据处理任务。在Spring Batch中,分区处理是一种优化策略,它将大型工作负载分解成多个较小、独立的任务,这些任务可以在不同的线程或甚至不同的节点上并行...

    Spring batch in action

    Spring Batch是一本介绍如何使用Spring Batch框架来构建批处理应用程序的专业书籍。在软件行业中,随着各种趋势的发展,例如基于Web的应用、面向服务的架构(SOA)以及事件驱动的应用,批处理应用程序虽然存在已久,...

    springbatch简单用

    SpringBatch 是一个强大的、全面的批处理框架,用于处理大量数据。它被设计为高度可扩展和模块化,适用于各种企业级应用。SpringBatch 提供了处理批量作业所需的全部基础设施,包括读取、处理和写入数据,以及作业...

    spring-batch包

    Spring Batch 是一个强大的、全面的批处理框架,用于处理大量数据的处理任务。它由 Spring 框架提供支持,因此具有高度的可配置性和可扩展性,适用于各种企业级应用。Spring Batch 4.0.0 版本是该框架的一个重要版本...

    Spring Batch In Action

    - **Spring Batch** 是一个基于 Java 的强大框架,专门设计用于处理大规模数据批处理任务。 - 它为开发人员提供了一套完整的工具来构建高效、可靠的批量数据处理应用。 - **Spring Batch** 的核心优势在于它能够简化...

    quartz整合springbatch定时集群实现mysql参考模版

    总的来说,"quartz_springbatch"模版提供了一个完整的解决方案,用于构建基于Quartz和SpringBatch的定时任务集群,且与MySQL数据库集成,确保数据的可靠性和任务的高可用性。通过学习和理解这个模版,开发者可以快速...

    spring-batch+quartz处理mysql数据示例

    Spring Batch 是一个全面、强大的批处理框架,它提供了大量用于处理批量数据的功能,包括读取、处理和写入数据,以及事务管理、错误处理等。Spring Batch 的核心组件包括: 1. **Job**: 代表一个完整的批处理任务,...

    Spring.Batch批处理框架

    Spring Batch 是一个轻量级的、完善的批处理框架,旨在帮助企业建立健壮、高效的批处理应用。Spring Batch是Spring的一个子项目,使用Java语言并基于Spring框架为基础开发,使得已经使用 Spring 框架的开发者或者企业更...

    spring-batch同步数据库mysql源码

    Spring-Batch作为Spring框架的一部分,为批量处理和数据同步提供了强大的支持。本文将深入探讨Spring-Batch如何与MySQL数据库进行同步,并结合给定的源码进行分析。 首先,Spring-Batch是一个轻量级、全面的批处理...

    Spring Batch读取txt文件并写入数据库的方法教程

    Spring Batch 是一个强大的批处理框架,它为处理大量数据提供了灵活和可扩展的解决方案。在本教程中,我们将探讨如何使用 Spring Batch 读取文本(txt)文件,并将读取到的数据处理后写入数据库。 首先,我们需要...

Global site tag (gtag.js) - Google Analytics