Spring batchデータ倉庫ETLフレーム構築を実現する(一)

12057 ワード

[color=red]はまだ完成していません。自分の参考にしてください。
参照リンク:
http://www.yihaomen.com/article/java/433.htm
http://www.zuidaima.com/share/1732772811131904.htm
http://13146489.iteye.com/blog/1412295
実現の具体的な機能:
Step 1:Aフォルダからcsvファイルを読み込み、処理後、データベースに保存します。
アプリ.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 parameters = new HashMap();
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");

}
}
CustoomItem Processor.java
package yihaomen;

import org.springframework.batch.item.ItemProcessor;

import yihaomen.model.Report;


public class CustomItemProcessor implements ItemProcessor {

@Override
public Report process(Report item) throws Exception {

System.out.println("Processing..." + item);
return item;
}

}
Report FieldSet Mapper.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 {

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;

}

}
Report Write 2 Database.java
package yihaomen;

import java.util.List;

import org.springframework.batch.item.ItemWriter;

import yihaomen.model.Report;

public class ReportWrite2Database implements ItemWriter{

@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 + "]";
}

}
レポート.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
	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">





class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">





class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

class="org.springframework.batch.core.launch.support.SimpleJobLauncher">



database.xml
	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">


class="org.springframework.jdbc.datasource.DriverManagerDataSource">






class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />




job-hello-world.xml
	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
">










commit-interval="10">












class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">




















yihaomen.model.Report





フレーム構造:
[img]http://dl2.iteye.com/upload/attachment/0097/4941/62b9824f-4eb6-32fe-a5a3-64f1c0dfa46a.png[img]
参照パッケージ
[img]http://dl2.iteye.com/upload/attachment/0097/4943/45be0aa9-fb0b-3c06-adde-9433775f4436.png[img]
[img]http://dl2.iteye.com/upload/attachment/0097/4945/70dc7552-6191-31cf-b9ed-3e8b5b773121.png[img]
[img]http://dl2.iteye.com/upload/attachment/0097/4947/842924ff-1fa8-3e98-96db-7a779baa362d.png[img]
[img]http://dl2.iteye.com/upload/attachment/0097/4949/0abf075b-f055-37c4-8be4-26027cfacc3e.png[img]
実行結果:
[img]http://dl2.iteye.com/upload/attachment/0097/4951/c9cb51f6-b9c8-3185-a012-64176c443186.png[img]