Delen via


CosmosClient Class

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

Implements

public final class CosmosClient
implements Closeable

Provides a client-side logical representation of the Azure Cosmos DB service. Calls to CosmosClient API's are blocked for completion.

CosmosClient is thread-safe. It's recommended to maintain a single instance of CosmosClient per lifetime of the application which enables efficient connection management and performance. CosmosClient initialization is a heavy operation - don't use initialization CosmosClient instances as credentials or network connectivity validations.

Method Summary

Modifier and Type Method and Description
void close()

Close this CosmosClient instance.

CosmosDatabaseResponse createDatabase(CosmosDatabaseProperties databaseProperties)

Creates a Cosmos database.

CosmosDatabaseResponse createDatabase(CosmosDatabaseProperties databaseProperties, CosmosDatabaseRequestOptions options)

Creates a database.

CosmosDatabaseResponse createDatabase(CosmosDatabaseProperties databaseProperties, ThroughputProperties throughputProperties)

Creates a Cosmos database.

CosmosDatabaseResponse createDatabase(CosmosDatabaseProperties databaseProperties, ThroughputProperties throughputProperties, CosmosDatabaseRequestOptions options)

Creates a Cosmos database.

CosmosDatabaseResponse createDatabase(String id)

Creates a Cosmos database.

CosmosDatabaseResponse createDatabase(String id, ThroughputProperties throughputProperties)

Creates a Cosmos database.

CosmosDatabaseResponse createDatabaseIfNotExists(String id)

Create a Cosmos database if it does not already exist on the service.

CosmosDatabaseResponse createDatabaseIfNotExists(String id, ThroughputProperties throughputProperties)

Create a Cosmos database if it does not already exist on the service.

GlobalThroughputControlConfigBuilder createGlobalThroughputControlConfigBuilder(String databaseId, String containerId)

Create global throughput control config builder which will be used to build GlobalThroughputControlConfig.

CosmosDatabase getDatabase(String id)

Gets the Cosmos database instance without making a service call.

CosmosPagedIterable<CosmosDatabaseProperties> queryDatabases(SqlQuerySpec querySpec, CosmosQueryRequestOptions options)

Query a Cosmos database.

CosmosPagedIterable<CosmosDatabaseProperties> queryDatabases(String query, CosmosQueryRequestOptions options)

Query a Cosmos database.

CosmosPagedIterable<CosmosDatabaseProperties> readAllDatabases()

Reads all Cosmos databases.

Methods inherited from java.lang.Object

Method Details

close

public void close()

Close this CosmosClient instance.

createDatabase

public CosmosDatabaseResponse createDatabase(CosmosDatabaseProperties databaseProperties)

Creates a Cosmos database.

CosmosDatabaseProperties databaseProperties = new CosmosDatabaseProperties(databaseName);
 cosmosClient.createDatabase(databaseProperties);

Parameters:

databaseProperties - CosmosDatabaseProperties the database properties.

Returns:

the CosmosDatabaseResponse with the created database.

createDatabase

public CosmosDatabaseResponse createDatabase(CosmosDatabaseProperties databaseProperties, CosmosDatabaseRequestOptions options)

Creates a database.

CosmosDatabaseProperties databaseProperties = new CosmosDatabaseProperties(databaseName);
 cosmosClient.createDatabase(databaseProperties);

Parameters:

databaseProperties - CosmosDatabaseProperties the database properties.
options - the request options.

Returns:

the CosmosDatabaseResponse with the created database.

createDatabase

public CosmosDatabaseResponse createDatabase(CosmosDatabaseProperties databaseProperties, ThroughputProperties throughputProperties)

Creates a Cosmos database.

ThroughputProperties throughputProperties = ThroughputProperties
     .createAutoscaledThroughput(autoScaleMaxThroughput);
 cosmosClient.createDatabase(databaseName, throughputProperties);

Parameters:

databaseProperties - CosmosDatabaseProperties the database properties.
throughputProperties - the throughput properties.

Returns:

the CosmosDatabaseResponse with the created database.

createDatabase

public CosmosDatabaseResponse createDatabase(CosmosDatabaseProperties databaseProperties, ThroughputProperties throughputProperties, CosmosDatabaseRequestOptions options)

Creates a Cosmos database.

ThroughputProperties throughputProperties = ThroughputProperties
     .createAutoscaledThroughput(autoScaleMaxThroughput);
 cosmosClient.createDatabase(databaseName, throughputProperties);

Parameters:

databaseProperties - CosmosDatabaseProperties the database properties.
throughputProperties - the throughput properties.
options - CosmosDatabaseRequestOptions the request options.

Returns:

the CosmosDatabaseResponse with the created database.

createDatabase

public CosmosDatabaseResponse createDatabase(String id)

Creates a Cosmos database.

CosmosDatabaseProperties databaseProperties = new CosmosDatabaseProperties(databaseName);
 cosmosClient.createDatabase(databaseProperties);

Parameters:

id - the id of the database.

Returns:

the CosmosDatabaseResponse with the created database.

createDatabase

public CosmosDatabaseResponse createDatabase(String id, ThroughputProperties throughputProperties)

Creates a Cosmos database.

ThroughputProperties throughputProperties = ThroughputProperties
     .createAutoscaledThroughput(autoScaleMaxThroughput);
 cosmosClient.createDatabase(databaseName, throughputProperties);

Parameters:

id - the id of the database.
throughputProperties - the throughput properties.

Returns:

the CosmosDatabaseResponse with the created database.

createDatabaseIfNotExists

public CosmosDatabaseResponse createDatabaseIfNotExists(String id)

Create a Cosmos database if it does not already exist on the service.

CosmosDatabaseProperties databaseProperties = new CosmosDatabaseProperties(databaseName);
 cosmosClient.createDatabaseIfNotExists(databaseProperties);

Parameters:

id - the id of the database.

Returns:

the CosmosDatabaseResponse with the created database.

createDatabaseIfNotExists

public CosmosDatabaseResponse createDatabaseIfNotExists(String id, ThroughputProperties throughputProperties)

Create a Cosmos database if it does not already exist on the service.

ThroughputProperties throughputProperties = ThroughputProperties
     .createAutoscaledThroughput(autoScaleMaxThroughput);
 cosmosClient.createDatabaseIfNotExists(databaseName, throughputProperties);

The throughputProperties will only be used if the specified database does not exist and therefor a new database will be created with throughputProperties.

Parameters:

id - the id of the database.
throughputProperties - the throughputProperties.

Returns:

the CosmosDatabaseResponse with the created database.

createGlobalThroughputControlConfigBuilder

public GlobalThroughputControlConfigBuilder createGlobalThroughputControlConfigBuilder(String databaseId, String containerId)

Create global throughput control config builder which will be used to build GlobalThroughputControlConfig.

Parameters:

databaseId - The database id of the control container.
containerId - The container id of the control container.

Returns:

getDatabase

public CosmosDatabase getDatabase(String id)

Gets the Cosmos database instance without making a service call.

Parameters:

id - the id of the database.

Returns:

CosmosDatabase the cosmos sync database.

queryDatabases

public CosmosPagedIterable queryDatabases(SqlQuerySpec querySpec, CosmosQueryRequestOptions options)

Query a Cosmos database.

CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
 CosmosPagedIterable<CosmosDatabaseProperties> databaseProperties =
     cosmosClient.queryDatabases("select * from d", options);
 databaseProperties.forEach(properties -> {
     System.out.println(properties.getId());
 });

Parameters:

querySpec - SqlQuerySpec the query spec.
options - the query request options.

Returns:

the CosmosPagedIterable<T> for feed response with the obtained databases.

queryDatabases

public CosmosPagedIterable queryDatabases(String query, CosmosQueryRequestOptions options)

Query a Cosmos database.

CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
 CosmosPagedIterable<CosmosDatabaseProperties> databaseProperties =
     cosmosClient.queryDatabases("select * from d", options);
 databaseProperties.forEach(properties -> {
     System.out.println(properties.getId());
 });

Parameters:

query - the query.
options - CosmosQueryRequestOptionsthe feed options.

Returns:

the CosmosPagedIterable<T> for feed response with the obtained databases.

readAllDatabases

public CosmosPagedIterable readAllDatabases()

Reads all Cosmos databases.

CosmosPagedIterable<CosmosDatabaseProperties> cosmosDatabaseProperties =
     cosmosClient.readAllDatabases();
 cosmosDatabaseProperties.forEach(databaseProperties -> {
     System.out.println(databaseProperties);
 });

Returns:

the CosmosPagedIterable<T> for feed response with the read databases.

Applies to