你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
CosmosClient 类
- java.
lang. Object - com.
azure. cosmos. CosmosClient
- com.
实现
public final class CosmosClient
implements Closeable
提供 Azure Cosmos DB 服务的客户端逻辑表示形式。 对 CosmosClient API 的调用将被阻止以完成。
CosmosClient 是线程安全的。 建议在应用程序的生命周期内维护 CosmosClient 的单个实例,从而实现高效的连接管理和性能。 CosmosClient 初始化是一项繁重的操作 - 不要使用初始化 CosmosClient 实例作为凭据或网络连接验证。
方法摘要
方法继承自 java.lang.Object
方法详细信息
close
public void close()
关闭此 CosmosClient 实例。
createDatabase
public CosmosDatabaseResponse createDatabase(CosmosDatabaseProperties databaseProperties)
创建 Cosmos 数据库。
CosmosDatabaseProperties databaseProperties = new CosmosDatabaseProperties(databaseName);
cosmosClient.createDatabase(databaseProperties);
参数:
返回:
createDatabase
public CosmosDatabaseResponse createDatabase(CosmosDatabaseProperties databaseProperties, CosmosDatabaseRequestOptions options)
创建数据库。
CosmosDatabaseProperties databaseProperties = new CosmosDatabaseProperties(databaseName);
cosmosClient.createDatabase(databaseProperties);
参数:
返回:
createDatabase
public CosmosDatabaseResponse createDatabase(CosmosDatabaseProperties databaseProperties, ThroughputProperties throughputProperties)
创建 Cosmos 数据库。
ThroughputProperties throughputProperties = ThroughputProperties
.createAutoscaledThroughput(autoScaleMaxThroughput);
cosmosClient.createDatabase(databaseName, throughputProperties);
参数:
返回:
createDatabase
public CosmosDatabaseResponse createDatabase(CosmosDatabaseProperties databaseProperties, ThroughputProperties throughputProperties, CosmosDatabaseRequestOptions options)
创建 Cosmos 数据库。
ThroughputProperties throughputProperties = ThroughputProperties
.createAutoscaledThroughput(autoScaleMaxThroughput);
cosmosClient.createDatabase(databaseName, throughputProperties);
参数:
返回:
createDatabase
public CosmosDatabaseResponse createDatabase(String id)
创建 Cosmos 数据库。
CosmosDatabaseProperties databaseProperties = new CosmosDatabaseProperties(databaseName);
cosmosClient.createDatabase(databaseProperties);
参数:
返回:
createDatabase
public CosmosDatabaseResponse createDatabase(String id, ThroughputProperties throughputProperties)
创建 Cosmos 数据库。
ThroughputProperties throughputProperties = ThroughputProperties
.createAutoscaledThroughput(autoScaleMaxThroughput);
cosmosClient.createDatabase(databaseName, throughputProperties);
参数:
返回:
createDatabaseIfNotExists
public CosmosDatabaseResponse createDatabaseIfNotExists(String id)
如果服务中尚不存在 Cosmos 数据库,请创建该数据库。
CosmosDatabaseProperties databaseProperties = new CosmosDatabaseProperties(databaseName);
cosmosClient.createDatabaseIfNotExists(databaseProperties);
参数:
返回:
createDatabaseIfNotExists
public CosmosDatabaseResponse createDatabaseIfNotExists(String id, ThroughputProperties throughputProperties)
如果服务中尚不存在 Cosmos 数据库,请创建该数据库。
ThroughputProperties throughputProperties = ThroughputProperties
.createAutoscaledThroughput(autoScaleMaxThroughput);
cosmosClient.createDatabaseIfNotExists(databaseName, throughputProperties);
仅当指定的数据库不存在时,才会使用 throughputProperties,并且将创建具有 throughputProperties 的新数据库。
参数:
返回:
createGlobalThroughputControlConfigBuilder
public GlobalThroughputControlConfigBuilder createGlobalThroughputControlConfigBuilder(String databaseId, String containerId)
创建将用于生成 GlobalThroughputControlConfig的全局吞吐量控制配置生成器。
参数:
返回:
getDatabase
public CosmosDatabase getDatabase(String id)
在不进行服务调用的情况下获取 Cosmos 数据库实例。
参数:
返回:
queryDatabases
public CosmosPagedIterable
查询 Cosmos 数据库。
CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
CosmosPagedIterable<CosmosDatabaseProperties> databaseProperties =
cosmosClient.queryDatabases("select * from d", options);
databaseProperties.forEach(properties -> {
System.out.println(properties.getId());
});
参数:
返回:
queryDatabases
public CosmosPagedIterable
查询 Cosmos 数据库。
CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
CosmosPagedIterable<CosmosDatabaseProperties> databaseProperties =
cosmosClient.queryDatabases("select * from d", options);
databaseProperties.forEach(properties -> {
System.out.println(properties.getId());
});
参数:
返回:
readAllDatabases
public CosmosPagedIterable
读取所有 Cosmos 数据库。
CosmosPagedIterable<CosmosDatabaseProperties> cosmosDatabaseProperties =
cosmosClient.readAllDatabases();
cosmosDatabaseProperties.forEach(databaseProperties -> {
System.out.println(databaseProperties);
});
返回: