This guide demonstrates how to create a database within your Azure Cosmos DB account using the official Java SDK. Databases are logical containers for collections and are used to group related data.
You'll need to instantiate a CosmosClient object. This requires your Cosmos DB account's endpoint URI and primary key.
// Replace with your actual endpoint and key
String endpoint = "YOUR_COSMOS_DB_ENDPOINT";
String key = "YOUR_COSMOS_DB_PRIMARY_KEY";
// Instantiate a CosmosClient
CosmosClient client = new CosmosClientBuilder()
.endpoint(endpoint)
.key(key)
.buildClient();
Access the database API through the CosmosClient object.
// Get the database API
CosmosDatabaseApi databaseApi = client.getDatabaseApi();
Choose a unique name for your new database. Database IDs must be between 1 and 64 characters long and can contain lowercase letters, numbers, or hyphens. They cannot end with a hyphen.
String newDatabaseId = "MyNewDatabase";
Use the createDatabase method to provision the database. You can also specify throughput settings at this stage if needed (though it's more common to set throughput at the collection/container level).
// Create the database
// For dedicated capacity, you can specify throughput here.
// For serverless, omit throughput.
CosmosDatabaseProperties databaseProperties = new CosmosDatabaseProperties(newDatabaseId);
CosmosDatabaseResponse response = databaseApi.createDatabase(databaseProperties)
.block(); // Use .block() for synchronous operations or adapt for reactive streams
System.out.println("Database created with ID: " + response.properties().id());
.block() call and handle the Mono<CosmosDatabaseResponse> asynchronously.
Check the response for success and ensure you close the client when your application terminates.
// Example of checking response and closing client (simplified)
if (response != null && response.statusCode() == 201) {
System.out.println("Successfully created database: " + newDatabaseId);
}
// Close the client when your application is done with it
client.close();
createDatabase will return an error. You might want to check for existence first using readDatabase or handle the exception appropriately.
Here's a consolidated example:
import com.azure.cosmos.*;
import com.azure.cosmos.models.CosmosDatabaseProperties;
import com.azure.cosmos.models.CosmosDatabaseResponse;
public class CreateCosmosDatabase {
public static void main(String[] args) {
String endpoint = "YOUR_COSMOS_DB_ENDPOINT"; // Replace with your endpoint
String key = "YOUR_COSMOS_DB_PRIMARY_KEY"; // Replace with your key
String databaseId = "MyNewJavaDatabase";
CosmosClient client = null;
try {
// Initialize Cosmos Client
client = new CosmosClientBuilder()
.endpoint(endpoint)
.key(key)
.buildClient();
System.out.println("Attempting to create database: " + databaseId);
// Define database properties
CosmosDatabaseProperties databaseProperties = new CosmosDatabaseProperties(databaseId);
// Create the database
CosmosDatabaseResponse response = client.getDatabaseApi().createDatabase(databaseProperties).block();
if (response != null && response.statusCode() == 201) {
System.out.println("Successfully created database with ID: " + response.properties().id());
} else {
System.err.println("Failed to create database. Status code: " + (response != null ? response.statusCode() : "N/A"));
}
} catch (CosmosException ce) {
System.err.println("CosmosException caught: " + ce.getStatusCode() + " - " + ce.getMessage());
} catch (Exception e) {
System.err.println("General exception caught: " + e.getMessage());
e.printStackTrace();
} finally {
if (client != null) {
client.close();
System.out.println("Cosmos client closed.");
}
}
}
}