Hi everyone,
If anyone is interested, I've added Spring Batch (2.1.x) ItemReader/Writer adapters for flat files to the BeanIO 1.2.x open source project. BeanIO supports reading and writing CSV, delimited, fixed length and XML file formats using an XML mapping file to bind a stream layout to Java objects. For more information, you can check out the reference guide at http://beanio.org. And here is a quick example:
Given a CSV file such as the following (in.csv):
And Java bean objects such as:Code:Header,2011-10-01 Detail,"Joe","Smith",25,100 Main St,,Chicago,IL,12345 Detail,"Jason",Jones,32,5600 State St,Apt #2,Santa Fe,NM,12345
A BeanIO mapping file (mapping.xml), such as the one below, can read bean objects (Map and Contact) from the file.Code:public Contact { String firstName; String lastName; int age; Address address; // getters & setters... } public Address { String street1; String street2; String city; String state; String zip; // getters & setters... }
Using the following Spring Batch ItemReader bean configuration:Code:<beanio xmlns="http://www.beanio.org/2011/01"> <!-- Mapping files can declare multiple stream layouts --> <stream name="contactFile" format="csv"> <!-- Binds "Header" records to Map objects, field names are keys --> <record name="header" class="map" maxOccurs="1"> <!-- "rid" stands for record identifier and is used to match record types --> <field name="recordType" rid="true" literal="Header" /> <field name="fileDate" type="date" format="yyyy-MM-dd" /> </record> <!-- Binds "Detail" records to Contact objects --> <record name="contact" class="example.Contact" minOccurs="0"> <field name="recordType" rid="true" literal="Detail" ignore="true" /> <field name="firstName" /> <field name="lastName" /> <field name="age" /> <bean name="address" class="example.Address"> <field name="street1" /> <field name="street2" /> <field name="city" /> <!-- Simple textual validations are supported: --> <field name="state" required="true" minLength="2" maxLength="2" /> <field name="zip" regex="\d{5}" /> </bean> </record> </stream> </beanio>
And likewise, an ItemWriter can be configured for writing the same objects to an output file:Code:<bean id="itemReader" class="org.beanio.spring.BeanIOFlatFileItemReader"> <property name="streamMapping" value="classpath:/mapping.xml" /> <property name="streamName" value="contactFile" /> <property name="resource" value="file:in.csv" /> </bean>
Of course, much more is possible, although with Spring Batch so extensible, I will refrain from any direct feature comparison because I would no doubt be quickly proven wrongCode:<bean id="itemWriter" class="org.beanio.spring.BeanIOFlatFileItemWriter"> <property name="streamMapping" value="classpath:/mapping.xml" /> <property name="streamName" value="contactFile" /> <property name="resource" value="file:out.csv" /> </bean>
Thanks,
Kevin



Reply With Quote