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

The following examples show how to use org.springframework.batch.item.file.mapping.DefaultLineMapper. 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: 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 #2
Source File: BatchJobConfiguration.java    From patient-batch-loader with GNU General Public License v3.0 7 votes vote down vote up
@Bean
public LineMapper<PatientRecord> lineMapper() {
    DefaultLineMapper<PatientRecord> mapper = new DefaultLineMapper<>();
    mapper.setFieldSetMapper((fieldSet) -> new PatientRecord(
        fieldSet.readString(0), fieldSet.readString(1),
        fieldSet.readString(2), fieldSet.readString(3),
        fieldSet.readString(4), fieldSet.readString(5),
        fieldSet.readString(6), fieldSet.readString(7),
        fieldSet.readString(8), fieldSet.readString(9),
        fieldSet.readString(10), fieldSet.readString(11),
        fieldSet.readString(12)));
    mapper.setLineTokenizer(new DelimitedLineTokenizer());
    return mapper;
}
 
Example #3
Source File: BatchConfiguration.java    From batch-processing-large-datasets-spring with MIT License 6 votes vote down vote up
@Bean
public LineMapper<Voltage> lineMapper() {

    final DefaultLineMapper<Voltage> defaultLineMapper = new DefaultLineMapper<>();
    final DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer();
    lineTokenizer.setDelimiter(";");
    lineTokenizer.setStrict(false);
    lineTokenizer.setNames(new String[] {"volt","time"});

    final VoltageFieldSetMapper fieldSetMapper = new VoltageFieldSetMapper();
    defaultLineMapper.setLineTokenizer(lineTokenizer);
    defaultLineMapper.setFieldSetMapper(fieldSetMapper);

    return defaultLineMapper;
}
 
Example #4
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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;
}
 
Example #12
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 #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: 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;
}