You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Batch读取CSV转对象并写入MongoDB技术咨询

Solution: Convert FileObj to OBJ2 and Write to MongoDB in Spring Batch

To handle the conversion from FileObj to OBJ2 and persist to MongoDB, you'll need to add an ItemProcessor (for transformation) and configure the MongoDB writer correctly. Here's how to implement this end-to-end:


1. Create the ItemProcessor for Conversion

The ItemProcessor is responsible for transforming your input FileObj into the output OBJ2 that will be written to MongoDB. Create a class implementing ItemProcessor<FileObj, OBJ2>:

import org.springframework.batch.item.ItemProcessor;

public class FileObjToOBJ2Processor implements ItemProcessor<FileObj, OBJ2> {

    @Override
    public OBJ2 process(FileObj fileObj) throws Exception {
        // Map fields from FileObj to OBJ2 - adjust this to match your actual field names
        OBJ2 obj2 = new OBJ2();
        obj2.setId(fileObj.getFileId()); // Example mapping
        obj2.setName(fileObj.getFileName());
        obj2.setDataValue(fileObj.getFileData());
        // Add all necessary field mappings here

        return obj2;
    }
}

Register this processor as a Spring bean:

@Bean
public ItemProcessor<FileObj, OBJ2> fileObjToOBJ2Processor() {
    return new FileObjToOBJ2Processor();
}

2. Update the Step Definition to Include the Processor

Modify your Step bean to:

  • Change the chunk generic type to <FileObj, OBJ2> (reflecting input and output types)
  • Add the processor between the reader and writer
@Bean
public Step step(ItemProcessor<FileObj, OBJ2> processor, MongoItemWriter<OBJ2> writer) {
    return stepBuilderFactory
            .get("step")
            .<FileObj, OBJ2>chunk(5) // Updated generic type to match transformation
            .reader(reader(null)) // Your existing StepScoped reader (null is okay for proxying)
            .processor(processor) // Insert conversion processor here
            .faultTolerant()
            .skipPolicy(fileVerificationSkipper())
            .writer(writer)
            .build();
}

3. Configure the MongoDB Writer

You have two common options to implement the MongoDB writer. Choose the one that fits your setup:

Option A: Using MongoTemplate (Direct MongoDB Access)

If you prefer using MongoTemplate directly, configure the writer like this:

import org.springframework.batch.item.data.MongoItemWriter;
import org.springframework.data.mongodb.core.MongoTemplate;

@Bean
public MongoItemWriter<OBJ2> writer(MongoTemplate mongoTemplate) {
    MongoItemWriter<OBJ2> writer = new MongoItemWriter<>();
    writer.setMongoTemplate(mongoTemplate);
    writer.setCollection("obj2_collection"); // Replace with your MongoDB collection name
    return writer;
}

Option B: Using Spring Data MongoDB Repository

If you have a Spring Data repository for OBJ2, use RepositoryItemWriter instead:

First, define your repository interface:

import org.springframework.data.mongodb.repository.MongoRepository;

public interface OBJ2Repository extends MongoRepository<OBJ2, String> {
}

Then configure the writer:

import org.springframework.batch.item.data.RepositoryItemWriter;

@Bean
public RepositoryItemWriter<OBJ2> writer(OBJ2Repository obj2Repository) {
    RepositoryItemWriter<OBJ2> writer = new RepositoryItemWriter<>();
    writer.setRepository(obj2Repository);
    writer.setMethodName("save"); // Uses the repository's save() method for each item
    return writer;
}

4. Ensure OBJ2 is a MongoDB-Mapped Entity

Make sure your OBJ2 class is annotated as a MongoDB document:

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "obj2_collection") // Match the collection name from your writer
public class OBJ2 {
    @Id
    private String id;
    // Add your fields, getters, and setters here
}

5. Verify Dependencies

Ensure you have the required dependencies in your project:

Maven (pom.xml)

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.batch</groupId>
        <artifactId>spring-batch-mongodb</artifactId>
    </dependency>
</dependencies>

Gradle (build.gradle)

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-batch'
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
    implementation 'org.springframework.batch:spring-batch-mongodb'
}

This setup will:

  1. Read CSV rows into FileObj instances via your existing FlatFileItemReader
  2. Convert each FileObj to OBJ2 using the processor
  3. Write the transformed OBJ2 instances to MongoDB in chunks of 5

内容的提问来源于stack exchange,提问作者Sonali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 14:57:37