📜  Spring Batch-CSV到XML

📅  最后修改于: 2020-11-11 06:53:05             🧑  作者: Mango


在本章中,我们将创建一个使用CSV Reader和XML Writer的简单Spring Batch应用程序。

读取器-我们在应用程序中使用的读取器FlatFileItemReader ,用于从CSV文件读取数据。

以下是我们在此应用程序中使用的输入CSV文件。本文档包含数据记录,这些数据记录指定了详细信息,例如教程ID,教程作者,教程标题,提交日期,教程图标和教程说明。

1001, "Sanjay", "Learn Java", 06/05/2007 
1002, "Abdul S", "Learn MySQL", 19/04/2007 
1003, "Krishna Kasyap", "Learn JavaFX", 06/07/2017

Writer-我们在应用程序中使用的Writer是StaxEventItemWriter,用于将数据写入XML文件。

处理器-我们在应用程序中使用的处理器是一个自定义处理器,它仅打印从CSV文件读取的记录。

jobConfig.xml

以下是我们的示例Spring Batch应用程序的配置文件。在此文件中,我们将定义作业和步骤。除了这些,我们还为ItemReader,ItemProcessor和ItemWriter定义了bean。 (在这里,我们将它们与相应的类相关联,并传递所需属性的值以进行配置。)

  
   
     
   
    
     
   
    
   
       
   
          
             
             
          
       
     
 
     
       
       
          
             
                
                   
                
             
      
             
                
             
          
       
     
   
    
       
       
       
     
 
   
       
          
            Tutorial 
          
       
    
 

Context.xml

以下是我们的Spring Batch应用程序的context.xml 。在此文件中,我们将定义诸如作业存储库,作业启动器和事务管理器之类的Bean。

  
    
    
       
       
       
     
 
     
    
       
     
   
    
       
      
       
       
    
  
    
      
       
       
    

CustomItemProcessor.java

以下是Processor类。在此类中,我们在应用程序中编写处理代码。在这里,我们正在打印每个记录的内容。

import org.springframework.batch.item.ItemProcessor;  

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

TutorialFieldSetMapper.java

以下是TutorialFieldSetMapper类,它将数据设置为Tutorial类。

import org.springframework.batch.item.file.mapping.FieldSetMapper; 
import org.springframework.batch.item.file.transform.FieldSet; 
import org.springframework.validation.BindException;  

public class TutorialFieldSetMapper implements FieldSetMapper {  

   @Override 
   public Tutorial mapFieldSet(FieldSet fieldSet) throws BindException {  
      
      //Instantiating the report object  
      Tutorial tutorial = new Tutorial(); 
       
      //Setting the fields  
      tutorial.setTutorial_id(fieldSet.readInt(0)); 
      tutorial.setTutorial_author(fieldSet.readString(1)); 
      tutorial.setTutorial_title(fieldSet.readString(2)); 
      tutorial.setSubmission_date(fieldSet.readString(3)); 
       
      return tutorial; 
   } 
}

Tutorial.java类

以下是Tutorial类。这是带有settergetter方法的简单Java类。在此类中,我们使用批注将此类的方法与XML文件的标记相关联。

import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement;  

@XmlRootElement(name = "tutorial") 
public class Tutorial {  
   private int tutorial_id; 
   private String tutorial_author; 
   private String tutorial_title;
   private String submission_date;  
 
   @XmlAttribute(name = "tutorial_id") 
   public int getTutorial_id() { 
      return tutorial_id; 
   }  
 
   public void setTutorial_id(int tutorial_id) { 
      this.tutorial_id = tutorial_id; 
   }  
 
   @XmlElement(name = "tutorial_author") 
   public String getTutorial_author() { 
      return tutorial_author; 
   }  
   public void setTutorial_author(String tutorial_author) { 
      this.tutorial_author = tutorial_author; 
   }  
      
   @XmlElement(name = "tutorial_title") 
   public String getTutorial_title() { 
      return tutorial_title; 
   }  
   
   public void setTutorial_title(String tutorial_title) { 
      this.tutorial_title = tutorial_title; 
   }  
   
   @XmlElement(name = "submission_date") 
   public String getSubmission_date() { 
      return submission_date; 
   }  
   
   public void setSubmission_date(String submission_date) { 
      this.submission_date = submission_date; 
   } 
   
   @Override 
   public String toString() { 
      return "  [Tutorial id=" + tutorial_id + ", 
         Tutorial Author=" + tutorial_author  + ", 
         Tutorial Title=" + tutorial_title + ", 
         Submission 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 08, 2017 10:10:12 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 
INFO: Refreshing 
org.springframework.context.support.ClassPathXmlApplicationContext@3d646c37: startup date 
[Mon May 08 10:10:12 IST 2017]; root of context hierarchy 
May 08, 2017 10:10:12 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
May 08, 2017 10:10:15 AM org.springframework.jdbc.datasource.init.ScriptUtils executeSqlScript 
INFO: Executing step: [step1] 
Processing...  [Tutorial id=1001, Tutorial Author=Sanjay, 
Tutorial Title=Learn Java, Submission Date=06/05/2007] 
Processing...  [Tutorial id=1002, Tutorial Author=Abdul S, 
Tutorial Title=Learn MySQL, Submission Date=19/04/2007] 
Processing...  [Tutorial id=1003, Tutorial Author=Krishna Kasyap, 
Tutorial Title=Learn JavaFX, Submission Date=06/07/2017] 
May 08, 2017 10:10:21 AM org.springframework.batch.core.launch.support.SimpleJobLauncher run 
INFO: Job: [FlowJob: [name=helloWorldJob]] completed with the following parameters: 
[{}] and the following status: [COMPLETED] 
Exit Status : COMPLETED

这将生成具有以下内容的XML文件。

 
 
    
      06/05/2007 
      Sanjay 
      Learn Java 
    
   
    
      19/04/2007 
      Abdul S 
      Learn MySQL 
    
   
    
      06/07/2017
      Krishna Kasyap 
      Learn JavaFX