Spring-Boot Spring-Batch MultiResourceItemReader用于合并文件

时间:2015-08-04 22:32:42

标签: spring-boot spring-batch

我正在尝试遵循简单的Spring Batch tutorial,但我想在创建Person对象时阅读两个文件而不是一个文件。

我一直在查看示例,我看到的所有使用两个或更多文件的示例都明确使用了遵循相同格式的多个文件。就我而言,我有两个文件,firstname.csvlastname.csv。我想从他们那里阅读(最好是一步)来创建我的Person

所以示例显示:

    @Bean
    public ItemReader<Person> reader() {
        FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
        reader.setResource(new ClassPathResource("sample-data.csv"));
        reader.setLineMapper(new DefaultLineMapper<Person>() {{
            setLineTokenizer(new DelimitedLineTokenizer() {{
                setNames(new String[] { "firstName", "lastName" });
            }});
            setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{
                setTargetType(Person.class);
            }});
        }});
        return reader;
    }

到目前为止我得到了什么:

  @Bean
  public ItemReader<Person> reader() {
    MultiResourceItemReader<Person> multiResourceItemReader = new MultiResourceItemReader<Person>();
    ClassPathResource[] resources = {new ClassPathResource("firstnames.csv"), new ClassPathResource("lastnames.csv")};
    multiResourceItemReader.setResources(resources);

    // Magic here ...

    return multiResourceItemReader;
  }

如何按照我的描述合并这些文件,而不是使用相同格式的方式合并所有文件?

2 个答案:

答案 0 :(得分:0)

我假设文件包含相同的行数,而对于两个文件中的行N,数据是针对同一个人的。

首先为姓氏创建两个ItemReader,为姓氏创建另一个public ItemReader<Person> getPersonReader { final ItemReader<Person> firstNameReader = firstNameReader(); final ItemReader<Person> lastNameReader = lastNameReader(); return new ItemReader<Person>(){ public Person read() { return mergeFirstLast(firstNameReader.read(), lastNameReader.read()); } }; } public Person mergeFirstLast(Person first, Person last) { // do merge logic here return ... } public ItemReader<Person> firstNameReader() { FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>(); reader.setResource(new ClassPathResource("firstname.csv")); reader.setLineMapper(new DefaultLineMapper<Person>() {{ setLineTokenizer(new DelimitedLineTokenizer() {{ setNames(new String[] { "firstName" }); }}); setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{ setTargetType(Person.class); }}); }}); return reader; } public ItemReader<Person> lastNameReader() { FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>(); reader.setResource(new ClassPathResource("lastname.csv")); reader.setLineMapper(new DefaultLineMapper<Person>() {{ setLineTokenizer(new DelimitedLineTokenizer() {{ setNames(new String[] { "lastName" }); }}); setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{ setTargetType(Person.class); }}); }}); return reader; } 。然后将两个项目读者的结果合并在一起。

像这样:

<wsdl:definitions xmlns:ns1="http://v3.ws.contour.jamasoftware.com/"
  xmlns:ns2="http://cxf.apache.org/bindings/xformat"
  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
  xmlns:tns="http://v3.ws.contour.jamasoftware.com"
  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ContourSoapServiceV3"
  targetNamespace="http://v3.ws.contour.jamasoftware.com">
<wsdl:import
  location="http://MYSERVER/jama/ws/v3/soap/ContourSoapService?wsdl=ContourSoapService.wsdl"
  namespace="http://v3.ws.contour.jamasoftware.com/"/>
<wsdl:binding name="ContourSoapServiceV3SoapBinding" type="ns1:ContourSoapService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
And a little later on...
<wsdl:operation name="getProjects">
  <soap:operation soapAction="getProjects" style="document"/>
  <wsdl:input name="getProjects">
    <soap:body use="literal"/>
  </wsdl:input>
  <wsdl:output name="getProjectsResponse">
    <soap:body use="literal"/>
  </wsdl:output>
</wsdl:operation>

答案 1 :(得分:-1)

这可以在项目处理器中读取第二个文件 -

您的-reader的类型为 - firtsNameReader

您的-Processor类型为 - firstNameReader,它返回具有 - 名字和姓氏

的对象

您的编写者将是类型对象 - 将名字和姓氏写入新文件

Class ItemProcessor


public static int readLines=0;

Public void process(firstName){

String lastName;

ItemProcessor.readLines++;

FileInputStream fs= new FileInputStream("lastName.csv");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
for(int i = 0; i < readLines.readLines; ++i){
  br.readLine();
  }
 lastName = br.readLine();



 System.out.println (firstName);
  System.out.println (lastName);

  NewObject obj = new NewObject();

  obj.setLastName(lastName);
  obj.setFirstName(firstName);

  return obj;

  }