你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
CosmosDatabase 类
- java.
lang. Object - com.
azure. cosmos. CosmosDatabase
- com.
public class CosmosDatabase
以同步方式执行读取和删除数据库、更新数据库吞吐量以及对子资源执行操作
方法摘要
方法继承自 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);
}
参数:
返回:
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);
}
参数:
返回:
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);
}
参数:
返回:
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);
}
参数:
返回:
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);
}
参数:
返回:
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);
}
参数:
返回:
createContainerIfNotExists
public CosmosContainerResponse createContainerIfNotExists(CosmosContainerProperties containerProperties)
如果不存在与 properties 对象中的 ID 匹配的容器,则创建容器。
CosmosContainerProperties containerProperties =
new CosmosContainerProperties(containerId, partitionKeyDefinition);
CosmosContainerResponse container = cosmosDatabase.createContainerIfNotExists(containerProperties);
参数:
返回:
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
);
仅当指定的容器不存在时,才会使用吞吐量属性,并且将创建一个新容器。
参数:
返回:
createContainerIfNotExists
public CosmosContainerResponse createContainerIfNotExists(String id, String partitionKeyPath)
如果不存在与 ID 匹配的容器,则创建 Cosmos 容器。
ThroughputProperties throughputProperties =
ThroughputProperties.createAutoscaledThroughput(autoscaledThroughput);
CosmosContainerResponse container = cosmosDatabase.createContainerIfNotExists(
containerId,
partitionKeyPath,
throughputProperties
);
参数:
返回:
createContainerIfNotExists
public CosmosContainerResponse createContainerIfNotExists(String id, String partitionKeyPath, ThroughputProperties throughputProperties)
如果不存在与 ID 匹配的容器,则创建 Cosmos 容器。
ThroughputProperties throughputProperties =
ThroughputProperties.createAutoscaledThroughput(autoscaledThroughput);
CosmosContainerResponse container = cosmosDatabase.createContainerIfNotExists(
containerId,
partitionKeyPath,
throughputProperties
);
仅当指定的容器不存在时,才会使用吞吐量属性,并且将创建一个新容器。
参数:
返回:
createUser
public CosmosUserResponse createUser(CosmosUserProperties userProperties)
创建 Cosmos 用户。
CosmosUserProperties userProperties = new CosmosUserProperties();
userProperties.setId(userId);
cosmosDatabase.createUser(userProperties);
参数:
返回:
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();
参数:
返回:
getClientEncryptionKey
public CosmosClientEncryptionKey getClientEncryptionKey(String id)
获取不进行服务调用的 CosmosClientEncryptionKey 对象
参数:
返回:
getContainer
public CosmosContainer getContainer(String id)
获取不进行服务调用的 Cosmos 容器实例。
若要获取实际对象,必须先执行读取操作。
参数:
返回:
getId
public String getId()
获取 Cosmos 数据库的 ID。
返回:
getUser
public CosmosUser getUser(String id)
获取不进行服务调用的 Cosmos 用户实例。
若要获取实际对象,必须先执行读取操作。
参数:
返回:
queryContainers
public CosmosPagedIterable
查询当前数据库中的容器。
CosmosPagedIterable<CosmosContainerProperties> cosmosContainersList =
cosmosDatabase.queryContainers("SELECT * FROM DB_NAME");
cosmosContainersList.forEach(cosmosContainerProperties -> {
System.out.println(cosmosContainerProperties);
});
参数:
返回:
queryContainers
public CosmosPagedIterable
查询当前数据库中的容器。
CosmosPagedIterable<CosmosContainerProperties> cosmosContainersList =
cosmosDatabase.queryContainers("SELECT * FROM DB_NAME");
cosmosContainersList.forEach(cosmosContainerProperties -> {
System.out.println(cosmosContainerProperties);
});
参数:
返回:
queryContainers
public CosmosPagedIterable
查询当前数据库中的容器。
CosmosPagedIterable<CosmosContainerProperties> cosmosContainersList =
cosmosDatabase.queryContainers("SELECT * FROM DB_NAME");
cosmosContainersList.forEach(cosmosContainerProperties -> {
System.out.println(cosmosContainerProperties);
});
参数:
返回:
queryContainers
public CosmosPagedIterable
查询容器迭代器。
CosmosPagedIterable<CosmosContainerProperties> cosmosContainersList =
cosmosDatabase.queryContainers("SELECT * FROM DB_NAME");
cosmosContainersList.forEach(cosmosContainerProperties -> {
System.out.println(cosmosContainerProperties);
});
参数:
返回:
queryUsers
public CosmosPagedIterable
查询当前数据库的所有 Cosmos 用户。
CosmosPagedIterable<CosmosUserProperties> userPropertiesList =
cosmosDatabase.queryUsers("SELECT * FROM DB_NAME");
userPropertiesList.forEach(userProperties -> {
System.out.println(userProperties);
});
参数:
返回:
queryUsers
public CosmosPagedIterable
查询当前数据库的所有 Cosmos 用户。
CosmosPagedIterable<CosmosUserProperties> userPropertiesList =
cosmosDatabase.queryUsers("SELECT * FROM DB_NAME");
userPropertiesList.forEach(userProperties -> {
System.out.println(userProperties);
});
参数:
返回:
queryUsers
public CosmosPagedIterable
查询当前数据库的所有 Cosmos 用户。
CosmosPagedIterable<CosmosUserProperties> userPropertiesList =
cosmosDatabase.queryUsers("SELECT * FROM DB_NAME");
userPropertiesList.forEach(userProperties -> {
System.out.println(userProperties);
});
参数:
返回:
queryUsers
public CosmosPagedIterable
查询当前数据库的所有 Cosmos 用户。
CosmosPagedIterable<CosmosUserProperties> userPropertiesList =
cosmosDatabase.queryUsers("SELECT * FROM DB_NAME");
userPropertiesList.forEach(userProperties -> {
System.out.println(userProperties);
});
参数:
返回:
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();
参数:
返回:
readAllClientEncryptionKeys
public CosmosPagedIterable
读取数据库中的所有 cosmos 客户端加密密钥。
CosmosPagedIterable<CosmosClientEncryptionKeyProperties> clientEncryptionKeys =
cosmosDatabase.readAllClientEncryptionKeys();
clientEncryptionKeys.forEach(encryptionKeyProperties ->
System.out.println(clientEncryptionKeys)
);
返回:
readAllContainers
public CosmosPagedIterable
读取当前数据库中的所有容器。
CosmosPagedIterable<CosmosContainerProperties> cosmosContainersList =
cosmosDatabase.readAllContainers();
cosmosContainersList.forEach(cosmosContainerProperties -> {
System.out.println(cosmosContainerProperties);
});
返回:
readAllUsers
public CosmosPagedIterable
读取当前数据库的所有 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);
参数:
返回:
upsertUser
public CosmosUserResponse upsertUser(CosmosUserProperties userProperties)
更新插入 Cosmos 用户。
CosmosUserProperties userProperties = new CosmosUserProperties();
userProperties.setId(userId);
cosmosDatabase.upsertUser(userProperties);
参数:
返回: