Java Code Examples for org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper

The following examples show how to use org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-cloud   Source File: UserReader.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public FlatFileItemReader<User> flatFileItemReader() {
    FlatFileItemReader<User> reader = new FlatFileItemReader<>();
    DefaultLineMapper<User> lineMapper = new DefaultLineMapper<User>() {
        {
            setLineTokenizer(new DelimitedLineTokenizer() {
                {
                    setNames(new String[]{"firstName", "lastName"});
                }
            });
            setFieldSetMapper(new BeanWrapperFieldSetMapper<User>() {
                {
                    setTargetType(User.class);
                }
            });
        }
    };
    reader.setResource(new ClassPathResource("sample-data.csv"));
    reader.setLineMapper(lineMapper);
    return reader;
}
 
Example 2
private void initialize() {
    Resource resource = new ClassPathResource(fileName);
    setResource(resource);

    DelimitedLineTokenizer delimitedLineTokenizer = new DelimitedLineTokenizer(DelimitedLineTokenizer.DELIMITER_COMMA);
    delimitedLineTokenizer.setNames(new String[]{"firstName", "lastName", "country", "year"});

    BeanWrapperFieldSetMapper<JavaChampion> beanWrapperFieldSetMapper = new BeanWrapperFieldSetMapper<>();
    beanWrapperFieldSetMapper.setTargetType(JavaChampion.class);

    DefaultLineMapper<JavaChampion> defaultLineMapper = new DefaultLineMapper<>();
    defaultLineMapper.setLineTokenizer(delimitedLineTokenizer);
    defaultLineMapper.setFieldSetMapper(beanWrapperFieldSetMapper);

    setLineMapper(defaultLineMapper);
}
 
Example 3
Source Project: spring-batch   Source File: ConvertNamesJobConfig.java    License: MIT License 6 votes vote down vote up
@Bean
public FlatFileItemReader<Person> itemReader() {
  BeanWrapperFieldSetMapper<Person> fieldSetMapper = new BeanWrapperFieldSetMapper<>();
  fieldSetMapper.setTargetType(Person.class);

  DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer();
  lineTokenizer.setDelimiter(",");
  lineTokenizer.setNames(new String[] {"firstName", "lastName"});

  DefaultLineMapper<Person> lineMapper = new DefaultLineMapper<>();
  lineMapper.setFieldSetMapper(fieldSetMapper);
  lineMapper.setLineTokenizer(lineTokenizer);

  FlatFileItemReader<Person> flatFileItemReader = new FlatFileItemReader<Person>();
  flatFileItemReader.setName("personItemReader");
  flatFileItemReader.setResource(new ClassPathResource("csv/persons.csv"));
  flatFileItemReader.setLineMapper(lineMapper);
  flatFileItemReader.setLinesToSkip(1);

  return flatFileItemReader;
}
 
Example 4
Source Project: spring-batch   Source File: CapitalizeNamesJobConfig.java    License: MIT License 6 votes vote down vote up
@Bean
public FlatFileItemReader<Person> itemReader() {
  BeanWrapperFieldSetMapper<Person> fieldSetMapper =
      new BeanWrapperFieldSetMapper<>();
  fieldSetMapper.setTargetType(Person.class);

  DelimitedLineTokenizer lineTokenizer =
      new DelimitedLineTokenizer();
  lineTokenizer.setDelimiter(",");
  lineTokenizer.setNames(new String[] {"firstName", "lastName"});

  DefaultLineMapper<Person> lineMapper = new DefaultLineMapper<>();
  lineMapper.setFieldSetMapper(fieldSetMapper);
  lineMapper.setLineTokenizer(lineTokenizer);

  FlatFileItemReader<Person> flatFileItemReader =
      new FlatFileItemReader<>();
  flatFileItemReader.setName("personItemReader");
  flatFileItemReader
      .setResource(new ClassPathResource("csv/persons.csv"));
  flatFileItemReader.setLineMapper(lineMapper);

  return flatFileItemReader;
}
 
Example 5
@Bean
public ItemReader<Staff> staffItemReader() {
    val reader = new FlatFileItemReader<Staff>();
    reader.setResource(new ClassPathResource("sample_staffs.csv"));
    reader.setLinesToSkip(1); // ヘッダーをスキップする
    reader.setLineMapper(new DefaultLineMapper<Staff>() {
        {
            setLineTokenizer(new DelimitedLineTokenizer() {
                {
                    setNames(new String[] { "firstName", "lastName", "email", "tel" });
                }
            });
            setFieldSetMapper(new BeanWrapperFieldSetMapper<Staff>() {
                {
                    setTargetType(Staff.class);
                }
            });
        }
    });
    return reader;
}
 
Example 6
Source Project: building-microservices   Source File: BatchConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
@StepScope
FlatFileItemReader<Person> flatFileItemReader(@Value("#{jobParameters[file]}") File file) {
    FlatFileItemReader<Person> r = new FlatFileItemReader<>();
    r.setResource(new FileSystemResource(file));
    r.setLineMapper(new DefaultLineMapper<Person>() {
        {
            this.setLineTokenizer(new DelimitedLineTokenizer(",") {
                {
                    this.setNames(new String[]{"first", "last", "email"});
                }
            });
            this.setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {
                {
                    this.setTargetType(Person.class);
                }
            });
        }
    });
    return r;
}
 
Example 7
@Bean
public FlatFileItemReader<Voltage> reader() {
    return new FlatFileItemReaderBuilder<Voltage>()
            .name("voltItemReader")
            .resource(new ClassPathResource("Volts.csv"))
            .delimited()
            .names(new String[]{"volt", "time"})
            .lineMapper(lineMapper())
            .fieldSetMapper(new BeanWrapperFieldSetMapper<Voltage>() {{
                setTargetType(Voltage.class);
            }})
            .build();
}
 
Example 8
@Bean
public FlatFileItemReader<PayrollTo> reader() {
    return new FlatFileItemReaderBuilder<PayrollTo>()
            .name("payrollItemReader")
            .resource(new ClassPathResource("payroll-data.csv"))
            .delimited()
            .names(new String[]{"identification","currency","ammount","accountType", "accountNumber", "description", "firstLastName"})
            .fieldSetMapper(new BeanWrapperFieldSetMapper<PayrollTo>() {{
                setTargetType(PayrollTo.class);
            }})
            .build();
}
 
Example 9
Source Project: spring-batch-rest   Source File: PersonJobConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
FlatFileItemReader<Person> personReader() {
    return new FlatFileItemReaderBuilder<Person>()
            .name("personItemReader")
            .resource(new ClassPathResource("person.csv"))
            .delimited()
            .names("firstName", "lastName")
            .fieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
                setTargetType(Person.class);
            }})
            .build();
}
 
Example 10
Source Project: spring-batch-rest   Source File: PersonJobConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
FlatFileItemReader<Person> personReader() {
    return new FlatFileItemReaderBuilder<Person>()
            .name("personItemReader")
            .resource(new ClassPathResource("person.csv"))
            .delimited()
            .names(new String[]{"firstName", "lastName"})
            .fieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
                setTargetType(Person.class);
            }})
            .build();
}
 
Example 11
Source Project: tutorials   Source File: SpringBatchScheduler.java    License: MIT License 5 votes vote down vote up
@Bean
public FlatFileItemReader<Book> reader() {
    return new FlatFileItemReaderBuilder<Book>().name("bookItemReader")
        .resource(new ClassPathResource("books.csv"))
        .delimited()
        .names(new String[] { "id", "name" })
        .fieldSetMapper(new BeanWrapperFieldSetMapper<Book>() {
            {
                setTargetType(Book.class);
            }
        })
        .build();
}