问题
I read a CSV file as input using spring batch and i have 2 CSV file as output.
The first file contains about 100 lines.
the input file contains 5 colones id,typeProduct and price. And i have just 2 type of product
i route through all these lines and i write two output files.
For both files a single line containing the type of product and the sum of the prices of all these products which have the same type.
So my need is before writing in the output files. i want to get all lines in a list to make conditions and adding a new attribute to my object for example result if sum> 5000
will take the value good else will take not good for example. and display them in the output line that exists in the file
Here is My product
public class Product {
private Long idt;
private String typeProduct;
private Double price;
private String result;
}
and here is the definition of my job
<batch:job id="exampleMultiWritersJob">
<batch:step id="stepMultiWriters">
<batch:tasklet transaction-manager="txManager">
<batch:chunk reader="exampleFileSourceReader" writer="exampleMultiWriters" commit-interval="10">
<batch:streams>
<batch:stream ref="cnraCosWriter" />
<batch:stream ref="cnraCopWriter" />
<batch:stream ref="rcarCosWriter" />
<batch:stream ref="rcarCopWriter" />
</batch:streams>
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="exampleFileSourceReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name="resource" value="file:#{jobParameters['file']}" />
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<!-- split it -->
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<!-- this is missing -->
<property name="delimiter" value=";"/>
<property name="names" value="idt,productType,price" />
</bean>
</property>
<property name="fieldSetMapper">
<!-- map to an object -->
<bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="prototypeBeanName" value="exampleFileMapper" />
</bean>
</property>
</bean>
</property>
</bean>
<bean id="exampleFileMapper" class="ma.controle.gestion.modele.Product" scope="prototype"/>
And here is the classifier methode
public class ExampleWriterRouteImpl {
@Classifier
public String classify(Product batch){
if(batch.getTypeProduct().equals("Telephone"))
return "tel";
else if(batch.getTypeProduct()).equals("PC")))
return "pc";
return null;
}
}
<bean id="classifier" class="org.springframework.batch.classify.BackToBackPatternClassifier">
<property name="routerDelegate">
<bean class="ma.controle.gestion.springbatch.ExampleWriterRouteImpl" />
</property>
<property name="matcherMap">
<map>
<entry key="tel" value-ref="telWriter" />
<entry key="pc" value-ref="pcWriter" />
</map>
</property>
<bean id="telWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
<!-- write to this csv file -->
<property name="resource" value="file:C:/output/tel.csv" />
<property name="shouldDeleteIfExists" value="true" />
<property name="shouldDeleteIfEmpty" value="true" />
<property name="appendAllowed" value="true" />
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<property name="delimiter" value=";" />
<property name="fieldExtractor">
<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="productType,price(Sum of all prdouctType),result" />
</bean>
</property>
</bean>
</property>
<bean id="pcWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
<!-- write to this csv file -->
<property name="resource" value="file:C:/output/pc.csv" />
<property name="shouldDeleteIfExists" value="true" />
<property name="shouldDeleteIfEmpty" value="true" />
<property name="appendAllowed" value="true" />
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<property name="delimiter" value=";" />
<property name="fieldExtractor">
<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="productType,price(Sum of all prdouctType),result" />
</bean>
</property>
</bean>
</property>
So i need to make sum of all product which have the same type and have only one single output line that contains product type and the sum of prices and result for each outputs files.
I did not know how to retrieve the list of these objects and retrieve only one line at the end.
Thanks.
来源:https://stackoverflow.com/questions/43946067/spring-batch-retrieve-the-list-of-objects-from-file-and-return-of-a-single-line