Wednesday 2 December 2020

Spring Batch: Read data from multiple resources

In this post, I am going to show how to read data from multiple xml files.

 

We are going to read employees data from xml files that are in below format.

<employees>
	<employee>
		<id>1</id>
		<firstName>Ram</firstName>
		<lastName>Gurram</lastName>
	</employee>
	<employee>
		<id>2</id>
		<firstName>Sailaja</firstName>
		<lastName>Dokku</lastName>
	</employee>
</employees>

 

Step 1: Define a model class that maps to employee details.

 

public class Employee implements ResourceAware{
	private int id;
	private String firstName;
	private String lastName;
	
	private Resource resource;

	....
	....
}


As you see above snippet, Employee class implements ResourceAware interface, it gives access to the resource from where the employee details read from.

 

Step 2: Keep all the xml files (emps1.xml, emps2.xml….) in src/test/resources/xml folder.

 

Step 3: Inject all the resource file details using @Value annotation.

@Value("classpath*:/xml/emps*xml")

public Resource[] inputFiles;

 

Step 4: Define an instance of ‘StaxEventItemReader’ that reads xml content and map to Employee instance.

@Bean
public StaxEventItemReader<Employee> reader() {

	StaxEventItemReader<Employee> staxEventItemReader = new StaxEventItemReader<>();

	Map<String, Class> aliases = new HashMap<>();
	aliases.put("employee", Employee.class);

	XStreamMarshaller unMarshaller = new XStreamMarshaller();
	unMarshaller.setAliases(aliases);

	staxEventItemReader.setFragmentRootElementName("employee");
	staxEventItemReader.setUnmarshaller(unMarshaller);

	return staxEventItemReader;
}


Step 5: Define instance of MultiResourceItemReader.

@Bean
public MultiResourceItemReader<Employee> multiResourceItemReader() {
	MultiResourceItemReader<Employee> multiResourceItemReader = new MultiResourceItemReader<>();

	multiResourceItemReader.setDelegate(reader());
	multiResourceItemReader.setResources(inputFiles);
	return multiResourceItemReader;
}


As you see above snippet, we set all the resource files to MultiResourceItemReader and delegate the requests to reader.

 

Find the below working application.

 

Step 1: Create new maven project ‘read-data-from-mulitple-resources’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>read-data-from-mulitple-resources</artifactId>
	<version>1</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>

		<!-- https://mvnrepository.com/artifact/org.springframework.batch/spring-batch-core -->
		<dependency>
			<groupId>org.springframework.batch</groupId>
			<artifactId>spring-batch-core</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
		</dependency>

		<dependency>
			<groupId>com.thoughtworks.xstream</groupId>
			<artifactId>xstream</artifactId>
			<version>1.4.11.1</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.springframework/spring-oxm -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
		</dependency>

	</dependencies>
</project>


Step 3: Create application.properties file under src/main/resources folder.

 

application.properties

logging.level.root=ERROR
logging.level.org.hibernate=ERROR

## H2 specific properties
spring.h2.console.enabled=true
spring.h2.console.path=/h2

spring.datasource.url=jdbc:h2:file:~/db/myOrg.db;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;

spring.datasource.username=krishna
spring.datasource.password=password123

spring.datasource.driverClassName=org.h2.Driver

## JPA specific properties
# Creates the schema, destroying previous data.
spring.jpa.hibernate.ddl-auto=create-drop

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

spring.jpa.show-sql=false
spring.jpa.properties.hibernate.format_sql=false

## Database connection pooling properties
# Number of ms to wait before throwing an exception if no connection is available.
spring.datasource.max-wait=10000

# Maximum number of active connections that can be allocated from this pool at the same time.
spring.datasource.tomcat.max-active=10
spring.datasource.tomcat.max-idle=5
spring.datasource.tomcat.min-idle=3


Step 3: Create emps1.xml, emps2.xml and emps3.xml files under src/main/resources/xml folder.

 

emps1.xml

<employees>
	<employee>
		<id>1</id>
		<firstName>Ram</firstName>
		<lastName>Gurram</lastName>
	</employee>
	<employee>
		<id>2</id>
		<firstName>Sailaja</firstName>
		<lastName>Dokku</lastName>
	</employee>
</employees>


emps2.xml

<employees>
	<employee>
		<id>3</id>
		<firstName>Harika</firstName>
		<lastName>Raghuram</lastName>
	</employee>
	<employee>
		<id>4</id>
		<firstName>Gopi</firstName>
		<lastName>Battu</lastName>
	</employee>
	<employee>
		<id>5</id>
		<firstName>Siva</firstName>
		<lastName>Prathipati</lastName>
	</employee>
</employees>


emps3.xml

<employees>
	<employee>
		<id>6</id>
		<firstName>Sharief</firstName>
		<lastName>Khan</lastName>
	</employee>
	<employee>
		<id>7</id>
		<firstName>Joel</firstName>
		<lastName>Chelli</lastName>
	</employee>
</employees>


Step 4: Create a package ‘com.sample.app.model’ and define Employee class.

 

Employee.java

package com.sample.app.model;

import org.springframework.batch.item.ResourceAware;
import org.springframework.core.io.Resource;

public class Employee implements ResourceAware{
	private int id;
	private String firstName;
	private String lastName;
	
	private Resource resource;

	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;
	}

	@Override
	public void setResource(Resource resource) {
		this.resource = resource;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Employee [id=");
		builder.append(id);
		builder.append(", firstName=");
		builder.append(firstName);
		builder.append(", lastName=");
		builder.append(lastName);
		builder.append(", resource=");
		builder.append(resource.getFilename());
		builder.append("]");
		return builder.toString();
	}
	
	

}


Step 5: Create a package ‘com.sample.app.configuration’ and define JobConfiguration.

 

JobConfiguration.java

package com.sample.app.configuration;

import java.util.HashMap;
import java.util.Map;

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.core.repository.JobRepository;
import org.springframework.batch.item.file.MultiResourceItemReader;
import org.springframework.batch.item.xml.StaxEventItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.oxm.xstream.XStreamMarshaller;
import org.springframework.transaction.PlatformTransactionManager;

import com.sample.app.model.Employee;

@Configuration
@EnableBatchProcessing
public class JobConfiguration {
	@Autowired
	private JobBuilderFactory jobBuilderFactory;

	@Autowired
	private StepBuilderFactory stepBuilderFactory;

	@Value("classpath*:/xml/emps*xml")
	public Resource[] inputFiles;

	@Bean
	public MultiResourceItemReader<Employee> multiResourceItemReader() {
		MultiResourceItemReader<Employee> multiResourceItemReader = new MultiResourceItemReader<>();

		multiResourceItemReader.setDelegate(reader());
		multiResourceItemReader.setResources(inputFiles);
		return multiResourceItemReader;
	}

	@Bean
	public StaxEventItemReader<Employee> reader() {

		StaxEventItemReader<Employee> staxEventItemReader = new StaxEventItemReader<>();

		Map<String, Class> aliases = new HashMap<>();
		aliases.put("employee", Employee.class);

		XStreamMarshaller unMarshaller = new XStreamMarshaller();
		unMarshaller.setAliases(aliases);

		staxEventItemReader.setFragmentRootElementName("employee");
		staxEventItemReader.setUnmarshaller(unMarshaller);

		return staxEventItemReader;
	}

	@Bean
	public Step step1() {
		return this.stepBuilderFactory.get("step1").chunk(5).reader(multiResourceItemReader()).writer(emps -> {
			for (Object emp : emps) {
				System.out.println(emp);
			}
		}).build();
	}

	@Bean
	public Job myJob(JobRepository jobRepository, PlatformTransactionManager platformTransactionManager) {

		return jobBuilderFactory.get("My-First-Job").start(step1()).build();
	}

}


Step 6: Define App.java

 

App.java

package com.sample.app;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableBatchProcessing
@SpringBootApplication
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}


Total project structure looks like below.







Run App.java, you will see below messages in console.

Employee [id=1, firstName=Ram, lastName=Gurram, resource=emps1.xml]
Employee [id=2, firstName=Sailaja, lastName=Dokku, resource=emps1.xml]
Employee [id=3, firstName=Harika, lastName=Raghuram, resource=emps2.xml]
Employee [id=4, firstName=Gopi, lastName=Battu, resource=emps2.xml]
Employee [id=5, firstName=Siva, lastName=Prathipati, resource=emps2.xml]
Employee [id=6, firstName=Sharief, lastName=Khan, resource=emps3.xml]
Employee [id=7, firstName=Joel, lastName=Chelli, resource=emps3.xml]


You can download complete working application from this link.

https://github.com/harikrishna553/springboot/tree/master/batch/read-data-from-mulitple-resources









Previous                                                    Next                                                    Home

No comments:

Post a Comment