你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

CosmosDatabase 类

  • java.lang.Object
    • com.azure.cosmos.CosmosDatabase

public class CosmosDatabase

以同步方式执行读取和删除数据库、更新数据库吞吐量以及对子资源执行操作

方法摘要

修饰符和类型 方法和描述
CosmosContainerResponse createContainer(CosmosContainerProperties containerProperties)

创建 Cosmos 容器。

CosmosContainerResponse createContainer(CosmosContainerProperties containerProperties, CosmosContainerRequestOptions options)

在传递其他请求选项时创建 Cosmos 容器。

CosmosContainerResponse createContainer(CosmosContainerProperties containerProperties, ThroughputProperties throughputProperties)

使用自定义吞吐量设置创建 Cosmos 容器。

CosmosContainerResponse createContainer(CosmosContainerProperties containerProperties, ThroughputProperties throughputProperties, CosmosContainerRequestOptions options)

创建 Cosmos 容器。

CosmosContainerResponse createContainer(String id, String partitionKeyPath)

创建 Cosmos 容器。

CosmosContainerResponse createContainer(String id, String partitionKeyPath, ThroughputProperties throughputProperties)

创建 Cosmos 容器。

CosmosContainerResponse createContainerIfNotExists(CosmosContainerProperties containerProperties)

如果不存在与 properties 对象中的 ID 匹配的容器,则创建容器。

CosmosContainerResponse createContainerIfNotExists(CosmosContainerProperties containerProperties, ThroughputProperties throughputProperties)

如果不存在与 properties 对象中的 ID 匹配的容器,则创建 Cosmos 容器。

CosmosContainerResponse createContainerIfNotExists(String id, String partitionKeyPath)

如果不存在与 ID 匹配的容器,则创建 Cosmos 容器。

CosmosContainerResponse createContainerIfNotExists(String id, String partitionKeyPath, ThroughputProperties throughputProperties)

如果不存在与 ID 匹配的容器,则创建 Cosmos 容器。

CosmosUserResponse createUser(CosmosUserProperties userProperties)

创建 Cosmos 用户。

CosmosDatabaseResponse delete()

删除当前的 Cosmos 数据库。

CosmosDatabaseResponse delete(CosmosDatabaseRequestOptions options)

删除当前 Cosmos 数据库,同时指定其他请求选项。

CosmosClientEncryptionKey getClientEncryptionKey(String id)

在不进行服务调用的情况下获取 Cosmos客户端加密密钥对象

CosmosContainer getContainer(String id)

获取不进行服务调用的 Cosmos 容器实例。

String getId()

获取 Cosmos 数据库的 ID。

CosmosUser getUser(String id)

获取不进行服务调用的 Cosmos 用户实例。

CosmosPagedIterable<CosmosContainerProperties> queryContainers(SqlQuerySpec querySpec)

查询当前数据库中的容器。

CosmosPagedIterable<CosmosContainerProperties> queryContainers(SqlQuerySpec querySpec, CosmosQueryRequestOptions options)

查询当前数据库中的容器。

CosmosPagedIterable<CosmosContainerProperties> queryContainers(String query)

查询当前数据库中的容器。

CosmosPagedIterable<CosmosContainerProperties> queryContainers(String query, CosmosQueryRequestOptions options)

查询容器迭代器。

CosmosPagedIterable<CosmosUserProperties> queryUsers(SqlQuerySpec querySpec)

查询当前数据库的所有 Cosmos 用户。

CosmosPagedIterable<CosmosUserProperties> queryUsers(SqlQuerySpec querySpec, CosmosQueryRequestOptions options)

查询当前数据库的所有 Cosmos 用户。

CosmosPagedIterable<CosmosUserProperties> queryUsers(String query)

查询当前数据库的所有 Cosmos 用户。

CosmosPagedIterable<CosmosUserProperties> queryUsers(String query, CosmosQueryRequestOptions options)

查询当前数据库的所有 Cosmos 用户。

CosmosDatabaseResponse read()

读取当前的 Cosmos 数据库。

CosmosDatabaseResponse read(CosmosDatabaseRequestOptions options)

在指定其他请求选项时读取当前 Cosmos 数据库。

CosmosPagedIterable<CosmosClientEncryptionKeyProperties> readAllClientEncryptionKeys()

读取数据库中的所有 cosmos 客户端加密密钥。

CosmosPagedIterable<CosmosContainerProperties> readAllContainers()

读取当前数据库中的所有容器。

CosmosPagedIterable<CosmosUserProperties> readAllUsers()

读取当前数据库的所有 Cosmos 用户。

ThroughputResponse readThroughput()

获取当前数据库的吞吐量。

ThroughputResponse replaceThroughput(ThroughputProperties throughputProperties)

设置当前数据库的吞吐量。

CosmosUserResponse upsertUser(CosmosUserProperties userProperties)

更新插入 Cosmos 用户。

方法继承自 java.lang.Object

方法详细信息

createContainer

public CosmosContainerResponse createContainer(CosmosContainerProperties containerProperties)

创建 Cosmos 容器。

CosmosContainerProperties containerProperties =
     new CosmosContainerProperties(containerId, partitionKeyDefinition);
 try {
     CosmosContainerResponse container = cosmosDatabase.createContainer(containerProperties);
 } catch (CosmosException ce) {
     System.out.println("Failed to create container: " + ce);
 }

参数:

containerProperties - 。。CosmosContainerProperties

返回:

包含 CosmosContainerResponse 创建的容器的 。

createContainer

public CosmosContainerResponse createContainer(CosmosContainerProperties containerProperties, CosmosContainerRequestOptions options)

在传递其他请求选项时创建 Cosmos 容器。

CosmosContainerProperties containerProperties =
     new CosmosContainerProperties(containerId, partitionKeyDefinition);
 try {
     CosmosContainerResponse container = cosmosDatabase.createContainer(containerProperties);
 } catch (CosmosException ce) {
     System.out.println("Failed to create container: " + ce);
 }

参数:

containerProperties - 。。CosmosContainerProperties
options - 。。CosmosContainerProperties

返回:

包含 CosmosContainerResponse 创建的容器的 。

createContainer

public CosmosContainerResponse createContainer(CosmosContainerProperties containerProperties, ThroughputProperties throughputProperties)

使用自定义吞吐量设置创建 Cosmos 容器。

CosmosContainerProperties containerProperties =
     new CosmosContainerProperties(containerId, partitionKeyDefinition);
 ThroughputProperties throughputProperties =
     ThroughputProperties.createAutoscaledThroughput(autoScaleMaxThroughput);
 try {
     CosmosContainerResponse container = cosmosDatabase.createContainer(
         containerProperties,
         throughputProperties
     );
 } catch (CosmosException ce) {
     System.out.println("Failed to create container: " + ce);
 }

参数:

containerProperties - 。。CosmosContainerProperties
throughputProperties - 吞吐量属性。

返回:

包含 CosmosContainerResponse 创建的容器的 。

createContainer

public CosmosContainerResponse createContainer(CosmosContainerProperties containerProperties, ThroughputProperties throughputProperties, CosmosContainerRequestOptions options)

创建 Cosmos 容器。

CosmosContainerProperties containerProperties =
     new CosmosContainerProperties(containerId, partitionKeyDefinition);
 ThroughputProperties throughputProperties =
     ThroughputProperties.createAutoscaledThroughput(autoScaleMaxThroughput);
 try {
     CosmosContainerResponse container = cosmosDatabase.createContainer(
         containerProperties,
         throughputProperties
     );
 } catch (CosmosException ce) {
     System.out.println("Failed to create container: " + ce);
 }

参数:

containerProperties - 容器属性。
throughputProperties - 吞吐量属性。
options - 选项。

返回:

cosmos 容器响应。

createContainer

public CosmosContainerResponse createContainer(String id, String partitionKeyPath)

创建 Cosmos 容器。

ThroughputProperties throughputProperties =
     ThroughputProperties.createAutoscaledThroughput(autoscaledThroughput);
 try {
     CosmosContainerResponse container = cosmosDatabase.createContainer(
         containerId,
         partitionKeyPath,
         throughputProperties
     );
 } catch (CosmosException ce) {
     System.out.println("Failed to create container: " + ce);
 }

参数:

id - 容器 ID。
partitionKeyPath - 分区键路径。

返回:

cosmos 容器响应。

createContainer

public CosmosContainerResponse createContainer(String id, String partitionKeyPath, ThroughputProperties throughputProperties)

创建 Cosmos 容器。

ThroughputProperties throughputProperties =
     ThroughputProperties.createAutoscaledThroughput(autoscaledThroughput);
 try {
     CosmosContainerResponse container = cosmosDatabase.createContainer(
         containerId,
         partitionKeyPath,
         throughputProperties
     );
 } catch (CosmosException ce) {
     System.out.println("Failed to create container: " + ce);
 }

参数:

id - id。
partitionKeyPath - 分区键路径。
throughputProperties - 吞吐量属性。

返回:

cosmos 容器响应。

createContainerIfNotExists

public CosmosContainerResponse createContainerIfNotExists(CosmosContainerProperties containerProperties)

如果不存在与 properties 对象中的 ID 匹配的容器,则创建容器。

CosmosContainerProperties containerProperties =
     new CosmosContainerProperties(containerId, partitionKeyDefinition);
 CosmosContainerResponse container = cosmosDatabase.createContainerIfNotExists(containerProperties);

参数:

containerProperties - 容器属性。

返回:

cosmos 容器响应。

createContainerIfNotExists

public CosmosContainerResponse createContainerIfNotExists(CosmosContainerProperties containerProperties, ThroughputProperties throughputProperties)

如果不存在与 properties 对象中的 ID 匹配的容器,则创建 Cosmos 容器。

CosmosContainerProperties containerProperties =
     new CosmosContainerProperties(containerId, partitionKeyDefinition);
 ThroughputProperties throughputProperties =
     ThroughputProperties.createAutoscaledThroughput(autoScaleMaxThroughput);
 CosmosContainerResponse container = cosmosDatabase.createContainerIfNotExists(
     containerProperties,
     throughputProperties
 );

仅当指定的容器不存在时,才会使用吞吐量属性,并且将创建一个新容器。

参数:

containerProperties - 容器属性。
throughputProperties - 容器的吞吐量属性。

返回:

cosmos 容器响应。

createContainerIfNotExists

public CosmosContainerResponse createContainerIfNotExists(String id, String partitionKeyPath)

如果不存在与 ID 匹配的容器,则创建 Cosmos 容器。

ThroughputProperties throughputProperties =
     ThroughputProperties.createAutoscaledThroughput(autoscaledThroughput);
 CosmosContainerResponse container = cosmosDatabase.createContainerIfNotExists(
     containerId,
     partitionKeyPath,
     throughputProperties
 );

参数:

id - ID。
partitionKeyPath - 分区键路径。

返回:

cosmos 容器响应。

createContainerIfNotExists

public CosmosContainerResponse createContainerIfNotExists(String id, String partitionKeyPath, ThroughputProperties throughputProperties)

如果不存在与 ID 匹配的容器,则创建 Cosmos 容器。

ThroughputProperties throughputProperties =
     ThroughputProperties.createAutoscaledThroughput(autoscaledThroughput);
 CosmosContainerResponse container = cosmosDatabase.createContainerIfNotExists(
     containerId,
     partitionKeyPath,
     throughputProperties
 );

仅当指定的容器不存在时,才会使用吞吐量属性,并且将创建一个新容器。

参数:

id - ID。
partitionKeyPath - 分区键路径。
throughputProperties - 容器的吞吐量属性。

返回:

cosmos 容器响应。

createUser

public CosmosUserResponse createUser(CosmosUserProperties userProperties)

创建 Cosmos 用户。

CosmosUserProperties userProperties = new CosmosUserProperties();
 userProperties.setId(userId);
 cosmosDatabase.createUser(userProperties);

参数:

userProperties - 设置。

返回:

cosmos 用户响应。

delete

public CosmosDatabaseResponse delete()

删除当前的 Cosmos 数据库。

CosmosDatabase cosmosDatabase = cosmosClient
     .getDatabase("<YOUR DATABASE NAME>");
 CosmosDatabaseResponse deleteResponse = cosmosDatabase.delete();

返回:

delete

public CosmosDatabaseResponse delete(CosmosDatabaseRequestOptions options)

删除当前 Cosmos 数据库,同时指定其他请求选项。

CosmosDatabase cosmosDatabase = cosmosClient
     .getDatabase("<YOUR DATABASE NAME>");
 CosmosDatabaseResponse deleteResponse = cosmosDatabase.delete();

参数:

options - 请求 CosmosDatabaseRequestOptions 选项。

返回:

getClientEncryptionKey

public CosmosClientEncryptionKey getClientEncryptionKey(String id)

获取不进行服务调用的 CosmosClientEncryptionKey 对象

参数:

id - clientEncryptionKey 的 id

返回:

Cosmos ClientEncryptionKey

getContainer

public CosmosContainer getContainer(String id)

获取不进行服务调用的 Cosmos 容器实例。

若要获取实际对象,必须先执行读取操作。

参数:

id - 容器的 id。

返回:

Cosmos 容器。

getId

public String getId()

获取 Cosmos 数据库的 ID。

返回:

数据库的 ID。

getUser

public CosmosUser getUser(String id)

获取不进行服务调用的 Cosmos 用户实例。

若要获取实际对象,必须先执行读取操作。

参数:

id - ID。

返回:

用户。

queryContainers

public CosmosPagedIterable queryContainers(SqlQuerySpec querySpec)

查询当前数据库中的容器。

CosmosPagedIterable<CosmosContainerProperties> cosmosContainersList =
     cosmosDatabase.queryContainers("SELECT * FROM DB_NAME");
 cosmosContainersList.forEach(cosmosContainerProperties -> {
     System.out.println(cosmosContainerProperties);
 });

参数:

querySpec - 查询规范。

返回:

queryContainers

public CosmosPagedIterable queryContainers(SqlQuerySpec querySpec, CosmosQueryRequestOptions options)

查询当前数据库中的容器。

CosmosPagedIterable<CosmosContainerProperties> cosmosContainersList =
     cosmosDatabase.queryContainers("SELECT * FROM DB_NAME");
 cosmosContainersList.forEach(cosmosContainerProperties -> {
     System.out.println(cosmosContainerProperties);
 });

参数:

querySpec - 查询规范。
options - 选项。

返回:

queryContainers

public CosmosPagedIterable queryContainers(String query)

查询当前数据库中的容器。

CosmosPagedIterable<CosmosContainerProperties> cosmosContainersList =
     cosmosDatabase.queryContainers("SELECT * FROM DB_NAME");
 cosmosContainersList.forEach(cosmosContainerProperties -> {
     System.out.println(cosmosContainerProperties);
 });

参数:

query - 查询。

返回:

queryContainers

public CosmosPagedIterable queryContainers(String query, CosmosQueryRequestOptions options)

查询容器迭代器。

CosmosPagedIterable<CosmosContainerProperties> cosmosContainersList =
     cosmosDatabase.queryContainers("SELECT * FROM DB_NAME");
 cosmosContainersList.forEach(cosmosContainerProperties -> {
     System.out.println(cosmosContainerProperties);
 });

参数:

query - 查询。
options - 选项。

返回:

queryUsers

public CosmosPagedIterable queryUsers(SqlQuerySpec querySpec)

查询当前数据库的所有 Cosmos 用户。

CosmosPagedIterable<CosmosUserProperties> userPropertiesList =
     cosmosDatabase.queryUsers("SELECT * FROM DB_NAME");
 userPropertiesList.forEach(userProperties -> {
     System.out.println(userProperties);
 });

参数:

querySpec - 查询规范。

返回:

queryUsers

public CosmosPagedIterable queryUsers(SqlQuerySpec querySpec, CosmosQueryRequestOptions options)

查询当前数据库的所有 Cosmos 用户。

CosmosPagedIterable<CosmosUserProperties> userPropertiesList =
     cosmosDatabase.queryUsers("SELECT * FROM DB_NAME");
 userPropertiesList.forEach(userProperties -> {
     System.out.println(userProperties);
 });

参数:

querySpec - 查询规范。
options - 选项。

返回:

queryUsers

public CosmosPagedIterable queryUsers(String query)

查询当前数据库的所有 Cosmos 用户。

CosmosPagedIterable<CosmosUserProperties> userPropertiesList =
     cosmosDatabase.queryUsers("SELECT * FROM DB_NAME");
 userPropertiesList.forEach(userProperties -> {
     System.out.println(userProperties);
 });

参数:

query - 查询。

返回:

queryUsers

public CosmosPagedIterable queryUsers(String query, CosmosQueryRequestOptions options)

查询当前数据库的所有 Cosmos 用户。

CosmosPagedIterable<CosmosUserProperties> userPropertiesList =
     cosmosDatabase.queryUsers("SELECT * FROM DB_NAME");
 userPropertiesList.forEach(userProperties -> {
     System.out.println(userProperties);
 });

参数:

query - 查询。
options - 选项。

返回:

read

public CosmosDatabaseResponse read()

读取当前的 Cosmos 数据库。 根据数据库的唯一标识符提取数据库的详细信息和属性。

CosmosDatabase cosmosDatabase = cosmosClient
     .getDatabase("<YOUR DATABASE NAME>");
 CosmosDatabaseResponse readResponse = cosmosDatabase.read();

返回:

read

public CosmosDatabaseResponse read(CosmosDatabaseRequestOptions options)

在指定其他请求选项时读取当前 Cosmos 数据库。 根据数据库的唯一标识符提取数据库的详细信息和属性。

CosmosDatabase cosmosDatabase = cosmosClient
     .getDatabase("<YOUR DATABASE NAME>");
 CosmosDatabaseResponse readResponse = cosmosDatabase.read();

参数:

options - 请求 CosmosDatabaseRequestOptions 选项。

返回:

readAllClientEncryptionKeys

public CosmosPagedIterable readAllClientEncryptionKeys()

读取数据库中的所有 cosmos 客户端加密密钥。

CosmosPagedIterable<CosmosClientEncryptionKeyProperties> clientEncryptionKeys =
     cosmosDatabase.readAllClientEncryptionKeys();
 clientEncryptionKeys.forEach(encryptionKeyProperties ->
     System.out.println(clientEncryptionKeys)
 );

返回:

readAllContainers

public CosmosPagedIterable readAllContainers()

读取当前数据库中的所有容器。

CosmosPagedIterable<CosmosContainerProperties> cosmosContainersList =
     cosmosDatabase.readAllContainers();
 cosmosContainersList.forEach(cosmosContainerProperties -> {
     System.out.println(cosmosContainerProperties);
 });

返回:

readAllUsers

public CosmosPagedIterable readAllUsers()

读取当前数据库的所有 Cosmos 用户。

CosmosPagedIterable<CosmosUserProperties> cosmosUserProperties = cosmosDatabase.readAllUsers();
 cosmosUserProperties.forEach(userProperties -> {
     System.out.println(userProperties);
 });

返回:

readThroughput

public ThroughputResponse readThroughput()

获取当前数据库的吞吐量。

ThroughputResponse throughputResponse = cosmosDatabase.readThroughput();
 System.out.println(throughputResponse);

返回:

吞吐量响应。

replaceThroughput

public ThroughputResponse replaceThroughput(ThroughputProperties throughputProperties)

设置当前数据库的吞吐量。

ThroughputProperties throughputProperties = ThroughputProperties
     .createAutoscaledThroughput(autoScaleMaxThroughput);

 ThroughputResponse throughputResponse = cosmosDatabase.replaceThroughput(throughputProperties);
 System.out.println(throughputResponse);

参数:

throughputProperties - 吞吐量属性。

返回:

吞吐量响应。

upsertUser

public CosmosUserResponse upsertUser(CosmosUserProperties userProperties)

更新插入 Cosmos 用户。

CosmosUserProperties userProperties = new CosmosUserProperties();
 userProperties.setId(userId);
 cosmosDatabase.upsertUser(userProperties);

参数:

userProperties - 设置。

返回:

cosmos 用户响应。

适用于