azure - Spring Boot Cosmos DB 实现中的动态查询

标签 azure spring-boot spring-data azure-cosmosdb azure-cosmosdb-sqlapi

我正在使用 Azure Cosmos DB for Spring 开发一个应用程序。

我有一个包含模型类和 ReactiveCosmosRepository 的结构,我用它来进行查询。

我通常在存储库类中注释我的查询:

@Repository
public interface ArchivedDocumentRepository extends ReactiveCosmosRepository<ArchivedDocument, String> {

@Query("SELECT * FROM c)
Flux<ArchivedDocument> findAllArchivedDocuments();
    
@Query("SELECT * FROM c where c.document_id = @documentId")
Mono<ArchivedDocument> findArchivedDocument(@Param("documentId") String documentId);
    
}

但是现在我需要使用一些逻辑创建 SQL,并且不能像这样注释它。 我怎样才能做到这一点?

我还直接使用了 Azure Cosmos SDK,您可以这样做:

client = new CosmosClientBuilder().endpoint("SOMEURL")
        .key("SOMEKEY")
        .preferredRegions(Collections.singletonList("SOMELOCATION"))
        .consistencyLevel(ConsistencyLevel.EVENTUAL).buildClient();

database = client.getDatabase("DBNAME");
String containerName = "CONTAINERNAME";
CosmosContainer container = database.getContainer(containerName);

String sql = "SELECT * FROM c";

CosmosPagedIterable<DocumentMetadata> filteredDocumentMetadata = container.queryItems(sql, new CosmosQueryRequestOptions(), DocumentMetadata.class);

...
}

如果我将现有的存储库代码与我的服务中的类似代码结合起来,我可以检索我喜欢的数据并按照我喜欢的方式构建查询,但这似乎有点不必要,例如当我已经有连接时实例化“客户端”对象?

有什么建议吗?如何将 SDK 直接与 Spring 层结合起来,以便能够基于逻辑创建查询?

最佳答案

我认为你走在正确的道路上。您可能需要使用 CosmosAsyncClient

在较低级别上工作

如果您使用的配置扩展了 AbstractCosmosConfiguration,那么您甚至不需要创建 CosmosAsyncClient,因为它是在 AbstractCosmosConfiguration 类中定义的。

自定义存储库

package com.vob.reactive.webflux.service;

import com.azure.cosmos.CosmosAsyncClient;
import com.azure.cosmos.models.SqlParameter;
import com.azure.cosmos.models.SqlQuerySpec;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

@Component
@Slf4j
public class CustomRepository {


    private final CosmosAsyncClient cosmosAsyncClient;

    @Autowired
    public CustomRepository(CosmosAsyncClient cosmosAsyncClient) {
        this.cosmosAsyncClient = cosmosAsyncClient;
    }

    public <T> Mono<T> getSomethingFrom(String database, String container, String id, Class<T> classType){
        return this.cosmosAsyncClient
                .getDatabase(database)
                .getContainer(container)
                .queryItems(new SqlQuerySpec("select * from c where c.document_id = @documentId", new SqlParameter("documentId", id)), classType)
                .single();
    }

}

您可能需要通过将 getDatabase 和 getContainer 存储在 Map 中来改进它们,这样您就不需要每次都检查它们是否存在

关于azure - Spring Boot Cosmos DB 实现中的动态查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67399958/

相关文章:

visual-studio - Azure WebJob - 创建目录失败

C# 使用 Azure 资源管理器 (ARM) 部署 Azure 虚拟机时获取结果状态

php - MySQL中使用clearDB自动加1

c# - 基于容器的函数应用程序在被 CORSSettingsChanged 和 AppSettingsChange 回收后丢失了自定义应用程序设置

java - model.addAttribute() 对于每个循环

java - Spring Data JPA 与远程 MySQL 服务器的 ssh 隧道

java - SpringBoot 2 无法访问 ConfigurationProperties

java - 如何设置 spring-boot 以允许从外部 IP 地址访问网络服务器

mongodb - mongodb中实现带总页数的嵌入式分页

spring-data - @EnableJpaRepositories.enableDefaultTransactions = false 不起作用