【问题标题】:How to read data from 2 collections and save data from both collection and save it in 3rd collection in SpringBatch如何从 2 个集合中读取数据并从两个集合中保存数据并将其保存在 SpringBatch 的第 3 个集合中
【发布时间】:2021-08-20 01:53:56
【问题描述】:

我正在使用 springbatch 使用 MongoItemReader bean 从 mongo db 读取数据。假设我想从同一个作业实例中的 2 个不同集合中读取数据。这可能吗?

@Bean
@StepScope
public MongoItemReader<Object> reader() throws UnexpectedInputException, ParseException, Exception {
    DataReader dataReader = new DataReader();
    return dataReader.read();
}

@Bean
public DataItemProcessor processor() {
    return new DataItemProcessor();
}

@Bean
public MongoItemWriter<DestinationCollectionModelClass> writer() {
    MongoItemWriter<DestinationCollectionModelClass> writer = new MongoItemWriter<>();
    writer.setCollection("collection_name_where_data_is_saved");
    writer.setTemplate(mongoTemplate);
    return writer;
}

@Bean
public Step step1(MongoItemWriter<DestinationModelClass> writer) throws UnexpectedInputException, ParseException, Exception {
    return stepBuilderFactory.get("step1")
            // TODO: P3 chunk size configurable
            .<Object, DestinationModelClass>chunk(100)
            .reader(dataReader())
            .processor(processor())
            .writer(writer())
            .build();

}

下面是我的类DataReader.java

公共类 DataReader 扩展 MongoItemReader {

@Autowired
private MongoTemplate mongoTemplate;

@Override
public MongoItemReader<Object> read() throws Exception, UnexpectedInputException, ParseException {

    List<Object> mongoItemReaderList = new ArrayList<>();

    Map<String, Direction> sorts = new HashMap<>();
    sorts.put("_id", Direction.ASC);
    

    MongoItemReader<Object> collectionOneReader = new MongoItemReader<>();
    collectionOneReader.setTemplate(mongoTemplate);
    collectionOneReader.setTargetType(CollectionOneModelClass.class);
    collectionOneReader.setQuery("{}");
    collectionOneReader.setSort(sorts);
    
    MongoItemReader<Object> collectionTwoReader = new MongoItemReader<>();
    collectionTwoReader.setTemplate(mongoTemplate);
    collectionTwoReader.setTargetType(CollectionTwoModelClass.class);
    collectionTwoReader.setQuery("{}");
    collectionTwoReader.setSort(sorts);

    mongoItemReaderList.add(collectionOneReader);
    mongoItemReaderList.add(collectionTwoReader);

    MongoItemReader<Object> readerObject =  (MongoItemReader<Object>) mongoItemReaderList;
    return readerObject;
}

}

下面是我的 DataItemProcessor.java

公共类 DataItemProcessor 实现 ItemProcessor {

public DataItemProcessor() {}

@Override
public DestinationModelClass process(Object phi) throws Exception {
    DestinationModelClass hbd = new DestinationModelClass();
    if(phi instanceof CollectionOneModelClass) {
        //Processing code if Object is an instance of CollectionOneModelClass
    }
    
    if(phi instanceof CollectionTwoModelClass) {
        //Processing code if Object is an instance of CollectionTwoModelClass
    }
    return hbd;
}

}

【问题讨论】:

  • 没有 Spring Batch你会怎么做?请为此分享您的代码。您是否打算“加入”两个系列?在这种情况下你会如何定义一个项目?
  • 我不想加入集合。在我的处理器中,我将使用 collection1 的一些属性和 collection2 的一些属性并保存在第三个集合中。我正在编辑我的问题并添加我的代码 sn-p
  • 请先用一个例子解释您的问题,然后再分享您尝试的解决方案。
  • 问题是我需要从 2 个不同的集合中读取数据并处理它们,并使用两个集合中的一些数据并将其保存在第三个集合中

标签: mongodb spring-boot spring-batch


【解决方案1】:

在同一个面向块的步骤中不能有两个阅读器。您可以做的是使用driving query pattern,在您的情况下,可以按如下方式实现:

  • 项目阅读器:从集合 1 中读取项目
  • 项目处理器:丰富集合 2 中的项目
  • Item Writer:将丰富的项目写入集合 3

【讨论】:

  • 谢谢@Mahmoud Ben Hassine,我会检查链接。
  • 好的,如果您需要帮助,请告诉我。否则,如果有帮助,请考虑接受答案:stackoverflow.com/help/someone-answers。谢谢。
  • 我通过在 Mongo 查询中创建视图实现了我的要求。我正在使用聚合查询创建视图 -$lookup –
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 2021-06-19
  • 1970-01-01
相关资源
最近更新 更多