ItemWriter


Let’s start with a Job and ItermReader

@Configuration
public class DbOutputDemoJobConfiguration {
 
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
 
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
 
    @Autowired
    @Qualifier("dbOutputDemoJobFlatFileReader")
    public ItemReader<Customer> dbOutputDemoJobFlatFileReader;
 
    @Autowired
    @Qualifier("dbOutputDemoJobFlatFileWriter")
    public ItemWriter<Customer> dbOutputDemoJobFlatFileWriter;
 
    @Bean
    public Step dbOutputDemoStep() {
        return stepBuilderFactory.get("dbOutputDemoStep")
                .<Customer,Customer>chunk(10)
                .reader(dbOutputDemoJobFlatFileReader)
                .writer(dbOutputDemoJobFlatFileWriter)
                .build();
    }
 
    @Bean
    public Job dbOutputDemoJob() {
        return jobBuilderFactory.get("dbOutputDemoJob")
                .start(dbOutputDemoStep())
                .build();
    }
}
 
@Configuration
public class DbOutputDemoJobReaderConfiguration {
 
    @Bean
    public FlatFileItemReader<Customer> dbOutputDemoJobFlatFileReader() {
        FlatFileItemReader<Customer> reader = new FlatFileItemReader<>();
        reader.setResource(new ClassPathResource("customerInit.csv"));
        DefaultLineMapper<Customer> customerLineMapper = new DefaultLineMapper<>();
 
        DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
        tokenizer.setNames(new String[] {"id"."firstName"."lastName"."birthdate"});
 
        customerLineMapper.setLineTokenizer(tokenizer);
        customerLineMapper.setFieldSetMapper((fieldSet -> {
            return Customer.builder().id(fieldSet.readLong("id"))
                    .firstName(fieldSet.readString("firstName"))
                    .lastName(fieldSet.readString("lastName"))
                    .birthdate(fieldSet.readString("birthdate"))
                    .build();
        }));
        customerLineMapper.afterPropertiesSet();
        reader.setLineMapper(customerLineMapper);
        returnreader; }}Copy the code






Data is written to the database


Text data to be stored

The database table

JdbcBatchItemWriter

@Configuration
public class DbOutputDemoJobWriterConfiguration {
 
    @Autowired
    public DataSource dataSource;
 
    @Bean
    public JdbcBatchItemWriter<Customer> dbOutputDemoJobFlatFileWriter(){ JdbcBatchItemWriter<Customer> itemWriter = new JdbcBatchItemWriter<>(); // set the dataSource itemwriter.setdatasource (dataSource); Itemwriter. setSql(itemWriter. itemwriter. setSql("insert into customer(id,firstName,lastName,birthdate) values " +
                "(:id,:firstName,:lastName,:birthdate)"); / / replace attribute value itemWriter. SetItemSqlParameterSourceProvider (new BeanPropertyItemSqlParameterSourceProvider < > ());returnitemWriter; }}Copy the code


The execution result






Data is written to a.data file


FlatFileItemWriter can write data from any object of type T to a plain file

We read and write the data from customerInit.csv to the file customerInfo.data

FlatFileItemWriter

@Configuration public class FlatFileDemoJobWriterConfiguration { @Bean public FlatFileItemWriter<Customer> flatFileDemoFlatFileWriter() throws Exception { FlatFileItemWriter<Customer> itemWriter = new FlatFileItemWriter<>(); String path = file.createTempFile (String path = file.createTempFile ("customerInfo".".data").getAbsolutePath();
        System.out.println(">> file is created in: "+ path); itemWriter.setResource(new FileSystemResource(path)); . / / the Customer object into a string itemWriter setLineAggregator (new MyCustomerLineAggregator ()); itemWriter.afterPropertiesSet();returnitemWriter; } } public class MyCustomerLineAggregator implements LineAggregator<Customer> { //JSON private ObjectMapper mapper = new  ObjectMapper(); @Override public String aggregate(Customer customer) { try {return mapper.writeValueAsString(customer);
        } catch (JsonProcessingException e) {
           throw new RuntimeException("Unable to serialize.",e); }}}Copy the code






Data is written to an XML file


To write data to XML files, StaxEventItemWriter must be used, and XStreamMarshaller is also used to sequence files

StaxEventItemWriter

@Configuration public class XMLFileDemoJobWriterConfiguration { @Bean public StaxEventItemWriter<Customer> XmlFileDemoXMLFileWriter () throws Exception {// Converting an object to XML XStreamMarshaller Marshaller = new XStreamMarshaller(); Map<String,Class> aliases = new HashMap<>(); aliases.put("customer",Customer.class); marshaller.setAliases(aliases); StaxEventItemWriter<Customer> itemWriter = new StaxEventItemWriter<>(); / / specify the root tag itemWriter. SetRootTagName ("customers"); itemWriter.setMarshaller(marshaller); String path = file.createTempFile (String path = file.createTempFile ("customerInfo".".xml").getAbsolutePath();
        System.out.println(">> xml file is generated: " + path);
        itemWriter.setResource(new FileSystemResource(path));
        itemWriter.afterPropertiesSet();
 
        returnitemWriter; }}Copy the code









Data is written to multiple files


To write data into multiple files, you need to use CompositItemWriter ClassifierCompositItemWriter or use

Differences between the two:

  • A CompositeItemWriter writes full data into multiple files;

  • ClassifierCompositeItemWriter according to specified rules, to meet the conditions of data to a specified file;


The data are written to the XML file and json file, realized in CompositeItemWriter, ClassifierCompositeItemWriter written to the file


@bean public StaxEventItemWriter<Customer> xmlFileWriter() throws Exception {// Convert an object to XML XStreamMarshaller marshaller = new XStreamMarshaller(); Map<String,Class> aliases = new HashMap<>(); aliases.put("customer",Customer.class); marshaller.setAliases(aliases); StaxEventItemWriter<Customer> itemWriter = new StaxEventItemWriter<>(); / / specify the root tag itemWriter. SetRootTagName ("customers"); itemWriter.setMarshaller(marshaller); String path = file.createTempFile (String path = file.createTempFile ("multiInfo".".xml").getAbsolutePath();
        System.out.println(">> xml file is created in: " + path);
        itemWriter.setResource(new FileSystemResource(path));
        itemWriter.afterPropertiesSet();
 
        returnitemWriter; } @Bean public FlatFileItemWriter<Customer> jsonFileWriter() throws Exception { FlatFileItemWriter<Customer> itemWriter = new FlatFileItemWriter<>(); String path = file.createTempFile (String path = file.createTempFile ("multiInfo".".json").getAbsolutePath();
        System.out.println(">> json file is created in: " + path);
        itemWriter.setResource(new FileSystemResource(path));
 
        itemWriter.setLineAggregator(new MyCustomerLineAggregator());
        itemWriter.afterPropertiesSet();
 
        return itemWriter;
 
    }
Copy the code


CompositeItemWriter













ClassifierCompositeItemWriter


Using ClassifierCompositeItemWriter according to the rules of output data to a file

    @Bean
    public ClassifierCompositeItemWriter<Customer> customerCompositeItemWriter() throws Exception {
       
        ClassifierCompositeItemWriter<Customer> itemWriter = new ClassifierCompositeItemWriter<>();
        itemWriter.setClassifier(new MyCustomerClassifier(xmlFileWriter(),jsonFileWriter()));
        return itemWriter;
    }
Copy the code


private ItemWriter<Customer> xmlWriter; private ItemWriter<Customer> jsonWriter; public MyCustomerClassifier(ItemWriter<Customer> xmlWriter, ItemWriter<Customer> jsonWriter) { this.xmlWriter = xmlWriter; this.jsonWriter = jsonWriter; } @Override public ItemWriter<? Super Customer> classify(Customer item) {return item.getid ()%2 == 0? jsonWriter:xmlWriter; }Copy the code

}

<br/> Output result! [file](https://graph.baidu.com/resource/222030be37e99c630058301583333232.png) <br/> ! [file] (https://graph.baidu.com/resource/222f005db65f6706ed1a001583333317.png) < br / > < br / > the reference:  https://blog.csdn.net/wuzhiwei549/article/details/88593942 https://blog.51cto.com/13501268/2298822Copy the code