简体   繁体   中英

Spring batch to save csv files records in Mongo DB

I am working on spring batch which reads record from csv file from one location and save into mongo DB. I want to read the records in particular Object type but while writing it should be in different object type. Like if I am reading file with obj1 then I want to set obj1 to obj2 and save to DB. Below is my code.

@Bean
    public Job readCSVFileJob() {
        return jobBuilderFactory
                .get("readCSVFileJob")
                .incrementer(new RunIdIncrementer())
                .start(step())
                .build();
    }

    @Bean
    public Step step() {
        return stepBuilderFactory
                .get("step")
                .<FileObj, FileObj>chunk(5)
                .reader(reader(null))
                .faultTolerant().skipPolicy(fileVerificationSkipper())
                .writer(writer())
                .build();
    }

    @Bean
    public SkipPolicy fileVerificationSkipper() {
        return new FileVerificationSkipper();
    }

    @Bean
    @StepScope
    public FlatFileItemReader<FileObj> reader(@Value("#{jobParameters['fullPathFileName']}") String fullPathFileName) {
        FlatFileItemReader<FileObj> itemReader = new FlatFileItemReader<FileObj>();
        itemReader.setLineMapper(lineMapper());
        itemReader.setLinesToSkip(1);
        try {
            itemReader.setResource(new PathResource(fullPathFileName)); // set resource from a FTP location
        } catch (Exception e) {
            e.printStackTrace();
        }
        return itemReader;
    }

    @Bean
    public LineMapper<FileObj> lineMapper() {
       //code for line mapper this works fine
        return lineMapper;
    }

    @Bean
    public MongoItemWriter<OBJ2> writer() {
       // in this I want to write in mongo DB with OBJ2 something like below
    repository.save(obj2);
        return writer;
    }

You need to add an ItemProcessor that transforms obj1 items to obj2 . Data transformation is a typical use case for an ItemProcessor .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM