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

The following examples show how to use org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper. 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 File: UserReader.java    From spring-cloud with 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
Source File: JavaChampionsReader.java    From Software-Architecture-with-Spring-5.0 with MIT License 6 votes vote down vote up
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 File: ConvertNamesJobConfig.java    From spring-batch with 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 File: CapitalizeNamesJobConfig.java    From spring-batch with 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
Source File: ImportStaffJobConfig.java    From spring-boot-doma2-sample with Apache License 2.0 6 votes vote down vote up
@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 File: BatchConfiguration.java    From building-microservices with 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
Source File: BatchConfiguration.java    From batch-processing-large-datasets-spring with MIT License 5 votes vote down vote up
@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
Source File: BatchConfig.java    From Software-Architecture-with-Spring-5.0 with MIT License 5 votes vote down vote up
@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 File: PersonJobConfig.java    From spring-batch-rest with 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 File: PersonJobConfig.java    From spring-batch-rest with 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 File: SpringBatchScheduler.java    From tutorials with 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();
}