CosmosDatabase Class
- java.
lang. Object - com.
azure. cosmos. CosmosDatabase
- com.
public class CosmosDatabase
Perform read and delete databases, update database throughput, and perform operations on child resources in a synchronous way
Method Summary
Methods inherited from java.lang.Object
Method Details
createContainer
public CosmosContainerResponse createContainer(CosmosContainerProperties containerProperties)
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:
Returns:
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:
Returns:
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:
Returns:
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:
Returns:
createContainer
public CosmosContainerResponse createContainer(String id, String partitionKeyPath)
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:
Returns:
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:
Returns:
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:
Returns:
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:
Returns:
createContainerIfNotExists
public CosmosContainerResponse createContainerIfNotExists(String id, String partitionKeyPath)
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:
Returns:
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:
Returns:
createUser
public CosmosUserResponse createUser(CosmosUserProperties userProperties)
Create Cosmos user.
CosmosUserProperties userProperties = new CosmosUserProperties();
userProperties.setId(userId);
cosmosDatabase.createUser(userProperties);
Parameters:
Returns:
delete
public CosmosDatabaseResponse delete()
Deletes the current Cosmos database.
CosmosDatabase cosmosDatabase = cosmosClient
.getDatabase("<YOUR DATABASE NAME>");
CosmosDatabaseResponse deleteResponse = cosmosDatabase.delete();
Returns:
delete
public CosmosDatabaseResponse delete(CosmosDatabaseRequestOptions options)
Delete the current Cosmos database while specifying additional request options.
CosmosDatabase cosmosDatabase = cosmosClient
.getDatabase("<YOUR DATABASE NAME>");
CosmosDatabaseResponse deleteResponse = cosmosDatabase.delete();
Parameters:
Returns:
getClientEncryptionKey
public CosmosClientEncryptionKey getClientEncryptionKey(String id)
Gets a CosmosClientEncryptionKey object without making a service call
Parameters:
Returns:
getContainer
public CosmosContainer getContainer(String id)
Gets a Cosmos container instance without making a service call.
To get the actual object a read operation must be performed first.
Parameters:
Returns:
getId
public String getId()
Get the id of the Cosmos database.
Returns:
getUser
public CosmosUser getUser(String id)
Gets a Cosmos user instance without making a service call.
To get the actual object a read operation must be performed first.
Parameters:
Returns:
queryContainers
public CosmosPagedIterable
Query containers in the current database.
CosmosPagedIterable<CosmosContainerProperties> cosmosContainersList =
cosmosDatabase.queryContainers("SELECT * FROM DB_NAME");
cosmosContainersList.forEach(cosmosContainerProperties -> {
System.out.println(cosmosContainerProperties);
});
Parameters:
Returns:
queryContainers
public CosmosPagedIterable
Query containers in the current database.
CosmosPagedIterable<CosmosContainerProperties> cosmosContainersList =
cosmosDatabase.queryContainers("SELECT * FROM DB_NAME");
cosmosContainersList.forEach(cosmosContainerProperties -> {
System.out.println(cosmosContainerProperties);
});
Parameters:
Returns:
queryContainers
public CosmosPagedIterable
Query containers in the current database.
CosmosPagedIterable<CosmosContainerProperties> cosmosContainersList =
cosmosDatabase.queryContainers("SELECT * FROM DB_NAME");
cosmosContainersList.forEach(cosmosContainerProperties -> {
System.out.println(cosmosContainerProperties);
});
Parameters:
Returns:
queryContainers
public CosmosPagedIterable
Query containers iterator.
CosmosPagedIterable<CosmosContainerProperties> cosmosContainersList =
cosmosDatabase.queryContainers("SELECT * FROM DB_NAME");
cosmosContainersList.forEach(cosmosContainerProperties -> {
System.out.println(cosmosContainerProperties);
});
Parameters:
Returns:
queryUsers
public CosmosPagedIterable
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:
Returns:
queryUsers
public CosmosPagedIterable
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:
Returns:
queryUsers
public CosmosPagedIterable
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:
Returns:
queryUsers
public CosmosPagedIterable
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:
Returns:
read
public CosmosDatabaseResponse 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:
read
public CosmosDatabaseResponse read(CosmosDatabaseRequestOptions options)
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:
Returns:
readAllClientEncryptionKeys
public CosmosPagedIterable
Reads all cosmos client encryption keys in a database.
CosmosPagedIterable<CosmosClientEncryptionKeyProperties> clientEncryptionKeys =
cosmosDatabase.readAllClientEncryptionKeys();
clientEncryptionKeys.forEach(encryptionKeyProperties ->
System.out.println(clientEncryptionKeys)
);
Returns:
readAllContainers
public CosmosPagedIterable
Read all containers in the current database.
CosmosPagedIterable<CosmosContainerProperties> cosmosContainersList =
cosmosDatabase.readAllContainers();
cosmosContainersList.forEach(cosmosContainerProperties -> {
System.out.println(cosmosContainerProperties);
});
Returns:
readAllUsers
public CosmosPagedIterable
Read all Cosmos users for the current database.
CosmosPagedIterable<CosmosUserProperties> cosmosUserProperties = cosmosDatabase.readAllUsers();
cosmosUserProperties.forEach(userProperties -> {
System.out.println(userProperties);
});
Returns:
readThroughput
public ThroughputResponse readThroughput()
Gets the throughput of the current database.
ThroughputResponse throughputResponse = cosmosDatabase.readThroughput();
System.out.println(throughputResponse);
Returns:
replaceThroughput
public ThroughputResponse replaceThroughput(ThroughputProperties throughputProperties)
Sets the throughput of the current database.
ThroughputProperties throughputProperties = ThroughputProperties
.createAutoscaledThroughput(autoScaleMaxThroughput);
ThroughputResponse throughputResponse = cosmosDatabase.replaceThroughput(throughputProperties);
System.out.println(throughputResponse);
Parameters:
Returns:
upsertUser
public CosmosUserResponse upsertUser(CosmosUserProperties userProperties)
Upserts a Cosmos user.
CosmosUserProperties userProperties = new CosmosUserProperties();
userProperties.setId(userId);
cosmosDatabase.upsertUser(userProperties);
Parameters:
Returns:
Applies to
Azure SDK for Java