1. Data Warehouse basic concept
A data warehouse is a system that aggregates data from different sources into a single, central, consistent data store to support data analysis, data mining, artificial intelligence (AI), and machine learning.

Imagine an E-commerce platform such as Amazon, which can perform approximately one million transactions a day or 30 million transactions a month. As a business operator, you may need to know how many items of each product have been sold in a month. We will quickly think about using SQL queries to implement this report something like:
select p.name, count(1)
from transaction t
join product p on t.product_id = p.id
where t.month = 'April'
group by t.product_id;
But with the increase in the data size and individuals using the data to perform various analyses, this query becomes extremely slow in query processing even if we create an index for the “transaction.month“ column.
Additionally, the result of this query is historical data that will not be changed or very very rarely changed (unless there was a mistake when creating a transaction, hence we have to update it). Consequently, why do we need to execute the above query every time the business operator requests the report? Instead of doing this, we can execute this query once and store the result in a data source named data warehouse, this technique is ETL.
2. ETL introduction
ETL stands for Extract, Transform, Load. It is a data integration process that involves retrieving data from multiple sources, transforming that data to fit the target system’s requirements, and loading the data into a data warehouse.

The goal of ETL is to consolidate data from different sources into a single, central location for analysis and reporting. This process typically involves the use of ETL tools that can automate the extraction, transformation, and loading of data, and ensure the accuracy and consistency of the final result. ETL is crucial in data warehousing, business intelligence, and analytics.
Some popular Python ETL frameworks are Apache Airflow, Apache Beam, Bonobo, Luigi, Bubbles, PySpark, and Spring Batch. These frameworks provide a way to build workflows and pipelines that move and transform data, while also providing monitoring, scheduling, and error-handling capabilities.
3. Implementation
We will create a sample project (which can be found over on Github) to demonstrate how ETL works with Spring Batch. Spring Batch is designed specifically for automating and simplifying batch processing and can handle data processing, mapping, conversion, and aggregation. It can work with various data sources, including databases, flat files, and XML files, and provides built-in support for parallel and distributed processing. Additionally, Spring Batch provides monitoring, and scheduling to make ETL processes more robust and maintainable.
This project is about demonstrating a basic concept of ETL so I try to reduce the complexity as much as possible. In reality, the ETL process is more complicated for example we may need a staging database – an intermediate storage area used for data processing during ETL or extracting data from multiple databases (even from 3rd systems via API calls).
3.1. Prerequisites
- Docker
- Java 11
- Gradle
- Spring boot
- Flyway
- Spring batch
3.2. Database design
In this same project, we will create two very simple databases: nht_transactional – a transaction database storing product transactions, and nht_datawarehouse a warehouse storing ETL results:
- nht_transaction.product: store product data with 18 sample records
- nht_transaction.transaction: store transaction of products with 1000 sample records
- nht_warehouse.fact_transaction_product_monthly: store the total quantity of each product monthly which will be used to generate the report.

Flyway will be used to create these tables and sample data when starting the Spring boot application.
3.3. ETL process
We use a Rest API to trigger the ETL process for a specific month for example:
curl --location --request POST 'localhost:8080/etl?year=2022&month=4'
The ETL process requires the year and month parameters inputted from the above Rest API and includes three steps: extract, transform, and load. Each step is implemented as a corresponding service in src.main.service.

3.3.1. Extract
This stage extracts all products that need to be reported by extending org.springframework.batch.item.data.RepositoryItemReader and use the “ProductRepository” with “findAll” supported by Spring Data.
@Component
@Slf4j
public class TransactionProductMonthlyExtractor extends RepositoryItemReader<Product> {
public TransactionProductMonthlyExtractor(@Autowired ProductRepository productRepository) {
this.setRepository(productRepository);
this.setMethodName("findAll");
Map<String, Direction> sorts = new HashMap<>();
sorts.put("id", Direction.ASC);
this.setSort(sorts);
}
@Override
protected Product doRead() throws Exception {
log.info("TransactionProductMonthlyExtractor::doRead");
return super.doRead();
}
}
3.3.2. Transform
In this stage, the product data is transformed and consolidated for creating fact_transaction_product_monthly. We get “year” and “month” from the ETL parameters and then load the total quantity by month of each product via a native SQL query.
public interface TransactionRepository extends JpaRepository<Transaction, BigInteger> {
@Query(nativeQuery = true, value = "SELECT COALESCE(SUM(t.quantity),0) " +
"FROM transaction t " +
"WHERE t.product_id = :productId AND YEAR(t.created_date) = :year AND MONTH(t.created_date) = :month")
Integer getTotalQuantity(@Param("productId") Integer productId, @Param("year") Integer year, @Param("month") Integer month);
}
public class TransactionProductMonthlyTransformer implements ItemProcessor<Product, FactTransactionProductMonthly> {
@Value("#{jobParameters['year']}")
private int year;
@Value("#{jobParameters['month']}")
private int month;
private final TransactionRepository transactionRepository;
@Override
public FactTransactionProductMonthly process(Product product) {
log.info("TransactionProductMonthlyTransformer::process");
return FactTransactionProductMonthly.builder()
.productId(product.getId())
.productName(product.getName())
.month(month)
.year(year)
.totalQuantity(transactionRepository.getTotalQuantity(product.getId(), year, month))
.build();
}
}
3.3.3. Load
Finally, we will move the transformed data into the data warehouse.
public class TransactionProductMonthlyLoader implements ItemWriter<FactTransactionProductMonthly> {
private final FactTransactionProductMonthlyRepository factTransactionProductMonthlyRepository;
@Override
public void write(List<? extends FactTransactionProductMonthly> items) throws Exception {
log.info("TransactionProductMonthlyLoader::write");
factTransactionProductMonthlyRepository.saveAll(items);
}
}
3.3.4. ETL configuration
The @EnableBatchProcessing annotation enables Spring Batch features and provides a base configuration for setting up batch jobs with the defined ETL services above.
@Configuration
@EnableBatchProcessing
public class BatchConfig {
@Value("${spring.batch.chunk-size}")
private Integer chunkSize;
@Bean("transactionProductMonthlyJob")
public Job transactionProductMonthlyJob(JobBuilderFactory jobBuilderFactory, Step transactionProductMonthlySteps,
TransactionProductCompletionListener transactionProductCompletionListener) {
return jobBuilderFactory
.get("transactionProductMonthlyJob")
.incrementer(new RunIdIncrementer())
.listener(transactionProductCompletionListener)
.flow(transactionProductMonthlySteps)
.end()
.build();
}
@Bean("transactionProductMonthlySteps")
public Step transactionProductMonthlySteps(StepBuilderFactory stepBuilderFactory, TransactionProductMonthlyExtractor reader,
TransactionProductMonthlyTransformer transformer, TransactionProductMonthlyLoader loader) {
return stepBuilderFactory
.get("transactionProductMonthlySteps").<Product, FactTransactionProductMonthly>chunk(chunkSize)
.reader(reader)
.processor(transformer)
.writer(loader)
.build();
}
}
public class TransactionProductMonthlyLoader implements ItemWriter<FactTransactionProductMonthly> {
private final FactTransactionProductMonthlyRepository factTransactionProductMonthlyRepository;
@Override
public void write(List<? extends FactTransactionProductMonthly> items) throws Exception {
log.info("TransactionProductMonthlyLoader::write");
factTransactionProductMonthlyRepository.saveAll(items);
}
}
Since we are using Spring batch with chunk-oriented processing approach (the other approach is tasklet-based). This approach reads the data sequentially and creates “chunks” that will be written out within a transaction boundary.
Each individual product is read by ItemReader, then handed to an ItemProcessor, and aggregated. Once the number of items read equals the commit interval, the entire chunk is written out via ItemWriter, and finally, the transaction is committed. A chunk-oriented step can be configured via spring.batch.chunk-size, the process of each chunk is as below.

You can refer to Spring official document to understand more about Spring Batch configuration.
4. Conclusion
In this article, we’ve seen a basic example of an ETL data pipeline using Spring Batch.
Most noteworthy, we saw the configurations of an application starter, created an ETL stream pipeline using the Spring Batch, and implemented services to get data from MySQL for our reading, transforming, and writing data to the warehouse. The example code can be found on GitHub.