- Spring Batch Tutorial
Spring Batch Tutorial: Reading Information From a File. When we start writing a batch job, our first step is to provide input data for our. The next part of this tutorial describes how we can read information from a database. Aug 03, 2014 In this post we will learn about how to use Spring Batch to read an XML file using StaxEventItemReader and write to MySQL Database using JdbcBatchItemWriter.We will also witness the usage of JobExecutionListener and itemProcessor.Let’s get going.
- Spring Batch Useful Resources
- Selected Reading
In this chapter, we will create a Spring Batch application which uses an MySQL Reader and a Flatfile Writer (.txt ).
Reader − The Reader we are using in the application is JdbcCursorItemReader to read data from MySQL database.
Assume we have created a table in the MySQL database as shown below.
Assume we have inserted the following records into it.
Writer − The Writer we are using in the application is FlatFileItemWriter to write the data to flatfile (.txt).
Processor − The Processor we are using in the application is a custom processor which just prints the records read from the CSV file.
jobConfig.xml
Following is the configuration file of our sample Spring Batch application. In this file, we will define the Job and the Steps. In addition to these, we also define the beans for ItemReader, ItemProcessor, and ItemWriter. (Here, we associate them with respective classes and pass the values for the required properties to configure them.)
Context.xml
Following is the context.xml of our Spring Batch application. In this file, we will define the beans like job repository, job launcher, and transaction manager.
CustomItemProcessor.java
Following is the Processor class. In this class, we write the code of processing in the application. Here, we are printing the contents of each record.
TutorialRowMapper.java
Following is the TutorialRowMapper class which sets the data to the Tutorial class.
Tutorial.java
Following is the Tutorial class. It is a simple Java class with setter and getter methods. In this class, we are using annotations to associate the methods of this class with the tags of the XML file.
App.java
Following is the code which launces the batch process. In this class, we will launch the Batch Application by running the JobLauncher.
![Spring batch read from database and write to xml file Spring batch read from database and write to xml file](/uploads/1/2/6/3/126347980/168975248.jpg)
On executing this application, it will produce the following output.
This will generate a .txt file with the following contents.
In this tutorial, we are taking a look at a student's question from my Spring Boot Introduction Course.
How can I read in a JSON file in Spring Boot and save the records to a database?
If you have ever had to read and write JSON data you know that this can be tricky. First, you have to read in the JSON data and mapping that data to your domain model isn't always easy. Once you have that in place you need to take that data and save it off to a database. In this tutorial, we will look at a quick and easy way to do this in Spring Boot.
Spring Boot Application
The first thing we need to do is to create a new Spring Boot application using the following dependencies.
JSON Data
The first thing we need to do is to get some dummy JSON data and one of my favorite services for doing this is JSON Placeholder. We are going to grab a list of users and save that to a file called users.json and place it inside of /src/main/resources/json/. Each JSON record for a user will look something like this.
Now that we have our list of users saved we need to model a domain after our user. We could create relationships between each of our domain models but to keep this simple I am going to store all of this data in a single user table. To accomplish this I am going to have embedded and embeddable domain models.
Spring Boot REST Application
Now that we have our domain model in place we are going to build out a REST controller that uses a service & Repository to list and save data.
The important thing about our service here is that it takes a list of users and calls our repository to save them all at once.
Read & Write JSON Data to Database
With our application in place, there is only one step left to do. To read the JSON and write it to a database we are going to use a command line runner. When we bring in the Web dependency we also get the jackson-databind dependency. This contains an Object Mapper class which allows us to easily map JSON data to our domain model.
Using that Object Mapper and our well-crafted domain model from above we have what we need to accomplish our goal. We are going to read in our users.json file and then map that data to our domain model.
If we run our application and look at the H2 Database console we can see the 10 records have been inserted.
Screencast
Conclusion
If you want to check out the source code for this project you can do so here.
![Spring boot batch read from database and write to file Spring boot batch read from database and write to file](/uploads/1/2/6/3/126347980/295959082.png)
Question: Are you facing any issues with JSON in your applications?