Setting Up Azure Cosmos DB

This tutorial will guide you through the essential steps to set up and start using Azure Cosmos DB, a globally distributed, multi-model database service.

Prerequisites

Before you begin, ensure you have:

Step 1: Create an Azure Cosmos DB Account

Navigate to the Azure Portal

Open your web browser and go to portal.azure.com. Log in with your Azure account credentials.

Search for "Azure Cosmos DB"

In the search bar at the top of the portal, type "Azure Cosmos DB" and select it from the search results.

Create a Cosmos DB Account

Click the "Create" button. You will be presented with various configuration options:

  • Subscription: Select your Azure subscription.
  • Resource group: Create a new one or select an existing resource group.
  • Account Name: Choose a unique name for your Cosmos DB account.
  • API: Select the API that best suits your needs (e.g., Core (SQL), MongoDB, Cassandra, Gremlin, Table). For this tutorial, we'll use Core (SQL).
  • Location: Choose the Azure region closest to your users for lower latency.

Review the settings and click "Review + create", then "Create".

Step 2: Create a Database and Container

Once your Cosmos DB account is deployed, you need to create a database and a container within it.

Access Your Cosmos DB Account

Go to your newly created Azure Cosmos DB account in the Azure portal.

Create a Database

In the left-hand menu, under "Data Explorer", click "New Container". A form will appear to create a new database and container:

  • Database id: Enter a name for your database (e.g., `MyDatabase`).
  • Container id: Enter a name for your container (e.g., `MyCollection`).
  • Partition key: This is crucial for performance and scalability. Choose a property that distributes your data evenly. For the Core (SQL) API, a common choice is `/partitionKey`.

Click "OK".

Step 3: Accessing Connection Strings

To connect your application to Azure Cosmos DB, you'll need connection strings.

Retrieve Connection Strings

In your Cosmos DB account's left-hand menu, navigate to "Keys". Here you will find your Primary Key, Secondary Key, Primary Connection String, and Secondary Connection String. Copy the Primary Connection String for use in your applications.

Code Example: Connecting with Azure Cosmos DB SDK (Node.js)

Here's a simple example of how to connect using the Azure Cosmos DB Node.js SDK:

// Import the CosmosClient class const { CosmosClient } = require("@azure/cosmos"); // Your Cosmos DB endpoint and key const endpoint = "YOUR_COSMOS_DB_ENDPOINT"; // e.g., https://your-account-name.documents.azure.com:443/ const key = "YOUR_COSMOS_DB_PRIMARY_KEY"; // Initialize the CosmosClient const client = new CosmosClient({ endpoint, key }); // Database and container names const databaseId = "MyDatabase"; const containerId = "MyCollection"; async function setupCosmosDB() { console.log(`Connecting to database: ${databaseId}`); const { database } = await client.databases.createIfNotExists({ id: databaseId }); console.log(`Database "${database.id}" created or already exists.`); console.log(`Creating container: ${containerId}`); const { container } = await database.containers.createIfNotExists({ id: containerId, partitionKey: { kind: "Hash", paths: ["/partitionKey"] // Ensure this matches your container's partition key } }); console.log(`Container "${container.id}" created or already exists.`); console.log("Azure Cosmos DB setup complete!"); } setupCosmosDB().catch(error => { console.error("An error occurred:", error); });

Remember to replace YOUR_COSMOS_DB_ENDPOINT and YOUR_COSMOS_DB_PRIMARY_KEY with your actual credentials.

Next: Creating Your First Item