如何在 Spring Batch 中将聚合查询与 MongoItemReader 一起使用 [英] How to use Aggregation Query with MongoItemReader in spring batch

查看:60
本文介绍了如何在 Spring Batch 中将聚合查询与 MongoItemReader 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需求是如何改变的,我必须在 setQuery() 中使用基本查询的聚合查询.这甚至可能吗?请建议我该怎么做?我的聚合查询已准备就绪,但不确定如何在 spring 批处理中使用它

Some how the requirement changed and I have to use aggregation query insted of basic query in setQuery(). Is this even possible? Please suggest how can i do that? My Aggregation query is ready but not sure how can I use that in spring batch

public ItemReader<ProfileCollection> searchMongoItemReader() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {

        MongoItemReader<MyCollection> mongoItemReader = new MongoItemReader<>();
        mongoItemReader.setTemplate(myMongoTemplate);
        mongoItemReader.setCollection(myMongoCollection);

        mongoItemReader.setQuery(" Some Simple Query - Basic");

        mongoItemReader.setTargetType(MyCollection.class);
        Map<String, Sort.Direction> sort = new HashMap<>();
        sort.put("field4", Sort.Direction.ASC);
        mongoItemReader.setSort(sort);
        return mongoItemReader;

    }

推荐答案

扩展 MongoItemReader 并为方法 doPageRead() 提供您自己的实现.通过这种方式,您将获得完整的分页支持,并且阅读文档将成为一个步骤的一部分.

extend MongoItemReader and provide your own implementation for method doPageRead(). This way you will have full pagination support and this reading of documents will be part of a step.

public class CustomMongoItemReader<T, O> extends MongoItemReader<T> {
private MongoTemplate template;
private Class<? extends T> inputType;
private Class<O> outputType
private MatchOperation match;
private ProjectionOperation projection;
private String collection;

@Override
protected Iterator<T> doPageRead() {
    Pageable page = PageRequest.of(page, pageSize) //page and page size are coming from the class that MongoItemReader extends
    Aggregation agg = newAggregation(match, projection, skip(page.getPageNumber() * page.getPageSize()), limit(page.getPageSize()));
    return (Iterator<T>) template.aggregate(agg, collection, outputType).iterator();

}
}

以及其他 getter 和 setter 等方法.只需看看 MongoItemReader 的源代码 此处.我还从中删除了查询支持.您也可以在相同的方法中使用它,只需从 MongoItemReader 复制粘贴即可.与排序相同.

And other getter and setters and other methods. Just have a look at sourcecode for MongoItemReader here. I also removed Query support from it. You can have that also in the same method just copy paste it from MongoItemReader. Same with Sort.

在有读者的课堂上,你会做这样的事情:

And in the class where you have a reader, you would do something like:

public MongoItemReader<T> reader() {
    CustomMongoItemReader reader = new CustomMongoItemReader();
    reader.setTemplate(mongoTemplate);
    reader.setName("abc");
    reader.setTargetType(input.class);
    reader.setOutputType(output.class);
    reader.setCollection(myMongoCollection);
    reader.setMatch(Aggregation.match(new Criteria()....)));
    reader.setProjection(Aggregation.project("..","..");
    return reader;
}

这篇关于如何在 Spring Batch 中将聚合查询与 MongoItemReader 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆