简体   繁体   English

Spring Batch如何读取多个表(查询)作为Reader并将其写为平面文件写入

[英]Spring Batch How to read multiple table (queries) as Reader and write it as flat file write

In my project I have read multiple tables with different queries and consolidate those results sets in flat files. 在我的项目中,我已经阅读了多个具有不同查询的表,并将这些结果集合并到平面文件中。 How do I achieve that. 我如何实现这一目标。 I mean JdbcReader is directly taking 1 select query, how can I customize it. 我的意思是JdbcReader直接采用1个选择查询,我该如何自定义它。

If JdbcCursorItemReader does not suit your needs, you are always free to implement a custom reader by implementing the ItemReader interface. 如果JdbcCursorItemReader不适合您的需求,您可以通过实现ItemReader接口自由地实现自定义阅读器。

public interface ItemReader<T> {
        T read() throws Exception, UnexpectedInputException, ParseException;
}

Just write a class that implements this interface and inject a jdbcTemplate to query multiple tables. 只需编写一个实现此接口的类,并注入一个jdbcTemplate来查询多个表。

public Class MyCompositeJdbcReader implements ItemReader<ConsolidateResult>{
    private JdbcTemplate jdbcTemplate;

    public ConsolidateResult read() 
       throws Exception, UnexpectedInputException, ParseException{
    ConsolidateResult cr = new ConsolidateResult();     

    String name= this.jdbcTemplate.queryForObject(
        "select name from customer where id = ?",new Object[]{1}, String.class);
    String phoneNumber= this.jdbcTemplate.queryForObject(
        "select phone from customer_contact where custid = ?",
        new Object[]{1},String.class);

    cr.setName(name);
    cr.setPhone(phoneNumber);
    return cr;
 }

} }

I did not compile the code but I hope it gives an idea. 我没有编译代码,但我希望它能给出一个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Spring Batch读写zOS(Mainframe)平面文件 - How to Read/write zOS(Mainframe) flat file using spring batch 如何为自定义Spring Batch阅读器编写Junit - How to write junits for custom spring batch reader 从数据库读取批量数据(动态查询),并使用spring batch将其写入平面文件 - Read bulk data (Dynamic query) from Database and write to flat file using spring batch spring 批量读取多个文件并写入一个文件 - spring batch read from multiple files and write to one file 使用Spring批处理读取文件并写入Map - Using Spring batch to read a file and write to a Map 无法在Spring批处理中写入FlatFile - 只有对象引用正在写入平面文件 - Unable to write to FlatFile in spring batch - only object reference are getting write to flat file 如何在spring-batch中写入压缩文件 - How to write to a compressed file in spring-batch Spring Batch-尝试读取/写入同一表时获取DeadlockLoserDataAccessException - Spring Batch - Getting a DeadlockLoserDataAccessException when trying to read/write to the same table 如何使用缓冲读取器和缓冲写入器在Java中读写文件 - How to read and write a file in java using buffered reader and bufferedwriter 如何在 spring 批处理中组合多个侦听器(步骤、读取、处理、写入和跳过) - How to combine multiple listeners (step, read, process, write and skip) in spring batch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM