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

The following examples show how to use org.springframework.batch.item.file.mapping.DefaultLineMapper. 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
@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 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
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 4
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 5
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 6
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 7
@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
@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 9
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 10
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 11
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 12
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;
}
 
Example 13
Source Project: tutorials   Source File: SpringBatchRetryConfig.java    License: 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 Project: tutorials   Source File: SpringBatchConfig.java    License: 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;
}