org.springframework.batch.item.file.FlatFileItemReader Java Examples

The following examples show how to use org.springframework.batch.item.file.FlatFileItemReader. 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: BatchConfiguration.java    From building-microservices with Apache License 2.0 7 votes vote down vote up
@Bean
Job personEtl(JobBuilderFactory jobBuilderFactory,
        StepBuilderFactory stepBuilderFactory,
        FlatFileItemReader<Person> reader,
        JdbcBatchItemWriter<Person> writer
) {

    Step step = stepBuilderFactory.get("file-to-database")
            .<Person, Person>chunk(5)
            .reader(reader)
            .writer(writer)
            .build();

    return jobBuilderFactory.get("etl")
            .start(step)
            .build();
}
 
Example #2
Source File: MultiFileIteamReaderDemo.java    From SpringAll with MIT License 7 votes vote down vote up
private FlatFileItemReader<TestData> fileItemReader() {
    FlatFileItemReader<TestData> reader = new FlatFileItemReader<>();
    reader.setLinesToSkip(1); // 忽略第一行

    // AbstractLineTokenizer的三个实现类之一,以固定分隔符处理行数据读取,
    // 使用默认构造器的时候,使用逗号作为分隔符,也可以通过有参构造器来指定分隔符
    DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
    // 设置属姓名,类似于表头
    tokenizer.setNames("id", "field1", "field2", "field3");
    // 将每行数据转换为TestData对象
    DefaultLineMapper<TestData> mapper = new DefaultLineMapper<>();
    mapper.setLineTokenizer(tokenizer);
    // 设置映射方式
    mapper.setFieldSetMapper(fieldSet -> {
        TestData data = new TestData();
        data.setId(fieldSet.readInt("id"));
        data.setField1(fieldSet.readString("field1"));
        data.setField2(fieldSet.readString("field2"));
        data.setField3(fieldSet.readString("field3"));
        return data;
    });

    reader.setLineMapper(mapper);
    return reader;
}
 
Example #3
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 #4
Source File: StoreInitializer.java    From spring-data-examples with Apache License 2.0 6 votes vote down vote up
/**
 * Reads a file {@code starbucks.csv} from the class path and parses it into {@link Store} instances about to
 * persisted.
 *
 * @return
 * @throws Exception
 */
public static List<Store> readStores() throws Exception {

	ClassPathResource resource = new ClassPathResource("starbucks.csv");
	Scanner scanner = new Scanner(resource.getInputStream());
	String line = scanner.nextLine();
	scanner.close();

	FlatFileItemReader<Store> itemReader = new FlatFileItemReader<Store>();
	itemReader.setResource(resource);

	// DelimitedLineTokenizer defaults to comma as its delimiter
	DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
	tokenizer.setNames(line.split(","));
	tokenizer.setStrict(false);

	DefaultLineMapper<Store> lineMapper = new DefaultLineMapper<Store>();
	lineMapper.setFieldSetMapper(fields -> {

		Point location = new Point(fields.readDouble("Longitude"), fields.readDouble("Latitude"));
		Address address = new Address(fields.readString("Street Address"), fields.readString("City"),
				fields.readString("Zip"), location);

		return new Store(UUID.randomUUID(), fields.readString("Name"), address);
	});

	lineMapper.setLineTokenizer(tokenizer);
	itemReader.setLineMapper(lineMapper);
	itemReader.setRecordSeparatorPolicy(new DefaultRecordSeparatorPolicy());
	itemReader.setLinesToSkip(1);
	itemReader.open(new ExecutionContext());

	List<Store> stores = new ArrayList<>();
	Store store = null;

	do {

		store = itemReader.read();

		if (store != null) {
			stores.add(store);
		}

	} while (store != null);

	return stores;
}
 
Example #5
Source File: AddressImportJobConfiguration.java    From spring-batch-lightmin with Apache License 2.0 6 votes vote down vote up
@Bean
@JobScope
public FlatFileItemReader<BatchTaskAddress> fileItemReader(@Value("#{jobParameters['fileSource']}") final String pathToFile,
                                                           final LineMapper<BatchTaskAddress> lineMapper) throws
        Exception {
    final FlatFileItemReader<BatchTaskAddress> reader = new FlatFileItemReader<>();
    reader.setEncoding("utf-8");
    reader.setLineMapper(lineMapper);
    reader.setLinesToSkip(1);
    reader.setResource(new FileSystemResource(pathToFile));
    reader.afterPropertiesSet();
    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: 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 #8
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 #9
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 #10
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 #11
Source File: SpringBatchConfiguration.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
@StepScope
public FlatFileItemReader<BookRecord> csvItemReader(@Value("#{jobParameters['file.input']}") String input) {
    FlatFileItemReaderBuilder<BookRecord> builder = new FlatFileItemReaderBuilder<>();
    FieldSetMapper<BookRecord> bookRecordFieldSetMapper = new BookRecordFieldSetMapper();
    LOGGER.info("Configuring reader to input {}", input);
    // @formatter:off
    return builder
      .name("bookRecordItemReader")
      .resource(new FileSystemResource(input))
      .delimited()
      .names(TOKENS)
      .fieldSetMapper(bookRecordFieldSetMapper)
      .build();
    // @formatter:on
}
 
Example #12
Source File: SpringBatchConfig.java    From tutorials with MIT License 5 votes vote down vote up
public ItemReader<Transaction> itemReader(Resource inputData) throws UnexpectedInputException, ParseException {
    FlatFileItemReader<Transaction> reader = new FlatFileItemReader<>();
    DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
    String[] tokens = {"username", "userid", "transactiondate", "amount"};
    tokenizer.setNames(tokens);
    reader.setResource(inputData);
    DefaultLineMapper<Transaction> lineMapper = new DefaultLineMapper<>();
    lineMapper.setLineTokenizer(tokenizer);
    lineMapper.setFieldSetMapper(new RecordFieldSetMapper());
    reader.setLinesToSkip(1);
    reader.setLineMapper(lineMapper);
    return reader;
}
 
Example #13
Source File: SpringBatchRetryConfig.java    From tutorials with MIT License 5 votes vote down vote up
public ItemReader<Transaction> itemReader(Resource inputData) throws ParseException {
    DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
    tokenizer.setNames(tokens);
    DefaultLineMapper<Transaction> lineMapper = new DefaultLineMapper<>();
    lineMapper.setLineTokenizer(tokenizer);
    lineMapper.setFieldSetMapper(new RecordFieldSetMapper());
    FlatFileItemReader<Transaction> reader = new FlatFileItemReader<>();
    reader.setResource(inputData);
    reader.setLinesToSkip(1);
    reader.setLineMapper(lineMapper);
    return reader;
}
 
Example #14
Source File: SpringbatchPartitionConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
@StepScope
public FlatFileItemReader<Transaction> itemReader(@Value("#{stepExecutionContext[fileName]}") String filename) throws UnexpectedInputException, ParseException {
    FlatFileItemReader<Transaction> reader = new FlatFileItemReader<>();
    DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
    String[] tokens = {"username", "userid", "transactiondate", "amount"};
    tokenizer.setNames(tokens);
    reader.setResource(new ClassPathResource("input/partitioner/" + filename));
    DefaultLineMapper<Transaction> lineMapper = new DefaultLineMapper<>();
    lineMapper.setLineTokenizer(tokenizer);
    lineMapper.setFieldSetMapper(new RecordFieldSetMapper());
    reader.setLinesToSkip(1);
    reader.setLineMapper(lineMapper);
    return reader;
}
 
Example #15
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();
}
 
Example #16
Source File: FlatFileToDbSkipJobConfiguration.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
@Bean
@StepScope
public FlatFileItemReader<Item> flatFileItemReader(@Value("#{jobParameters[pathToFile]}") String pathToFile) {
	FlatFileItemReader<Item> itemReader = new FlatFileItemReader<Item>();
	itemReader.setLineMapper(lineMapper());
	itemReader.setResource(new FileSystemResource("src/test/resources/" + pathToFile));
	return itemReader;
}
 
Example #17
Source File: FlatFileToDbSkipProcessorNonTransactionalJobConfiguration.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
@Bean
@StepScope
public FlatFileItemReader<Item> flatFileItemReader(@Value("#{jobParameters[pathToFile]}") String pathToFile) {
	FlatFileItemReader<Item> itemReader = new FlatFileItemReader<Item>();
	itemReader.setLineMapper(lineMapper());
	itemReader.setResource(new FileSystemResource("src/test/resources/" + pathToFile));
	return itemReader;
}
 
Example #18
Source File: FlatFileToDbSkipReaderTransactionalJobConfiguration.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
@Bean
@StepScope
public FlatFileItemReader<Item> flatFileItemReader(@Value("#{jobParameters[pathToFile]}") String pathToFile) {
	FlatFileItemReader<Item> itemReader = new FlatFileItemReader<Item>();
	itemReader.setLineMapper(lineMapper());
	itemReader.setResource(new FileSystemResource("src/test/resources/" + pathToFile));
	return itemReader;
}
 
Example #19
Source File: FlatFileToDbNoSkipJobConfiguration.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
@Bean
@StepScope
public FlatFileItemReader<Item> flatFileItemReader(@Value("#{jobParameters[pathToFile]}") String pathToFile) {
	FlatFileItemReader<Item> itemReader = new FlatFileItemReader<Item>();
	itemReader.setLineMapper(lineMapper());
	itemReader.setResource(new FileSystemResource("src/test/resources/" + pathToFile));
	return itemReader;
}
 
Example #20
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 #21
Source File: AddressImportJobConfiguration.java    From spring-batch-lightmin with Apache License 2.0 5 votes vote down vote up
@Bean
public Step addressImportStep(final FlatFileItemReader<BatchTaskAddress> fileItemReader,
                              final ItemWriter<BatchTaskAddress> addressDatabaseWriter,
                              final StepBuilderFactory stepBuilderFactory) {
    return stepBuilderFactory
            .get("addressImportStep")
            .<BatchTaskAddress, BatchTaskAddress>chunk(1)
            .reader(fileItemReader)
            .writer(addressDatabaseWriter)
            .build();
}
 
Example #22
Source File: CapitalizeNamesJobConfig.java    From spring-batch with MIT License 5 votes vote down vote up
@Bean
public FlatFileItemReader<Person> itemReader() {
  return new FlatFileItemReaderBuilder<Person>()
      .name("personItemReader")
      .resource(new ClassPathResource("csv/persons.csv"))
      .delimited().names(new String[] {"firstName", "lastName"})
      .targetType(Person.class).build();
}
 
Example #23
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 #24
Source File: BatchConfiguration.java    From messaging with Apache License 2.0 5 votes vote down vote up
@Bean
@StepScope
FlatFileItemReader<Contact> fileReader(
 @Value("file://#{jobParameters['file']}") Resource pathToFile)
 throws Exception {
 return new FlatFileItemReaderBuilder<Contact>().name("file-reader")
  .resource(pathToFile).targetType(Contact.class).delimited()
  .names("fullName,email".split(",")).build();
}
 
Example #25
Source File: BatchJobConfiguration.java    From patient-batch-loader with GNU General Public License v3.0 5 votes vote down vote up
@Bean
@StepScope
public FlatFileItemReader<PatientRecord> reader(
    @Value("#{jobParameters['" + Constants.JOB_PARAM_FILE_NAME + "']}")String fileName) {
    return new FlatFileItemReaderBuilder<PatientRecord>()
        .name(Constants.ITEM_READER_NAME)
        .resource(
            new PathResource(
                Paths.get(applicationProperties.getBatch().getInputPath() +
                    File.separator + fileName)))
        .linesToSkip(1)
        .lineMapper(lineMapper())
        .build();
}
 
Example #26
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 #27
Source File: StepService0004.java    From seed with Apache License 2.0 5 votes vote down vote up
private FlatFileItemReader<Person> reader(){
    return new FlatFileItemReaderBuilder<Person>()
            .name("step0001Reader")
            .resource(new FileSystemResource("/data/seedboot-batch.txt"))
            .strict(true)
            .encoding(SeedConstants.DEFAULT_CHARSET)
            .linesToSkip(1)
            .delimited().delimiter("|")
            .names(new String[]{"realName", "age", "birthDay"})
            .targetType(Person.class).customEditors(SettleJobConfiguration.customEditors)
            .build();
}
 
Example #28
Source File: StepService0006.java    From seed with Apache License 2.0 5 votes vote down vote up
private FlatFileItemReader<Person> reader(){
    return new FlatFileItemReaderBuilder<Person>()
            .name("step0001Reader")
            .resource(new FileSystemResource("/data/seedboot-batch.txt"))
            .strict(true)
            .encoding(SeedConstants.DEFAULT_CHARSET)
            .linesToSkip(1)
            .delimited().delimiter("|")
            .names(new String[]{"realName", "age", "birthDay"})
            .targetType(Person.class).customEditors(SettleJobConfiguration.customEditors)
            .build();
}
 
Example #29
Source File: CapitalizeNamesJobConfig.java    From spring-batch with MIT License 5 votes vote down vote up
@Bean
public FlatFileItemReader<Person> itemReader() {
  return new FlatFileItemReaderBuilder<Person>()
      .name("personItemReader").delimited()
      .names(new String[] {"firstName", "lastName"})
      .targetType(Person.class).build();
}
 
Example #30
Source File: FileItemReaderDemo.java    From SpringAll with MIT License 5 votes vote down vote up
private ItemReader<TestData> fileItemReader() {
    FlatFileItemReader<TestData> reader = new FlatFileItemReader<>();
    reader.setResource(new ClassPathResource("file")); // 设置文件资源地址
    reader.setLinesToSkip(1); // 忽略第一行

    // AbstractLineTokenizer的三个实现类之一,以固定分隔符处理行数据读取,
    // 使用默认构造器的时候,使用逗号作为分隔符,也可以通过有参构造器来指定分隔符
    DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();

    // 设置属性名,类似于表头
    tokenizer.setNames("id", "field1", "field2", "field3");
    // 将每行数据转换为TestData对象
    DefaultLineMapper<TestData> mapper = new DefaultLineMapper<>();
    mapper.setLineTokenizer(tokenizer);
    // 设置映射方式
    mapper.setFieldSetMapper(fieldSet -> {
        TestData data = new TestData();
        data.setId(fieldSet.readInt("id"));
        data.setField1(fieldSet.readString("field1"));
        data.setField2(fieldSet.readString("field2"));
        data.setField3(fieldSet.readString("field3"));
        return data;
    });

    reader.setLineMapper(mapper);
    return reader;
}