Search in sources :

Example 1 with Employee

use of com.pkslow.batch.entity.Employee in project pkslow-samples by LarryDpk.

the class JobConfig method reader.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Bean
public FlatFileItemReader<Employee> reader() {
    // Create reader instance
    FlatFileItemReader<Employee> reader = new FlatFileItemReader<Employee>();
    // Set number of lines to skips. Use it if file has header rows.
    reader.setLinesToSkip(1);
    // Configure how each line will be parsed and mapped to different values
    reader.setLineMapper(new DefaultLineMapper() {

        {
            // 3 columns in each row
            setLineTokenizer(new DelimitedLineTokenizer() {

                {
                    setNames(new String[] { "id", "firstName", "lastName" });
                }
            });
            // Set values in Employee class
            setFieldSetMapper(new BeanWrapperFieldSetMapper<Employee>() {

                {
                    setTargetType(Employee.class);
                }
            });
        }
    });
    return reader;
}
Also used : FlatFileItemReader(org.springframework.batch.item.file.FlatFileItemReader) DelimitedLineTokenizer(org.springframework.batch.item.file.transform.DelimitedLineTokenizer) Employee(com.pkslow.batch.entity.Employee) DefaultLineMapper(org.springframework.batch.item.file.mapping.DefaultLineMapper) BeanWrapperFieldSetMapper(org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper) Bean(org.springframework.context.annotation.Bean)

Example 2 with Employee

use of com.pkslow.batch.entity.Employee in project pkslow-samples by LarryDpk.

the class JobConfig method writer.

@Bean
public FlatFileItemWriter<Employee> writer() {
    // Create writer instance
    FlatFileItemWriter<Employee> writer = new FlatFileItemWriter<>();
    // Set output file location
    writer.setResource(new FileSystemResource(outputFilename));
    // All job repetitions should "append" to same output file
    writer.setAppendAllowed(true);
    // Name field values sequence based on object properties
    writer.setLineAggregator(new DelimitedLineAggregator<Employee>() {

        {
            setDelimiter(",");
            setFieldExtractor(new BeanWrapperFieldExtractor<Employee>() {

                {
                    setNames(new String[] { "id", "firstName", "lastName" });
                }
            });
        }
    });
    return writer;
}
Also used : Employee(com.pkslow.batch.entity.Employee) BeanWrapperFieldExtractor(org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor) FlatFileItemWriter(org.springframework.batch.item.file.FlatFileItemWriter) FileSystemResource(org.springframework.core.io.FileSystemResource) Bean(org.springframework.context.annotation.Bean)

Aggregations

Employee (com.pkslow.batch.entity.Employee)2 Bean (org.springframework.context.annotation.Bean)2 FlatFileItemReader (org.springframework.batch.item.file.FlatFileItemReader)1 FlatFileItemWriter (org.springframework.batch.item.file.FlatFileItemWriter)1 BeanWrapperFieldSetMapper (org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper)1 DefaultLineMapper (org.springframework.batch.item.file.mapping.DefaultLineMapper)1 BeanWrapperFieldExtractor (org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor)1 DelimitedLineTokenizer (org.springframework.batch.item.file.transform.DelimitedLineTokenizer)1 FileSystemResource (org.springframework.core.io.FileSystemResource)1