Package com.azure.cosmos
Class CosmosDatabase
java.lang.Object
com.azure.cosmos.CosmosDatabase
Perform read and delete databases, update database throughput, and perform operations on child resources in
a synchronous way
-
Method Summary
Modifier and TypeMethodDescriptioncreateContainer
(CosmosContainerProperties containerProperties) Creates a Cosmos container.createContainer
(CosmosContainerProperties containerProperties, CosmosContainerRequestOptions options) Creates a Cosmos container while passing additional request options.createContainer
(CosmosContainerProperties containerProperties, ThroughputProperties throughputProperties) Creates a Cosmos container with custom throughput setting.createContainer
(CosmosContainerProperties containerProperties, ThroughputProperties throughputProperties, CosmosContainerRequestOptions options) Creates a Cosmos container.createContainer
(String id, String partitionKeyPath) Create a Cosmos container.createContainer
(String id, String partitionKeyPath, ThroughputProperties throughputProperties) Create a Cosmos container.createContainerIfNotExists
(CosmosContainerProperties containerProperties) Create container if one matching the id in the properties object does not exist.createContainerIfNotExists
(CosmosContainerProperties containerProperties, ThroughputProperties throughputProperties) Creates a Cosmos container if one matching the id in the properties object does not exist.createContainerIfNotExists
(String id, String partitionKeyPath) Creates a Cosmos container if one matching the id does not exist.createContainerIfNotExists
(String id, String partitionKeyPath, ThroughputProperties throughputProperties) Creates a Cosmos container if one matching the id does not exist.createUser
(CosmosUserProperties userProperties) Create Cosmos user.delete()
Deletes the current Cosmos database.delete
(CosmosDatabaseRequestOptions options) Delete the current Cosmos database while specifying additional request options.Gets a CosmosClientEncryptionKey object without making a service callgetContainer
(String id) Gets a Cosmos container instance without making a service call.getId()
Get the id of the Cosmos database.Gets a Cosmos user instance without making a service call.queryContainers
(SqlQuerySpec querySpec) Query containers in the current database.queryContainers
(SqlQuerySpec querySpec, CosmosQueryRequestOptions options) Query containers in the current database.queryContainers
(String query) Query containers in the current database.queryContainers
(String query, CosmosQueryRequestOptions options) Query containers iterator.queryUsers
(SqlQuerySpec querySpec) Query all Cosmos users for the current database.queryUsers
(SqlQuerySpec querySpec, CosmosQueryRequestOptions options) Query all Cosmos users for the current database.queryUsers
(String query) Query all Cosmos users for the current database.queryUsers
(String query, CosmosQueryRequestOptions options) Query all Cosmos users for the current database.read()
Reads the current Cosmos database.read
(CosmosDatabaseRequestOptions options) Reads the current Cosmos database while specifying additional request options.Reads all cosmos client encryption keys in a database.Read all containers in the current database.Read all Cosmos users for the current database.Gets the throughput of the current database.replaceThroughput
(ThroughputProperties throughputProperties) Sets the throughput of the current database.upsertUser
(CosmosUserProperties userProperties) Upserts a Cosmos user.
-
Method Details
-
getId
Get the id of the Cosmos database.- Returns:
- the id of the database.
-
read
Reads the current Cosmos database. Fetch the details and properties of a database based on its unique identifier.CosmosDatabase cosmosDatabase = cosmosClient .getDatabase("<YOUR DATABASE NAME>"); CosmosDatabaseResponse readResponse = cosmosDatabase.read();
- Returns:
- the
CosmosDatabaseResponse
.
-
read
Reads the current Cosmos database while specifying additional request options. Fetch the details and properties of a database based on its unique identifier.CosmosDatabase cosmosDatabase = cosmosClient .getDatabase("<YOUR DATABASE NAME>"); CosmosDatabaseResponse readResponse = cosmosDatabase.read();
- Parameters:
options
- theCosmosDatabaseRequestOptions
request options.- Returns:
- the
CosmosDatabaseResponse
-
delete
Deletes the current Cosmos database.CosmosDatabase cosmosDatabase = cosmosClient .getDatabase("<YOUR DATABASE NAME>"); CosmosDatabaseResponse deleteResponse = cosmosDatabase.delete();
- Returns:
- the
CosmosDatabaseResponse
.
-
delete
Delete the current Cosmos database while specifying additional request options.CosmosDatabase cosmosDatabase = cosmosClient .getDatabase("<YOUR DATABASE NAME>"); CosmosDatabaseResponse deleteResponse = cosmosDatabase.delete();
- Parameters:
options
- theCosmosDatabaseRequestOptions
request options.- Returns:
- the
CosmosDatabaseResponse
.
-
createContainer
Creates a Cosmos container.CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerId, partitionKeyDefinition); try { CosmosContainerResponse container = cosmosDatabase.createContainer(containerProperties); } catch (CosmosException ce) { System.out.println("Failed to create container: " + ce); }
- Parameters:
containerProperties
- theCosmosContainerProperties
.- Returns:
- the
CosmosContainerResponse
with the created container. - Throws:
CosmosException
- if resource with specified id already exists
-
createContainer
public CosmosContainerResponse createContainer(CosmosContainerProperties containerProperties, ThroughputProperties throughputProperties) Creates a Cosmos container with custom throughput setting.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); }
- Parameters:
containerProperties
- theCosmosContainerProperties
.throughputProperties
- the throughput properties.- Returns:
- the
CosmosContainerResponse
with the created container. - Throws:
CosmosException
- if resource with specified id already exists
-
createContainer
public CosmosContainerResponse createContainer(CosmosContainerProperties containerProperties, CosmosContainerRequestOptions options) Creates a Cosmos container while passing additional request options.CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerId, partitionKeyDefinition); try { CosmosContainerResponse container = cosmosDatabase.createContainer(containerProperties); } catch (CosmosException ce) { System.out.println("Failed to create container: " + ce); }
- Parameters:
containerProperties
- theCosmosContainerProperties
.options
- theCosmosContainerProperties
.- Returns:
- the
CosmosContainerResponse
with the created container. - Throws:
CosmosException
- if resource with specified id already exists
-
createContainer
public CosmosContainerResponse createContainer(CosmosContainerProperties containerProperties, ThroughputProperties throughputProperties, CosmosContainerRequestOptions options) Creates a Cosmos container.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); }
- Parameters:
containerProperties
- the container properties.throughputProperties
- the throughput properties.options
- the options.- Returns:
- the cosmos container response.
- Throws:
CosmosException
- if resource with specified id already exists
-
createContainer
Create a Cosmos container.ThroughputProperties throughputProperties = ThroughputProperties.createAutoscaledThroughput(autoscaledThroughput); try { CosmosContainerResponse container = cosmosDatabase.createContainer( containerId, partitionKeyPath, throughputProperties ); } catch (CosmosException ce) { System.out.println("Failed to create container: " + ce); }
- Parameters:
id
- the container id.partitionKeyPath
- the partition key path.- Returns:
- the cosmos container response.
- Throws:
CosmosException
- if resource with specified id already exists
-
createContainer
public CosmosContainerResponse createContainer(String id, String partitionKeyPath, ThroughputProperties throughputProperties) Create a Cosmos container.ThroughputProperties throughputProperties = ThroughputProperties.createAutoscaledThroughput(autoscaledThroughput); try { CosmosContainerResponse container = cosmosDatabase.createContainer( containerId, partitionKeyPath, throughputProperties ); } catch (CosmosException ce) { System.out.println("Failed to create container: " + ce); }
- Parameters:
id
- the id.partitionKeyPath
- the partition key path.throughputProperties
- the throughput properties.- Returns:
- the cosmos container response.
- Throws:
CosmosException
- if resource with specified id already exists
-
createContainerIfNotExists
public CosmosContainerResponse createContainerIfNotExists(CosmosContainerProperties containerProperties) Create container if one matching the id in the properties object does not exist.CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerId, partitionKeyDefinition); CosmosContainerResponse container = cosmosDatabase.createContainerIfNotExists(containerProperties);
- Parameters:
containerProperties
- the container properties.- Returns:
- the cosmos container response.
-
createContainerIfNotExists
public CosmosContainerResponse createContainerIfNotExists(CosmosContainerProperties containerProperties, ThroughputProperties throughputProperties) Creates a Cosmos container if one matching the id in the properties object does not exist.CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerId, partitionKeyDefinition); ThroughputProperties throughputProperties = ThroughputProperties.createAutoscaledThroughput(autoScaleMaxThroughput); CosmosContainerResponse container = cosmosDatabase.createContainerIfNotExists( containerProperties, throughputProperties );
The throughput properties will only be used if the specified container does not exist and therefor a new container will be created.- Parameters:
containerProperties
- the container properties.throughputProperties
- the throughput properties for the container.- Returns:
- the cosmos container response.
-
createContainerIfNotExists
Creates a Cosmos container if one matching the id does not exist.ThroughputProperties throughputProperties = ThroughputProperties.createAutoscaledThroughput(autoscaledThroughput); CosmosContainerResponse container = cosmosDatabase.createContainerIfNotExists( containerId, partitionKeyPath, throughputProperties );
- Parameters:
id
- the id.partitionKeyPath
- the partition key path.- Returns:
- the cosmos container response.
-
createContainerIfNotExists
public CosmosContainerResponse createContainerIfNotExists(String id, String partitionKeyPath, ThroughputProperties throughputProperties) Creates a Cosmos container if one matching the id does not exist.ThroughputProperties throughputProperties = ThroughputProperties.createAutoscaledThroughput(autoscaledThroughput); CosmosContainerResponse container = cosmosDatabase.createContainerIfNotExists( containerId, partitionKeyPath, throughputProperties );
The throughput properties will only be used if the specified container does not exist and therefor a new container will be created.- Parameters:
id
- the id.partitionKeyPath
- the partition key path.throughputProperties
- the throughput properties for the container.- Returns:
- the cosmos container response.
-
readAllContainers
Read all containers in the current database.CosmosPagedIterable<CosmosContainerProperties> cosmosContainersList = cosmosDatabase.readAllContainers(); cosmosContainersList.forEach(cosmosContainerProperties -> { System.out.println(cosmosContainerProperties); });
- Returns:
- the
CosmosPagedIterable
.
-
queryContainers
Query containers in the current database.CosmosPagedIterable<CosmosContainerProperties> cosmosContainersList = cosmosDatabase.queryContainers("SELECT * FROM DB_NAME"); cosmosContainersList.forEach(cosmosContainerProperties -> { System.out.println(cosmosContainerProperties); });
- Parameters:
query
- the query.- Returns:
- the
CosmosPagedIterable
.
-
queryContainers
public CosmosPagedIterable<CosmosContainerProperties> queryContainers(String query, CosmosQueryRequestOptions options) Query containers iterator.CosmosPagedIterable<CosmosContainerProperties> cosmosContainersList = cosmosDatabase.queryContainers("SELECT * FROM DB_NAME"); cosmosContainersList.forEach(cosmosContainerProperties -> { System.out.println(cosmosContainerProperties); });
- Parameters:
query
- the query.options
- the options.- Returns:
- the
CosmosPagedIterable
.
-
queryContainers
Query containers in the current database.CosmosPagedIterable<CosmosContainerProperties> cosmosContainersList = cosmosDatabase.queryContainers("SELECT * FROM DB_NAME"); cosmosContainersList.forEach(cosmosContainerProperties -> { System.out.println(cosmosContainerProperties); });
- Parameters:
querySpec
- the query spec.- Returns:
- the
CosmosPagedIterable
.
-
queryContainers
public CosmosPagedIterable<CosmosContainerProperties> queryContainers(SqlQuerySpec querySpec, CosmosQueryRequestOptions options) Query containers in the current database.CosmosPagedIterable<CosmosContainerProperties> cosmosContainersList = cosmosDatabase.queryContainers("SELECT * FROM DB_NAME"); cosmosContainersList.forEach(cosmosContainerProperties -> { System.out.println(cosmosContainerProperties); });
- Parameters:
querySpec
- the query spec.options
- the options.- Returns:
- the
CosmosPagedIterable
.
-
getContainer
Gets a Cosmos container instance without making a service call.To get the actual object a read operation must be performed first.
- Parameters:
id
- id of the container.- Returns:
- Cosmos Container.
-
createUser
Create Cosmos user.CosmosUserProperties userProperties = new CosmosUserProperties(); userProperties.setId(userId); cosmosDatabase.createUser(userProperties);
- Parameters:
userProperties
- the settings.- Returns:
- the cosmos user response.
-
upsertUser
Upserts a Cosmos user.CosmosUserProperties userProperties = new CosmosUserProperties(); userProperties.setId(userId); cosmosDatabase.upsertUser(userProperties);
- Parameters:
userProperties
- the settings.- Returns:
- the cosmos user response.
-
readAllUsers
Read all Cosmos users for the current database.CosmosPagedIterable<CosmosUserProperties> cosmosUserProperties = cosmosDatabase.readAllUsers(); cosmosUserProperties.forEach(userProperties -> { System.out.println(userProperties); });
- Returns:
- the
CosmosPagedIterable
.
-
queryUsers
Query all Cosmos users for the current database.CosmosPagedIterable<CosmosUserProperties> userPropertiesList = cosmosDatabase.queryUsers("SELECT * FROM DB_NAME"); userPropertiesList.forEach(userProperties -> { System.out.println(userProperties); });
- Parameters:
query
- the query.- Returns:
- the
CosmosPagedIterable
.
-
queryUsers
public CosmosPagedIterable<CosmosUserProperties> queryUsers(String query, CosmosQueryRequestOptions options) Query all Cosmos users for the current database.CosmosPagedIterable<CosmosUserProperties> userPropertiesList = cosmosDatabase.queryUsers("SELECT * FROM DB_NAME"); userPropertiesList.forEach(userProperties -> { System.out.println(userProperties); });
- Parameters:
query
- the query.options
- the options.- Returns:
- the
CosmosPagedIterable
.
-
queryUsers
Query all Cosmos users for the current database.CosmosPagedIterable<CosmosUserProperties> userPropertiesList = cosmosDatabase.queryUsers("SELECT * FROM DB_NAME"); userPropertiesList.forEach(userProperties -> { System.out.println(userProperties); });
- Parameters:
querySpec
- the query spec.- Returns:
- the
CosmosPagedIterable
.
-
queryUsers
public CosmosPagedIterable<CosmosUserProperties> queryUsers(SqlQuerySpec querySpec, CosmosQueryRequestOptions options) Query all Cosmos users for the current database.CosmosPagedIterable<CosmosUserProperties> userPropertiesList = cosmosDatabase.queryUsers("SELECT * FROM DB_NAME"); userPropertiesList.forEach(userProperties -> { System.out.println(userProperties); });
- Parameters:
querySpec
- the query spec.options
- the options.- Returns:
- the
CosmosPagedIterable
.
-
getUser
Gets a Cosmos user instance without making a service call.To get the actual object a read operation must be performed first.
- Parameters:
id
- the id.- Returns:
- the user.
-
replaceThroughput
Sets the throughput of the current database.ThroughputProperties throughputProperties = ThroughputProperties .createAutoscaledThroughput(autoScaleMaxThroughput); ThroughputResponse throughputResponse = cosmosDatabase.replaceThroughput(throughputProperties); System.out.println(throughputResponse);
- Parameters:
throughputProperties
- the throughput properties.- Returns:
- the throughput response.
-
readThroughput
Gets the throughput of the current database.ThroughputResponse throughputResponse = cosmosDatabase.readThroughput(); System.out.println(throughputResponse);
- Returns:
- the throughput response.
-
getClientEncryptionKey
Gets a CosmosClientEncryptionKey object without making a service call- Parameters:
id
- id of the clientEncryptionKey- Returns:
- Cosmos ClientEncryptionKey
-
readAllClientEncryptionKeys
Reads all cosmos client encryption keys in a database.CosmosPagedIterable<CosmosClientEncryptionKeyProperties> clientEncryptionKeys = cosmosDatabase.readAllClientEncryptionKeys(); clientEncryptionKeys.forEach(encryptionKeyProperties -> System.out.println(clientEncryptionKeys) );
- Returns:
- a
CosmosPagedIterable
.
-