春季批处理:一步即可使用多个项目读取器

bid*_*jee 5 java spring-batch

我是春季新手。我需要在春季批处理中完成的任务如下:

  1. 需要从数据库中读取一些元数据。
  2. 基于此元数据,我需要读取一些文件。
  3. 经过一些处理后,需要将这些值从文件写入数据库。

我的查询如下:

一种。对于第一个要求,我需要将整个结果集映射到一个对象,其中与Person相关的数据在一个表中,与Pets相关的数据在另一个表中,并通过person id联接。

public class PersonPetDetails {

    private String personName;
    private String personAddr;

    private int personAge;

    private List<Pet> pets;
Run Code Online (Sandbox Code Playgroud)

为此,我编写了一个自定义项目读取器,该读取器扩展了JdbcCursorItemReader。

public class CustomJDBCCusrorItemReader<T> extends JdbcCursorItemReader<T> {

    private ResultSetExtractor<T> resultSetExtractor;


    public void setResultSetExtractor(ResultSetExtractor<T> resultSetExtractor) {
        this.resultSetExtractor = resultSetExtractor;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        setVerifyCursorPosition(false);
        Assert.notNull(getDataSource(), "DataSource must be provided");
        Assert.notNull(getSql(), "The SQL query must be provided");
        Assert.notNull(resultSetExtractor, "ResultSetExtractor must be provided");
    }


    @Override
    protected T readCursor(ResultSet rs, int currentRow) throws SQLException {      
        return resultSetExtractor.extractData(rs);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是达到我要求的正确方法吗?还是有更好的方法?

b。AFAIK,在春季批处理中,没有作家就没有读者。因此,在工作的不同步骤中,我无法调用另一组阅读器。然后,如何在一个步骤中调用多个读者?

C。另外,根据某些条件,我可能需要调用第三组Reader。我如何有条件地一步一步给读者打电话?

感谢您浏览我的帖子。我知道那很长。任何帮助深表感谢。另外,我猜示例代码片段将帮助我更好地理解这一点。:)

Ngh*_* Do 3

我会推荐如下

高层设计:

  1. Partitioner 它将处理人员列表。注意:此时还没有提取宠物数据。

  2. Reader 它将获得属于一个人的宠物列表。注意:读者将返回仅特定于某个人的宠物列表。

  3. 基于宠物的处理器,您将根据您的要求进行处理。

  4. Writer 根据您写入数据库的要求。

低级代码片段:

  1. Partitioner

    public class PetPersonPartitioner implements Partitioner {
    
      @Autowired
      private PersonDAO personDAO;
    
      @Override
      public Map<String, ExecutionContext> partition(int gridSize) {
    
        Map<String, ExecutionContext> queue = new HashMap<String, ExecutionContext>();
    
        List<Person> personList = this.personDAO.getAllPersons();
        for (Person person : personList) {
    
          ExecutionContext ec = new ExecutionContext();
          ec.put("person", person);
          ec.put("personId", person.getId());
    
          queue.put(person.getId(), ec);
        }
    
      return queue;
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. Reader

    <bean id="petByPersonIdRowMapper" class="yourpackage.PetByPersonIdRowMapper" />
    
    <bean id="petByPesonIdStatementSetter" scope="step"
          class="org.springframework.batch.core.resource.ListPreparedStatementSetter">
        <property name="parameters">
            <list>
                <value>#{stepExecutionContext['personId']}</value>
            </list>
        </property>
    </bean>
    
    Run Code Online (Sandbox Code Playgroud)
public class PetByPersonIdRowMapper implements RowMapper<PersonPetDetails> {
    @Override
    public BillingFeeConfigEntity mapRow(ResultSet rs, int rowNum) throws SQLException {
        PersonPetDetails record = new PersonPetDetails();

        record.setPersonId(rs.getLong("personId"));
      record.setPetId(rs.getLong("petid");
      ...
      ...
}
Run Code Online (Sandbox Code Playgroud)
  1. Processor 您可以继续处理每个PersonPetDetails对象。