Spring-batch-mysql-to-flat-file

提供:Dev Guides
移動先:案内検索

Spring Batch-MySQLからフラットファイル

この章では、MySQL Readerと Flatfile Writer(.txt)を使用するSpring Batchアプリケーションを作成します。

*Reader* -アプリケーションで使用しているReaderは、MySQLデータベースからデータを読み取る *JdbcCursorItemReader* です。

以下に示すように、MySQLデータベースにテーブルを作成したと仮定します。

CREATE TABLE details.xml_mysql(
   person_id int(10) NOT NULL,
   sales VARCHAR(20),
   qty int(3),
   staffName VARCHAR(20),
   date VARCHAR(20)
);

次のレコードを挿入したとします。

mysql> select * from tutorialsdata;
+-------------+-----------------+----------------+-----------------+
| tutorial_id | tutorial_author | tutorial_title | submission_date |
+-------------+-----------------+----------------+-----------------+
|         101 | Sanjay          | Learn Java     | 06-05-2007      |
|         102 | Abdul S         | Learn MySQL    | 19-04-2007      |
|         103 | Krishna Kasyap  | Learn JavaFX   | 06-07-2017      |
+-------------+-----------------+----------------+-----------------+
3 rows in set (0.00 sec)
*Writer* -アプリケーションで使用しているWriterは、 *FlatFileItemWriter* で、データを *flatfile* (.txt)に書き込みます。

プロセッサ-アプリケーションで使用しているプロセッサは、CSVファイルから読み取ったレコードを印刷するだけのカスタムプロセッサです。

jobConfig.xml

サンプルのSpring Batchアプリケーションの設定ファイルは次のとおりです。 このファイルでは、ジョブとステップを定義します。 これらに加えて、ItemReader、ItemProcessor、ItemWriterのBeanも定義します。 (ここでは、それらをそれぞれのクラスに関連付け、必要なプロパティの値を渡して設定します。)

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:batch = "http://www.springframework.org/schema/batch"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xmlns:util = "http://www.springframework.org/schema/util"
   xsi:schemaLocation = "http://www.springframework.org/schema/batch

      http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

   <import resource = "../jobs/context.xml"/>
   <bean id = "tutorial" class = "Tutorial" scope = "prototype"/>
   <bean id = "itemProcessor" class = "CustomItemProcessor"/>

   <batch:job id = "helloWorldJob">
      <batch:step id = "step1">
         <batch:tasklet>
            <batch:chunk reader = "mysqlItemReader"
               writer = "flatFileItemWriter" processor = "itemProcessor"
               commit-interval = "10">
            </batch:chunk>
         </batch:tasklet>
      </batch:step>
   </batch:job>

   <bean id = "mysqlItemReader"
      class = "org.springframework.batch.item.database.JdbcCursorItemReader" >
      <property name = "dataSource" ref = "dataSource"/>
      <property name = "sql" value = "select * from details.tutorialsdata"/>
      <property name = "rowMapper">
         <bean class = "TutorialRowMapper"/>
      </property>
   </bean>

   <bean id = "flatFileItemWriter"
      class = " org.springframework.batch.item.file.FlatFileItemWriter">
      <property name = "resource" value = "file:target/outputfiles/employee_output.txt"/>
      <property name = "lineAggregator">
         <bean class = " org.springframework.batch.item.file.transform.PassThroughLineAggregator"/>
      </property>
   </bean>
</beans>

Context.xml

以下は、Spring Batchアプリケーションの context.xml です。 このファイルでは、ジョブリポジトリ、ジョブランチャー、トランザクションマネージャーなどのBeanを定義します。

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jdbc = "http://www.springframework.org/schema/jdbc"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/jdbc
      http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd ">

   <!-- stored job-meta in database -->
   <bean id = "jobRepository"
      class = "org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
      <property name = "dataSource" ref = "dataSource"/>
      <property name = "transactionManager" ref = "transactionManager"/>
      <property name = "databaseType" value = "mysql"/>
   </bean>

   <bean id = "transactionManager"
      class = "org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>

   <bean id = "dataSource"
      class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name = "driverClassName" value = "com.mysql.jdbc.Driver"/>
      <property name = "url" value = "jdbc:mysql://localhost:3306/details"/>
      <property name = "username" value = "myuser"/>
      <property name = "password" value = "password"/>
   </bean>

   <bean id = "jobLauncher"
      class = "org.springframework.batch.core.launch.support.SimpleJobLauncher">
      <property name = "jobRepository" ref = "jobRepository"/>
   </bean>

   <!-- create job-meta tables automatically -->
   <jdbc:initialize-database data-source = "dataSource">
      <jdbc:script location = "org/springframework/batch/core/schema-drop-mysql.sql"/>
      <jdbc:script location = "org/springframework/batch/core/schema-mysql.sql"/>
   </jdbc:initialize-database>
</beans>

CustomItemProcessor.java

プロセッサクラスは次のとおりです。 このクラスでは、アプリケーションで処理のコードを記述します。 ここでは、各レコードの内容を印刷しています。

import org.springframework.batch.item.ItemProcessor;

//Implementing the ItemProcessor interface
public class CustomItemProcessor implements ItemProcessor<Tutorial, Tutorial> {

   @Override
   public Tutorial process(Tutorial item) throws Exception {
      System.out.println("Processing..." + item);
      return item;
   }
}

TutorialRowMapper.java

以下は、データを Tutorial クラスに設定する TutorialRowMapper クラスです。

public class TutorialRowMapper implements RowMapper<Tutorial> {

   @Override
   public Tutorial mapRow(ResultSet rs, int rowNum) throws SQLException {

      Tutorial tutorial = new Tutorial();

      tutorial.setTutorial_id(rs.getInt("tutorial_id"));
      tutorial.setTutorial_title(rs.getString("tutorial_title"));
      tutorial.setTutorial_author(rs.getString("tutorial_author"));
      tutorial.setSubmission_date(rs.getString("submission_date"));
      return tutorial;
   }
}

Tutorial.java

以下は Tutorial クラスです。 これは、 setter および getter メソッドを持つ単純なJavaクラスです。 このクラスでは、アノテーションを使用して、このクラスのメソッドをXMLファイルのタグに関連付けています。

public class Tutorial {
   private int tutorial_id;
   private String tutorial_title;
   private String tutorial_author;
   private String submission_date;

   public int getTutorial_id() {
      return tutorial_id;
   }

   public void setTutorial_id(int tutorial_id) {
      this.tutorial_id = tutorial_id;
   }

   public String getTutorial_title() {
      return tutorial_title;
   }

   public void setTutorial_title(String tutorial_title) {
      this.tutorial_title = tutorial_title;
   }

   public String getTutorial_author() {
      return tutorial_author;
   }

   public void setTutorial_author(String tutorial_author) {
      this.tutorial_author = tutorial_author;
   }

   public String getSubmission_date() {
      return submission_date;
   }
   public void setSubmission_date(String submission_date) {
      this.submission_date = submission_date;
   }

   @Override
   public String toString() {
      return " [id=" + tutorial_id + ", title=" +
      tutorial_title                      + ",
      author=" + tutorial_author + ", date=" +
      submission_date + "]";
   }
}

App.java

以下は、バッチ処理を実行するコードです。 このクラスでは、JobLauncherを実行してバッチアプリケーションを起動します。

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

   public static void main(String[] args) throws Exception {

      String[] springConfig  =  { "jobs/job_hello_world.xml" };

     //Creating the application context object
      ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);

     //Creating the job launcher
      JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");

     //Creating the job
      Job job = (Job) context.getBean("helloWorldJob");

     //Executing the JOB
      JobExecution execution = jobLauncher.run(job, new JobParameters());
      System.out.println("Exit Status : " + execution.getStatus());
   }
}

このアプリケーションを実行すると、次の出力が生成されます。

May 09, 2017 5:44:48 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXml
ApplicationContext@3d646c37: startup date [Tue May
09 17:44:48 IST 2017]; root of context hierarchy
May 09, 2017 5:44:48 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
May 09, 2017 5:44:56 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run
INFO: Job: [FlowJob: [name=helloWorldJob]] launched
with the following parameters: [{}]
May 09, 2017 5:44:56 PM org.springframework.batch.core.job.SimpleStepHandler handleStep
INFO: Executing step: [step1]
Processing...Report [id=101, title=Learn Java, author=Sanjay, date=06-05-2007]
Processing...Report [id=102, title=Learn MySQL, author=Abdul S, date=19-04-2007]
Processing...Report [id=103, title=Learn JavaFX, author=Krishna Kasyap, date=0607-2017]
May 09, 2017 5:44:57 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run
INFO: Job: [FlowJob: [name=helloWorldJob]] completed with the following parameters:
[{}] and the following status: [COMPLETED]
Hello
Exit Status : COMPLETED

これにより、次の内容の .txt ファイルが生成されます。

Report [id=101, title=Learn Java, author=Sanjay, date=06-05-2007]
Report [id=102, title=Learn MySQL, author=Abdul S, date=19-04-2007]
Report [id=103, title=Learn JavaFX, author=Krishna Kasyap, date=06-07-2017]