Search in sources :

Example 1 with Transaction

use of org.baeldung.batch.model.Transaction in project tutorials by eugenp.

the class SpringBatchConfig method itemReader.

@Bean
public ItemReader<Transaction> itemReader() throws UnexpectedInputException, ParseException {
    FlatFileItemReader<Transaction> reader = new FlatFileItemReader<Transaction>();
    DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
    String[] tokens = { "username", "userid", "transactiondate", "amount" };
    tokenizer.setNames(tokens);
    reader.setResource(inputCsv);
    DefaultLineMapper<Transaction> lineMapper = new DefaultLineMapper<Transaction>();
    lineMapper.setLineTokenizer(tokenizer);
    lineMapper.setFieldSetMapper(new RecordFieldSetMapper());
    reader.setLinesToSkip(1);
    reader.setLineMapper(lineMapper);
    return reader;
}
Also used : RecordFieldSetMapper(org.baeldung.batch.service.RecordFieldSetMapper) FlatFileItemReader(org.springframework.batch.item.file.FlatFileItemReader) DelimitedLineTokenizer(org.springframework.batch.item.file.transform.DelimitedLineTokenizer) Transaction(org.baeldung.batch.model.Transaction) DefaultLineMapper(org.springframework.batch.item.file.mapping.DefaultLineMapper) Bean(org.springframework.context.annotation.Bean)

Example 2 with Transaction

use of org.baeldung.batch.model.Transaction in project tutorials by eugenp.

the class SpringbatchPartitionConfig method itemWriter.

@Bean(destroyMethod = "")
@StepScope
public StaxEventItemWriter<Transaction> itemWriter(Marshaller marshaller, @Value("#{stepExecutionContext[opFileName]}") String filename) throws MalformedURLException {
    StaxEventItemWriter<Transaction> itemWriter = new StaxEventItemWriter<>();
    itemWriter.setMarshaller(marshaller);
    itemWriter.setRootTagName("transactionRecord");
    itemWriter.setResource(new FileSystemResource("src/main/resources/output/" + filename));
    return itemWriter;
}
Also used : StaxEventItemWriter(org.springframework.batch.item.xml.StaxEventItemWriter) Transaction(org.baeldung.batch.model.Transaction) FileSystemResource(org.springframework.core.io.FileSystemResource) StepScope(org.springframework.batch.core.configuration.annotation.StepScope) JobRepositoryFactoryBean(org.springframework.batch.core.repository.support.JobRepositoryFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 3 with Transaction

use of org.baeldung.batch.model.Transaction in project tutorials by eugenp.

the class SpringbatchPartitionConfig method itemReader.

@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;
}
Also used : RecordFieldSetMapper(org.baeldung.batch.service.RecordFieldSetMapper) FlatFileItemReader(org.springframework.batch.item.file.FlatFileItemReader) DelimitedLineTokenizer(org.springframework.batch.item.file.transform.DelimitedLineTokenizer) Transaction(org.baeldung.batch.model.Transaction) DefaultLineMapper(org.springframework.batch.item.file.mapping.DefaultLineMapper) ClassPathResource(org.springframework.core.io.ClassPathResource) StepScope(org.springframework.batch.core.configuration.annotation.StepScope) JobRepositoryFactoryBean(org.springframework.batch.core.repository.support.JobRepositoryFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 4 with Transaction

use of org.baeldung.batch.model.Transaction in project tutorials by eugenp.

the class RecordFieldSetMapper method mapFieldSet.

public Transaction mapFieldSet(FieldSet fieldSet) throws BindException {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    Transaction transaction = new Transaction();
    // you can either use the indices or custom names
    // I personally prefer the custom names easy for debugging and
    // validating the pipelines
    transaction.setUsername(fieldSet.readString("username"));
    transaction.setUserId(fieldSet.readInt("userid"));
    transaction.setAmount(fieldSet.readDouble(3));
    // Converting the date
    String dateString = fieldSet.readString(2);
    try {
        transaction.setTransactionDate(dateFormat.parse(dateString));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return transaction;
}
Also used : Transaction(org.baeldung.batch.model.Transaction) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

Transaction (org.baeldung.batch.model.Transaction)4 Bean (org.springframework.context.annotation.Bean)3 RecordFieldSetMapper (org.baeldung.batch.service.RecordFieldSetMapper)2 StepScope (org.springframework.batch.core.configuration.annotation.StepScope)2 JobRepositoryFactoryBean (org.springframework.batch.core.repository.support.JobRepositoryFactoryBean)2 FlatFileItemReader (org.springframework.batch.item.file.FlatFileItemReader)2 DefaultLineMapper (org.springframework.batch.item.file.mapping.DefaultLineMapper)2 DelimitedLineTokenizer (org.springframework.batch.item.file.transform.DelimitedLineTokenizer)2 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 StaxEventItemWriter (org.springframework.batch.item.xml.StaxEventItemWriter)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 FileSystemResource (org.springframework.core.io.FileSystemResource)1