Spring Batch Classifier Example, easy in 10 minutes

Today, we show you Spring Batch Classifier Example that helps you to classify data to multiple files, even different file types, e.g: XML, JSON, Text. If you work in a big company and enjoy the development of large architecture and certainly you need to share data with multiple systems. This is really needed for you.

Spring Batch Classifier Example

Project Setup

Tools and framework that we use:
Spring Boot 2.2.2 RELEASE
Spring Batch 4.2.1
Maven 3.6
Java 8

Project Directory

Our project will have a structure like below

Project Dependencies

We’re using the dependencies like below

Routing data to multiple files using Classifier

Here, we reuse source code from the post Spring Batch Hello World Example and adapt the part of classifier implementation.

First, we create a simple classifier that routes employees of Java department to XML writer and the rest of the employees will be routed to JSON writer.

Adapting classifier to Spring Batch Job

Here, we need to create XML writer and JSON writer beans then adapt the MyClassifier

Input data

Here, we assume that we need to read employee information from CSV file named employees.csv, its format looks like below:

This file is located in the project folder resources /csv/employees.csv

Running Application

To demo our Spring Batch Classifier Example, we run the below command in the console.

Note: You must be in your project directory before running that command.
While running that command, you will see the output like below

Spring Batch classifier example

Let’s have a look into two output files:

XML output file

JSON output file

That’s all about Spring Batch Classifier Example.

If you find this post useful, don't hesitate to share it with your friends or other people for growing knowledge together and is an effort to contribute us.


Spring Boot Tutorial for Beginners
Spring Batch Frameworks

Download the complete source code

SpringBatch-Classifier-Example.zip (164 downloads)
Please share it if you found this useful
Hide Buttons