首页 文章

从20个相关表中获取数据(通过id),将它们组合到一个json文件中并利用 spring 批量

提问于
浏览
0

我在SQL Server中有一个Person数据库,其中包含地址,许可证,亲属等表格,其中大约有20个 . 所有表都具有每个人唯一的id参数 . 这些表中有数百万条记录 . 我需要使用他们的公共id参数组合人员的这些记录,并转换为具有一些列名称更改的json表文件 . 这个json文件然后通过 生产环境 者推送到kafka . 如果我可以将kafka 生产环境 者的示例作为项目编写者,那么真正的问题是了解如何利用 spring batch 项目阅读器,处理器和项目编写器创建复合json文件的策略和细节 . 这是我的第一个Spring批处理应用程序,所以我对此比较陌生 .

我希望使用复合阅读器或处理器的实现策略的建议使用person id作为游标,并使用每个表的id查询每个表,将结果记录转换为json并将其聚合为复合,关系json带有根表PersonData的文件,它提供给kafka集群 .

基本上我有一个数据源,读者的数据库相同 . 我计划使用Person表来获取该人唯一的id和其他记录,并使用id作为其他19个表的where子句 . 将每个结果集从表转换为json,并在末尾合成json对象并写入kafka .

2 回答

  • 0

    我们在一个项目中有这样的要求,并用以下方法解决它 .

    • 在Splitflow中,并行运行,我们有一个永久表的步骤,在文件中加载表的数据,按公共ID排序(这是可选的,但如果你有文件中的数据,它更容易测试) .

    • 然后我们实现了自己的“MergeReader” . 这个mergereader为每个文件/表都有FlatFileItemReaders(让我们称之为dataReaders) . 所有这些FlatFileItemReaders都包含一个SingleItemPeekableItemReader . MergeReader的read方法的逻辑如下:

    public MyContainerPerId read() {
    
       // you need a container to store the items, that belong together
       MyContainerPerId container = new MyContainerPerId();
    
       //  peek through all "dataReaders" to find the lowest actual key
       int lowestId = searchLowestKey();
    
       for (Reader dataReader : dataReaders) {
           // I assume, that more than one entry in a table can belong to
           // the same person id
           wihile (dataReader.peek().getId() == lowestId) {
           {
                 container.add(dataReader.read());
           }
       }
    
       // the container contains all entries from all tables
       // belonging to the same person id    
       return container;
    }
    

    如果需要重新启动功能,则必须以某种方式实现ItemStream,以便跟踪每个dataReader的当前读取位置 .

  • 0

    我使用了描述hereDriving Query Based ItemReaders 使用模式来解决此问题 .

    Reader:只是一个JdbcCursoritemReader的默认实现,带有要获取的sql
    唯一的关系ID(例如,从人物中选择ID)

    处理器:使用这个长id作为输入,并使用jdbcTemplate从spring实现的dao通过针对特定id的每个表的查询获取数据(例如select * from license where id =)并将结果以列表格式映射到a POJO of Person - 然后转换为json对象(使用Jackson)然后转换为字符串

    Writer:要么用json字符串写出文件,要么在使用kafka的情况下将json字符串发布到主题

相关问题