This article will discuss the use of spring batch to read and write the CSV file through a complete example. The process for this instance is to read a CSV file with four fields (Id,name,age,score), do a simple processing of the Read field, and then output it to another CSV file.
Engineering structures such as:
The Joblaunch class is used to start the job, and the Csvitemprocessor class is used to process the data acquired by reader, and the student class is a Pojo class for storing the mapped data. Inputfile.csv is the data read file, Outputfile.csv is the data output file.
Application.xml file configuration as in the previous article, no longer repeat.
The job configuration in the Batch.xml file is as follows:
<job id= "Csvjob" >
<step id= "Csvstep" >
<tasklet transaction-manager= "TransactionManager" >
<chunk reader= "Csvitemreader" writer= "Csvitemwriter" processor= "Csvitemprocessor" commit-interval= "1" >
</chunk>
</tasklet>
</step>
</job>
This file is configured with the job:csvjob of this operation. This job contains a step to complete a full CSV file reading and writing function. The Csvitemreader completes the read operation of the CSV file, and the csvitemprocessor completes the processing of the obtained data by the Csvitemwriter, which completes the writing operation to the CSV file.
The Csvitemreader configuration in the Batch.xml file is as follows:
<!--read the CSV file --
<bean:bean id= "Csvitemreader"
class= "Org.springframework.batch.item.file.FlatFileItemReader" scope= "Step" >
<bean:property name= "resource" value= "Classpath:inputFile.csv"/>
<bean:property name= "Linemapper" >
<bean:bean
class= "Org.springframework.batch.item.file.mapping.DefaultLineMapper" >
<bean:property name= "Linetokenizer" ref= "Linetokenizer"/>
<bean:property name= "Fieldsetmapper" >
<bean:bean
class= "Org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper" >
<bean:property name= "Prototypebeanname" value= "Student" ></bean:property>
</bean:bean>
</bean:property>
</bean:bean>
</bean:property>
</bean:bean>
<bean:bean id= "student" class= "com.wanggc.springbatch.sample.csv.Student" ></bean:bean>
<!--linetokenizer --
<bean:bean id= "Linetokenizer" class= "Org.springframework.batch.item.file.transform.DelimitedLineTokenizer ">
<bean:property name= "delimiter" value= ","/>
<bean:property name= "names" >
<bean:list>
<bean:value>ID</bean:value>
<bean:value>name</bean:value>
<bean:value>age</bean:value>
<bean:value>score</bean:value>
</bean:list>
</bean:property>
</bean:bean>
Csvitemreader implements the Flatfileitemreader class provided by spring batch, which is used primarily for read operations of flat files. It contains two necessary attributes resource and linemapper. The former specifies the location of the file to be read, which maps each line of the file into a Pojo object. Where Linemapper also has two important properties Linetokenizer and Fieldsetmapper, Linetokenizer decomposes a line of the file into a fieldset and then maps the Fieldsetmapper object to Pojo.
This approach is very similar to the read operation of the DB. Linemapper similar to ResultSet, a row in a file resembles a record in a table, encapsulated as a fieldset, similar to RowMapper. As for how to encapsulate a record, this work is done by Linetokenizer's inheriting class Delimitedlinetokenizer. The Delimiter property of Delimitedlinetokenizer determines the decomposition of a row of data according to what, the default is ",", the names attribute identifies the name of each field that is decomposed, When passed to Fieldsetmapper (this example uses Beanwrapperfieldsetmapper), the corresponding value can be obtained by this name. The Fieldsetmapper property Prototypebeanname is the name of the mapping Pojo class. When this property is set, the framework maps a fieldset of Linetokenizer into a Pojo object, which is done by name (the name of the callout in the Linetokenizer decomposition corresponds to the name of the field in the Pojo object).
In summary, Flatfileitemreader reads a record by completing the following four steps: 1, reading a record from resource specified file, 2,linetokenizer the record into fileset according to delimiter, The name of each field is obtained by the names property, 3, the decomposed Fileset is passed to Fieldsetmapper, which is mapped to Pojo object by name, and 4, and finally returned by the Flatfileitemreader object that the Pojo will map to. The framework passes the returned object to processor.
The Csvitemprocessor implements the Itemprocessor class. This class accepts the Pojo object mapped by reader, can do the corresponding business logic for this object, and then returns, the framework will pass the returned results to writer for write operations. The specific implementation code is as follows:
Package com.wanggc.springbatch.sample.csv;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Component;
/**
* Itemprocessor class.
*/
@Component ("Csvitemprocessor")
Public class Csvitemprocessor implements Itemprocessor<student, student> {
/**
* Simple processing of the data to be taken.
*
* @param student
* pre-processing data.
* @return processed data.
* @exception Exception
* processing is any exception that occurs.
*/
@Override
Public Student Process (Student Student) throws Exception {
/* Merge ID and name */
Student.setname (Student.getid () + "--" + student.getname ());
/ * Age plus 2 * /
Student.setage (Student.getage () + 2);
/ * Score plus ten * /
Student.setscore (Student.getscore () + ten);
/ * Pass the processed results to writer * /
return student;
}
}
The Csvitemreader configuration in the Batch.xml file is as follows:
<!--write a CSV file --
<bean:bean id= "Csvitemwriter"
class= "Org.springframework.batch.item.file.FlatFileItemWriter" scope= "Step" >
<bean:property name= "resource" value= "File:src/outputfile.csv"/>
<bean:property name= "Lineaggregator" >
<bean:bean
class= "Org.springframework.batch.item.file.transform.DelimitedLineAggregator" >
<bean:property name= "delimiter" value= "," ></bean:property>
<bean:property name= "Fieldextractor" >
<bean:bean
class= "Org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor" >
<bean:property name= "names" value= "Name,age,score" ></bean:property>
</bean:bean>
</bean:property>
</bean:bean>
</bean:property>
</bean:bean>
The Csvitemwriter implements the Flatfileitemwriter class. This class is similar to the Flatfileitemreader class and has two important properties: Resource and Lineaggregator. The former is the path to the file to be exported, and the latter is similar to Linetokenizer. Lineaggregator (The Delimitedlineaggregator class for this example) also has two important properties: Delimiter and Fieldextractor. Delimiter indicates what the field of the output is divided, which assembles the Pojo object into a string consisting of the fields of the Pojo object. The same flatfileitemwriter write a record also has the following four steps to complete: 1,processor passed over an object to Lineaggregator;2,lineaggregator to convert the object into an array; 3, The Lineaggregator attribute Fieldextractor converts the array into a string separated by delimiter, and 4 is the output of the string.
In this way, the reading, processing, and writing of a single piece of data are basically completed. Of course, reading and writing can also write their own classes to deal with, just pay attention to inherit Flatfileitemreader and Flatfileitemwriter on it.
The student class code used in the example is as follows:
Package com.wanggc.springbatch.sample.csv;
/** Pojo Class _student * /
Public class Student {
/** ID * /
private String ID = "";
/** name * /
private String name = "";
/** Age * /
private int age = 0;
/** score * /
Private FLOAT score = 0;
/*getter and Setter deleted */
}
Spring Batch Simple Application (CSV file operation) (ii)