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

The following examples show how to use org.springframework.batch.item.file.FlatFileItemReader. 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: building-microservices   Source File: BatchConfiguration.java    License: 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 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 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
Source Project: SpringAll   Source File: MultiFileIteamReaderDemo.java    License: MIT License 6 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 6
@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 7
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 8
@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 9
Source Project: spring-data-examples   Source File: StoreInitializer.java    License: 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 10
@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 11
@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 12
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 13
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 14
Source Project: spring-batch   Source File: CapitalizeNamesJobConfig.java    License: 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 15
Source Project: spring-batch   Source File: CapitalizeNamesJobConfig.java    License: 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 16
Source Project: spring-batch   Source File: CapitalizeNamesJobConfig.java    License: 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 17
Source Project: spring-batch   Source File: CapitalizeNamesJobConfig.java    License: 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 18
Source Project: spring-batch   Source File: HelloWorldJobConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public FlatFileItemReader<Person> reader() {
  return new FlatFileItemReaderBuilder<Person>()
      .name("personItemReader")
      .resource(new ClassPathResource("csv/persons.csv"))
      .delimited().names(new String[] {"firstName", "lastName"})
      .targetType(Person.class).build();
}
 
Example 19
Source Project: SpringAll   Source File: FileItemReaderDemo.java    License: 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;
}
 
Example 20
Source Project: seed   Source File: StepService0006.java    License: 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 21
Source Project: seed   Source File: StepService0004.java    License: 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 22
@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 23
Source Project: messaging   Source File: BatchConfiguration.java    License: 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 24
@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 25
@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 26
@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 27
@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 28
@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 29
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();
}
 
Example 30
Source Project: tutorials   Source File: SpringbatchPartitionConfig.java    License: 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;
}