You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过DocumentDB Java SDK获取集合当前已分配的RUs?

嘿,我来帮你搞定用Java SDK获取DocumentDB集合已分配RUs的事儿!下面分两种常见SDK版本给你具体实现方案,还有一些需要注意的细节。

核心实现方案

旧版DocumentDB SDK(依赖:azure-documentdb)

如果你的项目还在使用传统的azure-documentdb依赖,直接通过DocumentClient读取集合资源,就能提取吞吐量信息:

步骤1:确保依赖正确

在你的pom.xml中添加(如果是Maven项目):

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-documentdb</artifactId>
    <version>2.4.7</version> <!-- 用最新稳定版即可 -->
</dependency>

步骤2:完整代码示例

import com.microsoft.azure.documentdb.DocumentClient;
import com.microsoft.azure.documentdb.DocumentClientException;
import com.microsoft.azure.documentdb.ResourceResponse;
import com.microsoft.azure.documentdb.DocumentCollection;
import com.microsoft.azure.documentdb.ConnectionPolicy;
import com.microsoft.azure.documentdb.ConsistencyLevel;

public class CollectionRUReader {
    public static void main(String[] args) {
        // 替换成你的账户信息
        String accountEndpoint = "https://<your-db-account>.documents.azure.com:443/";
        String accountKey = "<your-master-key>";
        String databaseId = "<target-db-id>";
        String collectionId = "<target-collection-id>";

        // 初始化DocumentClient
        DocumentClient client = new DocumentClient(
                accountEndpoint,
                accountKey,
                ConnectionPolicy.GetDefault(),
                ConsistencyLevel.Session
        );

        try {
            // 读取集合资源
            ResourceResponse<DocumentCollection> collectionResponse = client.readCollection(
                    String.format("/dbs/%s/colls/%s", databaseId, collectionId),
                    null // 请求选项,可留空
            );

            // 提取已分配的RUs
            int assignedThroughput = collectionResponse.getResource().getOfferThroughput();
            System.out.printf("集合 %s 当前已分配的RUs: %d%n", collectionId, assignedThroughput);
        } catch (DocumentClientException e) {
            System.err.println("读取集合信息失败: " + e.getMessage());
            e.printStackTrace();
        } finally {
            // 务必关闭客户端释放资源
            client.close();
        }
    }
}

新版Azure Cosmos DB SDK(依赖:azure-cosmos)

如果你已经升级到新版的azure-cosmos SDK(推荐,因为旧版已进入维护期),代码结构会更简洁:

步骤1:添加依赖

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-cosmos</artifactId>
    <version>4.46.0</version> <!-- 用最新稳定版 -->
</dependency>

步骤2:代码示例

import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.cosmos.CosmosContainer;
import com.azure.cosmos.models.CosmosContainerResponse;

public class CosmosContainerRUReader {
    public static void main(String[] args) {
        String accountEndpoint = "https://<your-db-account>.documents.azure.com:443/";
        String accountKey = "<your-master-key>";
        String databaseId = "<target-db-id>";
        String containerId = "<target-container-id>"; // 新版SDK中Collection改名为Container

        // 使用try-with-resources自动关闭客户端
        try (CosmosClient cosmosClient = new CosmosClientBuilder()
                .endpoint(accountEndpoint)
                .key(accountKey)
                .buildClient()) {

            CosmosContainer container = cosmosClient.getDatabase(databaseId).getContainer(containerId);
            CosmosContainerResponse containerResponse = container.read();

            int assignedThroughput = containerResponse.getProperties().getThroughput();
            System.out.printf("容器 %s 当前已分配的RUs: %d%n", containerId, assignedThroughput);
        } catch (Exception e) {
            System.err.println("读取容器信息失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
关键注意事项
  • 权限要求:确保你使用的密钥(master key或者资源令牌)拥有读取集合/容器元数据的权限,比如Microsoft.DocumentDB/databaseAccounts/readMetadata权限。
  • 共享吞吐量场景:如果你的集合是共享数据库级别的吞吐量,那么getOfferThroughput()会返回null,这时候需要读取数据库的吞吐量信息,代码逻辑类似,把读取集合换成读取数据库即可。
  • 异常处理:实际生产环境中,建议针对不同的DocumentClientException状态码(比如401权限不足、404资源不存在)做针对性处理,而不是简单打印堆栈。

内容的提问来源于stack exchange,提问作者everydaylearn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:01:01