有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在spring批处理中使用MongoItemReader的聚合查询

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

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;

    }

共 (2) 个答案

  1. # 1 楼答案

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

    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();
    
    }
    }
    

    以及其他的获取者和设置者以及其他方法。请看一下MongoItemReader here的源代码。 我还从中删除了查询支持。你也可以用同样的方法从MongoItemReader复制粘贴它。Sort也一样

    在有读者的课堂上,你会做一些类似的事情:

    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;
    }
    
  2. # 2 楼答案

    为了能够在作业中使用聚合,利用spring batch的所有功能,您必须创建一个自定义ItemReader。 扩展AbstractPaginatedDateItemReader,我们可以使用可分页操作中的所有元素。 下面是该自定义类的一个简单示例:

    public class CustomAggreagationPaginatedItemReader<T> extends AbstractPaginatedDataItemReader<T> implements InitializingBean {
    
        private static final Pattern PLACEHOLDER = Pattern.compile("\\?(\\d+)");
        private MongoOperations template;
        private Class<? extends T> type;
        private Sort sort;
        private String collection;
    
        public CustomAggreagationPaginatedItemReader() {
            super();
            setName(ClassUtils.getShortName(CustomAggreagationPaginatedItemReader.class));
        }
    
        public void setTemplate(MongoOperations template) {
            this.template = template;
        }
    
        public void setTargetType(Class<? extends T> type) {
            this.type = type;
        }
    
        public void setSort(Map<String, Sort.Direction> sorts) {
            this.sort = convertToSort(sorts);
        }
    
        public void setCollection(String collection) {
            this.collection = collection;
        }
    
        @Override
        @SuppressWarnings("unchecked")
        protected Iterator<T> doPageRead() {
            Pageable pageRequest = new PageRequest(page, pageSize, sort);
    
            BasicDBObject cursor = new BasicDBObject();
            cursor.append("batchSize", 100);
    
            SkipOperation skipOperation = skip(Long.valueOf(pageRequest.getPageNumber()) * Long.valueOf(pageRequest.getPageSize()));
    
            Aggregation aggregation = newAggregation(
                    //Include here all your aggreationOperations,
                    skipOperation,
                    limit(pageRequest.getPageSize())
                ).withOptions(newAggregationOptions().cursor(cursor).build());
    
            return (Iterator<T>) template.aggregate(aggregation, collection, type).iterator();
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            Assert.state(template != null, "An implementation of MongoOperations is required.");
            Assert.state(type != null, "A type to convert the input into is required.");
            Assert.state(collection != null, "A collection is required.");
        }
    
        private String replacePlaceholders(String input, List<Object> values) {
            Matcher matcher = PLACEHOLDER.matcher(input);
            String result = input;
    
            while (matcher.find()) {
                String group = matcher.group();
                int index = Integer.parseInt(matcher.group(1));
                result = result.replace(group, getParameterWithIndex(values, index));
            }
    
            return result;
        }
    
        private String getParameterWithIndex(List<Object> values, int index) {
            return JSON.serialize(values.get(index));
        }
    
        private Sort convertToSort(Map<String, Sort.Direction> sorts) {
            List<Sort.Order> sortValues = new ArrayList<Sort.Order>();
    
            for (Map.Entry<String, Sort.Direction> curSort : sorts.entrySet()) {
                sortValues.add(new Sort.Order(curSort.getValue(), curSort.getKey()));
            }
    
            return new Sort(sortValues);
        }
    }
    

    如果你仔细观察,你会发现它是使用Spring框架中的MongoItemReader创建的,你可以在org上看到这个类。springframework。一批项目数据MongoItemReader,有一种方法可以创建一个扩展AbstractPaginatedDaitaItemReader的全新类,如果你看一看“doPageRead”方法,你应该能够看到它只使用MongoTemplate的find操作,因此不可能在其中使用聚合操作

    以下是我们的CustomReader如何使用它:

    @Bean
    public ItemReader<YourDataClass> reader(MongoTemplate mongoTemplate) {
        CustomAggreagationPaginatedItemReader<YourDataClass> customAggreagationPaginatedItemReader = new CustomAggreagationPaginatedItemReader<>();
    
        Map<String, Direction> sort = new HashMap<String, Direction>();
        sort.put("id", Direction.ASC);
    
        customAggreagationPaginatedItemReader.setTemplate(mongoTemplate);
        customAggreagationPaginatedItemReader.setCollection("collectionName");
        customAggreagationPaginatedItemReader.setTargetType(YourDataClass.class);
        customAggreagationPaginatedItemReader.setSort(sort);
    
        return customAggreagationPaginatedItemReader;
    }
    

    正如您可能注意到的,您还需要一个MongoTemplate实例,下面是它的工作原理:

    @Bean
    public MongoTemplate mongoTemplate(MongoDbFactory mongoDbFactory) {
        return new MongoTemplate(mongoDbFactory);
    }
    

    其中,MongoDbFactory是spring framework的一个自动连接对象

    希望这足以帮助你