0

我正在尝试将 Spring Boot 应用程序从 mssql 迁移到 CosmosDb。

我们有以下存储库实现,使用org.springframework.data.jpa.domain.Specification; 它可以根据用户请求的任何(一个或多个)列获取数据。

static Specification<User> isMatching(final Map<String, String> parameters) {
    return new Specification<User>() {
        private static final long serialVersionUID = 1L;

        public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            List<Predicate> predicates = new ArrayList<>();
            parameters.entrySet().forEach(e -> {
                
                predicates.add(builder.and(builder.equal(root.get(e.getKey()), e.getValue())));
            });
            return builder.and(predicates.toArray(new Predicate[predicates.size()]));
        }
    };
}

然后我们将其称为如下:

repo.findAll(UserRepo.isMatching(parameters));

我不得不从类路径中删除 spring-boot-starter-data-jpa,因为它不支持 CosmosDb。我在类路径中使用下面的 spring 和 spring 数据 cosmos 引用,因为这是唯一对我有用的组合。

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath />
    </parent>
<dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-spring-data-cosmos</artifactId>
            <version>3.0.0</version>
        </dependency>

我真的不知道如何为 CosmosDb 实现上述规范逻辑。我非常感谢任何有关此的指导。提前致谢!

4

1 回答 1

1

目前 azure-spring-data-cosmos 不支持 azure-spring-data-cosmos SDK 中的 Specification / QueryDSL。我能想到的最接近的选择是将 Criteria API 与SqlQuerySpec. 使用SqlQuerySpec您可以生成CosmosQuery类型,然后可以根据您的存储库样式(同步与异步)使用 CosmosTemplate 或 ReactiveCosmosTemplate 执行该类型。

关于版本控制,我建议使用最新版本的 spring-boot-starter-parent - 2.6.2 和 azure-spring-data-cosmos 3.17.0,这样您也可以获得查询注释的最新功能。

如果这SqlQuerySpec也不起作用,另一种方法是在您的 spring 应用程序中获取CosmosClientbean applicationContext,并直接针对 azure-cosmos Java v4 SDK 运行查询,该 SDK 在我们的 azure-spring-data-cosmos SDK 的引擎盖下使用。

请关注此 github 问题以获取有关您问题的任何更新 - https://github.com/Azure/azure-sdk-for-java/issues/25629

于 2022-02-07T17:25:15.877 回答