Last active
May 24, 2018 20:58
-
-
Save fmbenhassine/8c1c87894e705ce916a1624f64fa8018 to your computer and use it in GitHub Desktop.
Easy Batch implementation of https://spring.io/guides/gs/batch-processing/ #EasyBatch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.*; | |
public class DatabaseUtil { | |
private static final String DATABASE_URL = "jdbc:hsqldb:mem"; | |
private static final String USER = "sa"; | |
private static final String PASSWORD = "pwd"; | |
public static Connection getConnection() throws SQLException { | |
return DriverManager.getConnection(DATABASE_URL, USER, PASSWORD); | |
} | |
static void startEmbeddedDatabase() throws Exception { | |
//do not let hsqldb reconfigure java.util.logging used by easy batch | |
System.setProperty("hsqldb.reconfig_logging", "false"); | |
createPeopleTable(); | |
} | |
public static void createPeopleTable() throws Exception { | |
Connection connection = getConnection(); | |
Statement statement = connection.createStatement(); | |
String query = "DROP TABLE IF EXISTS people"; | |
statement.executeUpdate(query); | |
query = "CREATE TABLE people (\n" + | |
" id BIGINT IDENTITY NOT NULL PRIMARY KEY,\n" + | |
" firstName VARCHAR(20),\n" + | |
" lastName VARCHAR(20)\n" + | |
");"; | |
statement.executeUpdate(query); | |
statement.close(); | |
connection.close(); | |
} | |
public static void dumpPeopleTable() throws Exception { | |
System.out.println("Loading people from the database..."); | |
Connection connection = getConnection(); | |
Statement statement = connection.createStatement(); | |
ResultSet resultSet = statement.executeQuery("select * from people"); | |
while (resultSet.next()) { | |
System.out.println( | |
"Person : firstName= " + resultSet.getString("firstName") + " | " + | |
"lastName= " + resultSet.getString("lastName") | |
); | |
} | |
resultSet.close(); | |
statement.close(); | |
connection.close(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.easybatch.core.job.Job; | |
import org.easybatch.core.job.JobExecutor; | |
import org.easybatch.flatfile.DelimitedRecordMapper; | |
import org.easybatch.flatfile.FlatFileRecordReader; | |
import org.easybatch.jdbc.BeanPropertiesPreparedStatementProvider; | |
import org.easybatch.jdbc.JdbcRecordWriter; | |
import java.sql.Connection; | |
import static org.easybatch.core.job.JobBuilder.aNewJob; | |
public class Main { | |
public static void main(String[] args) throws Exception { | |
String[] fields = new String[]{"firstName", "lastName"}; | |
DatabaseUtil.startEmbeddedDatabase(); | |
Connection connection = DatabaseUtil.getConnection(); | |
String query = "INSERT INTO people (firstName, lastName) VALUES (?, ?)"; | |
Job job = aNewJob() | |
.named("importUserJob") | |
.reader(new FlatFileRecordReader("src/main/resources/sample-data.csv")) | |
.mapper(new DelimitedRecordMapper(Person.class, fields)) | |
.processor(new PersonProcessor()) | |
.writer(new JdbcRecordWriter(connection, query, new BeanPropertiesPreparedStatementProvider(Person.class, fields))) | |
.build(); | |
JobExecutor.execute(job); | |
DatabaseUtil.dumpPeopleTable(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Person { | |
private String lastName; | |
private String firstName; | |
public Person() { | |
} | |
public Person(String firstName, String lastName) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
public String getFirstName() { | |
return firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
@Override | |
public String toString() { | |
return "firstName: " + firstName + ", lastName: " + lastName; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.easybatch.core.processor.RecordProcessingException; | |
import org.easybatch.core.processor.RecordProcessor; | |
import org.easybatch.core.record.GenericRecord; | |
import org.easybatch.core.record.Record; | |
public class PersonProcessor implements RecordProcessor<Record<Person>, Record<Person>> { | |
public Record<Person> processRecord(Record<Person> record) throws RecordProcessingException { | |
Person person = record.getPayload(); | |
final String firstName = person.getFirstName().toUpperCase(); | |
final String lastName = person.getLastName().toUpperCase(); | |
final Person transformedPerson = new Person(firstName, lastName); | |
System.out.println("Converting (" + person + ") into (" + transformedPerson + ")"); | |
return new GenericRecord<Person>(record.getHeader(), transformedPerson); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<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>test</groupId> | |
<artifactId>test-eb</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>org.easybatch</groupId> | |
<artifactId>easybatch-flatfile</artifactId> | |
<version>4.2.0-SNAPSHOT</version> | |
</dependency> | |
<dependency> | |
<groupId>org.easybatch</groupId> | |
<artifactId>easybatch-jdbc</artifactId> | |
<version>4.2.0-SNAPSHOT</version> | |
</dependency> | |
<dependency> | |
<groupId>org.hsqldb</groupId> | |
<artifactId>hsqldb</artifactId> | |
<version>2.3.4</version> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Jill | Doe | |
---|---|---|
Joe | Doe | |
Justin | Doe | |
Jane | Doe | |
John | Doe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment