Batch Processing - MongoDB to CSV Export using Spring Batch
Last Updated :
28 Apr, 2025
Batch processing is a common requirement while dealing with large volumes of data. Batch processing plays an important role in handling large datasets efficiently using chunks or batches. Spring framework provides a flexible framework for building batch-processing applications in Java.
Steps to Export MongoDB to CSV File
Below are the steps to read from MongoDB and generate CSV files or export to CSV files.
Step 1: Create Spring Boot Project
Create a Spring boot starter project and add the necessary dependencies. Here is the attachment on how to create and add the dependencies.

Create the project and click on next in the next step add the required dependencies as shown below.

For Batch Processing, the required dependencies are Spring Batch, Spring Data MongoDB.
Step 2: Creating the Entity
Let us consider Employee as an entity class for better understanding.
Define the Employee Entity class with the fields representing the employee attributes and after annotating the class with @Document annotation to specify the MongoDB collection name.
Java
package com.app.entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* Represents an Employee entity.
*/
@Document(collection = "Employee")
public class Employee {
@Id
private int id;
private String firstName;
private String lastName;
private String department;
private int salary;
public Employee() {
super();
}
//Constructor
public Employee(int id, String firstName, String lastName, String department, int salary) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.department = department;
this.salary = salary;
}
// getter and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
In the above Java class, we have added some required attributes.
Step 3: Configuring the Spring batch
Create the configuration class for batch processing and annotate the class with the annotations @Configuration and @EnableBatchProcessing, define a job in the corresponding class to read the data from MongoDb and Write it to a CSV file using Spring Batch.
Here is the MongoDBReader class.
Java
import java.util.HashMap;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.data.MongoItemReader;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor;
import org.springframework.batch.item.file.transform.DelimitedLineAggregator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import com.app.entity.Employee;
// Configuration class for reading data from MongoDB
@Configuration
@EnableBatchProcessing
public class MongoDbReader {
// Autowiring JobBuilderFactory to create jobs
@Autowired
private JobBuilderFactory jobBuilderFactory;
// Autowiring StepBuilderFactory to create steps
@Autowired
private StepBuilderFactory stepBuilderFactory;
// Autowiring MongoTemplate for MongoDB operations
@Autowired
private MongoTemplate mongoTemplate;
// Method to create a job for reading employee data
@Bean
public Job readEmployee() throws Exception {
return jobBuilderFactory.get("readEmployee").flow(step1()).end().build();
}
// Method to create a step for processing employee data
@Bean
public Step step1() throws Exception {
return stepBuilderFactory.get("step1").<Employee, Employee>chunk(5).reader(reader())
.writer(writer()).build();
}
// Method to create a reader for reading employee data from MongoDB
@Bean
public MongoItemReader<Employee> reader() {
MongoItemReader<Employee> reader = new MongoItemReader<>();
reader.setTemplate(mongoTemplate);
reader.setSort(new HashMap<String, Sort.Direction>() {{
put("_id", Direction.DESC);
}});
reader.setTargetType(Employee.class);
reader.setQuery("{}");
return reader;
}
// Method to create a writer for writing employee data to a CSV file
@Bean
public FlatFileItemWriter<Employee> writer() {
FlatFileItemWriter<Employee> writer = new FlatFileItemWriter<>();
writer.setResource(new FileSystemResource("src/main/resources/employee.csv"));
// Use append mode
writer.setAppendAllowed(true);
DelimitedLineAggregator<Employee> dl = new DelimitedLineAggregator<>();
BeanWrapperFieldExtractor<Employee> bf = new BeanWrapperFieldExtractor<>();
bf.setNames(new String[]{"id", "firstName", "lastName", "department", "salary"});
dl.setFieldExtractor(bf);
writer.setLineAggregator(dl);
return writer;
}
}
In the above class we have done the following things:
- Implemented the ItemReader: The custom ItemReader reader() method in the above class is used to read the data from MongoDB using the MongoItemReader. We have configured the reader to connect to MongoDB.
- Implemented the ItemWriter: We have used the custom ItemWriter writer() method in the above class to write the data to CSV file using FlatFileItemWriter. We have configured the writer to specify the outpul file location and format of the data.
Step 4: Scheduling the Batch Job
Create the configuration class with the annotations @Configuration and @EnableScheduling and implement a scheduled task to trigger the spring batch job at regular intervals using @Scheduled annotation, there operation can be performed based on the requirement.
Java
package com.app.config;
import java.text.SimpleDateFormat;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
@Configuration
@EnableScheduling
public class SchedulerConfig {
// Inject JobLauncher and Job beans from Spring context
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
// Create a SimpleDateFormat object for date formatting
private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
// Define a scheduled task using @Scheduled annotation
// The task will be executed every 50 seconds with an initial delay of 5 seconds
@Scheduled(fixedDelay = 50000, initialDelay = 5000)
public void scheduleByFixedRate() throws Exception {
System.out.println("Batch job starting");
try {
// Create JobParameters with a unique identifier
JobParameters jobParameters = new JobParametersBuilder()
.addLong("startAt", System.currentTimeMillis()).toJobParameters();
// Run the job using JobLauncher and JobParameters
jobLauncher.run(job, jobParameters);
System.out.println("Batch job executed successfully\n");
} catch (Exception e) {
// Handle any exceptions that occur during job execution
System.err.println("Error executing batch job: " + e.getMessage());
}
}
}
Step 5: Running the Application
After completing all the setup, then run the spring boot application and then the csv file will be generated in the specific project location as shown below:

As we can see in the above attachment, after running the spring boot application the one CSV file is generated named employee.csv in the location src/main/resource and in the csv file the data is shown below.

So, this is the generated CSV data. And below are the logs during the execution of the job.
.png)
And, here is the data base collection image:

Similar Reads
Advanced Java Tutorial | Mastery in Java Programming Advanced Java typically refers to the specialized topics and advanced features of the Java programming language beyond the basics covered in Core Java. Includes concepts like Servlets, JSP (JavaServer Pages), JDBC (Java Database Connectivity), Java EE (Enterprise Edition), web services, frameworks l
13 min read
Java Enterprise Edition
Introduction to Java ServletsJava Servlet is a Java program that runs on a Java-enabled web server or application server. It handles client requests, processes them and generates responses dynamically. Servlets are the backbone of many server-side Java applications due to their efficiency and scalability.Key Features:Servlets w
7 min read
Life Cycle of a ServletThe entire life cycle of a Servlet is managed by the Servlet container, which uses the jakarta.servlet.Servlet interface to understand the Servlet object and manage it. So, before creating a Servlet object, let's first understand the life cycle of the Servlet object, which is actually understanding
6 min read
Introduction to JSPJavaServer Pages (JSP) is a server-side technology that creates dynamic web applications. It allows developers to embed Java code directly into HTML pages and it makes web development more efficient.JSP is an advanced version of Servlets. It provides enhanced capabilities for building scalable and p
4 min read
JSP ArchitectureJSP (Java Server Pages) uses a three-tier architecture with a client, web server, and database. When the client sends a request, the web server's JSP engine processes the JSP file by converting it into a servlet, compiling, and executing it. The generated HTML is sent back to the client. The followi
2 min read
JSF | Java Server FacesJSF technology includes a set of APIs, which represent different UI components and helps in managing their states. These APIs further help in handling events on the UI components and validate user inputs through the UI components. JSF framework provides the flexibility of creating simple as well as
4 min read
Enterprise Java Beans (EJB)Note java.beans: This package has been deprecated in Java 9 and later versions, in favor of using annotations and other modern ways of creating beans. Enterprise Java Beans (EJB) is one of the several Java APIs for standard manufacture of enterprise software. EJB is a server-side software element th
4 min read
Multithreading
Concurrency
java.util.concurrent PackageJava Concurrency package covers concurrency, multithreading, and parallelism on the Java platform. Concurrency is the ability to run several or multi programs or applications in parallel. The backbone of Java concurrency is threads (a lightweight process, which has its own files and stacks and can a
9 min read
Java.util.concurrent.Executor interface with ExamplesThe concurrent API in Java provides a feature known as an executor that initiates and controls the execution of threads. As such, an executor offers an alternative to managing threads using the thread class. At the core of an executor is the Executor interface. It refers to the objects that execute
1 min read
Java.util.concurrent.ExecutorService Interface with ExamplesThe ExecutorService interface extends Executor by adding methods that help manage and control the execution of threads. It is defined in java.util.concurrent package. It defines methods that execute the threads that return results, a set of threads that determine the shutdown status. The ExecutorSer
3 min read
Java Runnable Interfacejava.lang.Runnable is an interface that is to be implemented by a class whose instances are intended to be executed by a thread. There are two ways to start a new Thread - Subclass Thread and implement Runnable. There is no need to subclass a Thread when a task can be done by overriding only the run
3 min read
Callable and Future in JavaIn Java, multithreading allows tasks to run concurrently, improving performance and responsiveness. Traditionally, developers used the Runnable interface to define tasks, but it has two major limitations: it cannot return a result and cannot throw checked exceptions.To overcome these, Java introduce
2 min read
Difference Between Callable and Runnable in Javajava.lang.Runnable is an interface that is to be implemented by a class whose instances are intended to be executed by a thread. There are two ways to start a new Thread â Subclass Thread and implement Runnable. There is no need of sub-classing Thread when a task can be done by overriding only run()
3 min read
JDBC (Java Database Connectivity)
JDBC (Java Database Connectivity)JDBC is an API that helps applications to communicate with databases, it allows Java programs to connect to a database, run queries, retrieve, and manipulate data. Because of JDBC, Java applications can easily work with different relational databases like MySQL, Oracle, PostgreSQL, and more.JDBC Arc
5 min read
JDBC DriversDBC drivers are software components that enable Java applications to communicate with different types of databases. Each database (like MySQL, Oracle, or PostgreSQL) requires a specific JDBC driver that translates Java JDBC calls into the database-specific protocol.The JDBC classes are contained in
4 min read
Establishing JDBC Connection in JavaBefore performing any database operations, you first need to establish a connection using JDBC. This connection acts like a communication channel through which SQL queries are sent and results are received. Setting up this connection involves loading the database driver, specifying the database URL,
6 min read
Types of Statements in JDBCIn Java, the Statement interface in JDBC (Java Database Connectivity) is used to create and execute SQL queries in Java applications. JDBC provides three types of statements to interact with the database:StatementPrepared StatementCallable Statement1. StatementA Statement object is used for general-
5 min read
Java Frameworks
Introduction to Spring FrameworkThe Spring Framework is a lightweight Java framework widely used for building scalable, maintainable enterprise applications. It offers a comprehensive programming and configuration model for Java-based development.Benefits of Using Spring FrameworkSimplified Development: Spring reduces boilerplate
7 min read
Spring - Understanding Inversion of Control with ExampleSpring IoC (Inversion of Control) Container is the core of the Spring Framework. It creates and manages objects (beans), injects dependencies and manages their life cycles. It uses Dependency Injection (DI), based on configurations from XML files, Java-based configuration, annotations or POJOs. Sinc
6 min read
Introduction to Spring BootSpring is one of the most popular frameworks for building enterprise applications, but traditional Spring projects require heavy XML configuration, making them complex for beginners.Spring Boot solves this problem by providing a ready-to-use, production-grade framework on top of Spring. It eliminate
4 min read
Spring - MVC FrameworkThe Spring MVC Framework follows the Model-View-Controller architectural design pattern, which works around the Front Controller, i.e., the Dispatcher Servlet. The Dispatcher Servlet handles and dispatches all incoming HTTP requests to the appropriate controller. It uses @Controller and @RequestMapp
4 min read
How to Create a REST API using Java Spring Boot?Representational State Transfer (REST) is a software architectural style that defines a set of constraints for creating web services. RESTful web services allow systems to access and manipulate web resources through a uniform and predefined set of stateless operations. Unlike SOAP, which exposes its
4 min read
What is Spring Data JPA?Spring Data JPA is a powerful framework that simplifies database access in Spring Boot applications by providing an abstraction layer over the Java Persistence API (JPA). It enables seamless integration with relational databases using Object-Relational Mapping (ORM), eliminating the need for boilerp
6 min read
Spring - JDBC TemplateIn this article, we will discuss the Spring JDBC Template and how to configure the JDBC Template to execute queries. Spring JDBC Template provides a fluent API that improves code simplicity and readability, and the JDBC Template is used to connect to the database and execute SQL Queries. What is JDB
7 min read
Spring Hibernate Configuration and Create a Table in DatabaseSpring Boot and Hibernate are a powerful combination for building scalable and efficient database-driven applications. Spring Boot simplifies application development by reducing boilerplate code, while Hibernate, a popular ORM (Object-Relational Mapping) framework, enables easy database interactions
4 min read
Aspect Oriented Programming (AOP) in Spring FrameworkSpring AOP (Aspect-Oriented Programming) is a programming technique in the Spring Framework that helps separate cross-cutting concerns (like logging, security, transactions) from the main business logic. Instead of adding this logic inside every class, AOP allows you to write it once and apply it wh
3 min read
Introduction to Spring Security and its FeaturesSpring Security is a powerful authentication and authorization framework used to secure Java-based web applications. It easily integrates with Spring Boot and provides advanced security mechanisms such as OAuth2, JWT-based authentication, role-based access control, and protection against common vuln
3 min read
What is Spring Cloud?There are many reasons to use Spring Framework for example if you want faster development, less configuration, auto-configuration, embedded server, production-ready application, and many more. But apart from that most importantly we have ready-made support for microservices and this ready-made suppo
2 min read
Introduction and Working of Struts Web FrameworkStruts is an open-source web application framework developed by Apache Software Foundation, it is used to create a web application based on servlet and JSP. It depends on the MVC (Model View Controller) framework. Struts are thoroughly useful in building J2EE (Java 2 Platform, Enterprise Edition) ap
3 min read
JUnit
Introduction to JUnit 5JUnit is a Testing Framework. The Junit 5 is the latest version of the testing framework, and it has a lot of features when compared with Junit 4. JUnit 5, also known as JUnit Jupiter. It introduces several new features and improvements over its predecessor, JUnit 4, making it more powerful and flex
8 min read
JUnit 5 vs JUnit 4JUnit 4 and JUnit 5 both are Java-based testing frameworks, The JUnit 5 is the advanced testing framework when compared with JUnit 4. The JUnit provides a lot of features like Annotation-based coding, parallel test execution, and other features. Difference between JUnit 5 and JUnit 4TopicJUnit 5JUni
4 min read
How to Write Test Cases in Java Application using Mockito and Junit?Mockito is an open-source testing framework used for unit testing of Java applications. It plays a vital role in developing testable applications. Mockito is used to mock interfaces so that a dummy functionality can be added to a mock interface that can be used in Unit Testing. Unit Testing is a typ
4 min read
Unit Testing in Spring Boot Project using Mockito and JunitSpring Boot is a Java-based framework built on top of Spring that simplifies application development with minimal configuration. Itâs ideal for creating production-ready applications quickly, thanks to features like embedded servers, auto-configuration and reduced boilerplate code.Mockito is an open
4 min read
JUnit 5 - Test Suites with ExampleJUnit 5 encourages a modular approach to test creation with its test suites. These suites function as containers, bundling multiple test classes for easier management and collective execution within a single run. In this article, we will learn about JUnit 5 - Test Suites. Test SuiteIn JUnit 5, a tes
2 min read
JUnit 5 â JaCoCo Code CoverageIn simple terms, code coverage means measuring the percentage of lines of code that are executed during automated tests. For example, if you have a method containing 100 lines of code and you are writing a test case for it, code coverage tells you briefly how many of those lines were actively exerci
5 min read