春バッチの例 - MySQLデータベースへのCSVファイル

Spring Batchの例– CSVファイルからMySQLデータベース

このチュートリアルでは、CSVファイルからデータベースにデータを読み込むためにSpring Batchジョブを構成する方法を示します。

使用したツールとライブラリ:

  1. メーベン3

  2. Eclipse 4.2

  3. JDK 1.6

  4. Spring Core 3.2.2.RELEASE

  5. Spring Batch 2.2.0.RELEASE

  6. MySQL Javaドライバー5.1.25

1. Javaプロジェクト

Mavenを使用してJavaプロジェクトを作成する

$ mvn archetype:generate -DgroupId=com.example -DartifactId=SpringBatchExample
  -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Eclipseプロジェクトに変換し、Eclipse IDEにインポートします。

$ cd SpringBatchExample/
$ mvn eclipse:eclipse

2. プロジェクトの依存関係

すべてのプロジェクトの依存関係をpom.xmlで宣言します。

pom.xml


    4.0.0
    com.example
    SpringBatchExample
    jar
    1.0-SNAPSHOT
    SpringBatchExample
    http://maven.apache.org

    
        1.6
        3.2.2.RELEASE
        2.2.0.RELEASE
        5.1.25
    

    

        
        
            org.springframework
            spring-core
            ${spring.version}
        

        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        

        
        
            org.springframework.batch
            spring-batch-core
            ${spring.batch.version}
        
        
            org.springframework.batch
            spring-batch-infrastructure
            ${spring.batch.version}
        

        
        
            mysql
            mysql-connector-java
            ${mysql.driver.version}
        

    
    
        spring-batch
        
          
            org.apache.maven.plugins
            maven-eclipse-plugin
            2.9
            
                true
                false
            
          
          
            org.apache.maven.plugins
            maven-compiler-plugin
            2.3.2
            
                ${jdk.version}
                ${jdk.version}
            
          
        
    

3. プロジェクトのディレクトリ構造

最終的なプロジェクト構造を確認します。

spring-batch-csv-database-project-structure

4. CSVファイル

これは、リソースフォルダー内のcsvファイルです。

report.csv

Date,Impressions,Clicks,Earning
6/1/13,"139,237",37,227.21
6/2/13,"149,582",55,234.71
6/3/13,"457,425",132,211.48
6/4/13,"466,870",141,298.40
6/5/13,"472,385",194,281.35
......

5. MySQLデータベース

MySQLデータベースの「dataSource」Beanを定義します。 jdbc:initialize-databaseは、メタデータテーブルを自動的に作成するために使用されます。SpringBatchでは、ジョブの詳細を保存するためにjdbc:initialize-databaseが必要です。

resources/spring/batch/config/database.xml



  
  
    
    
    
    
  

  

  
  
    
    
  

6. Spring Batchコア設定

jobRepositoryおよびjobLauncherを定義します。

resources/spring/batch/config/context.xml



  
  
    
    
    
  

  
  

  
    
  

7. 春のバッチジョブ

これは、Springバッチジョブを構成するためのメインのxmlファイルです。 このjob-report.xmlファイルは、report.csvファイルを読み取り、それをreportプレーンpojoと照合して、データをMySQLデータベースに書き込むジョブを定義します。

コメントを読んでください、それは自明であるべきです。 ところで、「RAW_REPORT」テーブルを手動で作成することを忘れないでください。

resources/spring/batch/jobs/job-report.xml



  

  
    
      
        
        
      
    
  

  

    
    

    
        
          
          
                
                
            
          
          
                 
                 
              
              
                
              
          

          
      

  

  
    
    
      
            
      
    
    
    
        
    
  

com/example/model/Report.java

package com.example.model;

public class Report {

    private String Date;
    private String Impressions;
    private String Clicks;
    private String Earning;

    //getter and setter methods


}

Note
詳細な説明については、このSpring batch referencesを参照してください。

8. それを実行します

すべてをロードし、jobLauncherで実行します。 これは最も簡単に起動してテストする方法ですが、実際には、Springタスク、Quartzなどのスケジューラフレームワーク、または「cron」コマンドなどのシステムスケジューラで起動する必要があります(今後のチュートリアルで紹介します)。

com/example/App.java

package com.example;

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) {

    String[] springConfig  =
        {   "spring/batch/config/database.xml",
            "spring/batch/config/context.xml",
            "spring/batch/jobs/job-report.xml"
        };

    ApplicationContext context =
        new ClassPathXmlApplicationContext(springConfig);

    JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
    Job job = (Job) context.getBean("reportJob");

    try {

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

    } catch (Exception e) {
        e.printStackTrace();
    }

    System.out.println("Done");

  }
}

出力。 Spring Batchメタデータテーブルが作成され、report.cvsのコンテンツがデータベーステーブル「RAW_REPORT」に挿入されます。

spring-batch-cvs-database-data

完了しました。

ソースコードをダウンロード

ダウンロード–SpringBatch-CSV-Database-Example.zip(18 kb)