Java Code Examples for org.springframework.batch.item.file.FlatFileItemReader#setLineMapper()
The following examples show how to use
org.springframework.batch.item.file.FlatFileItemReader#setLineMapper() .
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 |
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: UserReader.java From spring-cloud with Apache License 2.0 | 6 votes |
@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 File: ConvertNamesJobConfig.java From spring-batch with MIT License | 6 votes |
@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 |
@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: BatchConfiguration.java From building-microservices with Apache License 2.0 | 6 votes |
@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 6
Source File: AddressImportJobConfiguration.java From spring-batch-lightmin with Apache License 2.0 | 6 votes |
@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 7
Source File: StoreInitializer.java From spring-data-examples with Apache License 2.0 | 6 votes |
/** * 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 8
Source File: FileItemReaderDemo.java From SpringAll with MIT License | 5 votes |
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 9
Source File: FlatFileToDbNoSkipJobConfiguration.java From spring-boot-starter-batch-web with Apache License 2.0 | 5 votes |
@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 10
Source File: FlatFileToDbSkipReaderTransactionalJobConfiguration.java From spring-boot-starter-batch-web with Apache License 2.0 | 5 votes |
@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 11
Source File: FlatFileToDbSkipProcessorNonTransactionalJobConfiguration.java From spring-boot-starter-batch-web with Apache License 2.0 | 5 votes |
@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 12
Source File: FlatFileToDbSkipJobConfiguration.java From spring-boot-starter-batch-web with Apache License 2.0 | 5 votes |
@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 13
Source File: SpringbatchPartitionConfig.java From tutorials with MIT License | 5 votes |
@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 14
Source File: SpringBatchRetryConfig.java From tutorials with MIT License | 5 votes |
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 15
Source File: SpringBatchConfig.java From tutorials with MIT License | 5 votes |
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; }