0
0
AzureConceptBeginner · 4 min read

Azure Cosmos DB API Types: Overview and Usage

Azure Cosmos DB supports multiple API types to work with different data models and protocols. These include SQL API for document databases, MongoDB API for MongoDB workloads, Cassandra API for wide-column stores, Gremlin API for graph databases, and Table API for key-value storage.
⚙️

How It Works

Think of Azure Cosmos DB as a versatile storage house that can speak different languages depending on what you need. Each API type is like a translator that lets you interact with the database using familiar commands and data formats.

For example, if you are used to working with JSON documents, the SQL API lets you query and manage data using SQL-like syntax. If you come from a MongoDB background, the MongoDB API allows you to use MongoDB drivers and tools seamlessly. Similarly, the Cassandra API supports wide-column data models, the Gremlin API is designed for graph data, and the Table API works like a simple key-value store.

This flexibility means you can choose the API that best fits your application's data model and existing tools, while still benefiting from Cosmos DB's global distribution, scalability, and low latency.

💻

Example

This example shows how to create a Cosmos DB container using the SQL API with Azure SDK for JavaScript. It creates a database and a container to store JSON documents.

javascript
import { CosmosClient } from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com:443/";
const key = "your-primary-key";
const client = new CosmosClient({ endpoint, key });

async function createDatabaseAndContainer() {
  const { database } = await client.databases.createIfNotExists({ id: "MyDatabase" });
  const { container } = await database.containers.createIfNotExists({ id: "MyContainer", partitionKey: { kind: "Hash", paths: ["/category"] } });
  console.log("Database and container created successfully.");
}

createDatabaseAndContainer().catch(console.error);
Output
Database and container created successfully.
🎯

When to Use

Choose the API type based on your application's data model and existing ecosystem:

  • SQL API: Use for JSON document storage with rich query capabilities.
  • MongoDB API: Ideal if you want to migrate or build apps using MongoDB drivers and tools.
  • Cassandra API: Best for wide-column data models and Cassandra workloads.
  • Gremlin API: Use for graph-based data like social networks or recommendation engines.
  • Table API: Suitable for simple key-value or table storage scenarios.

This helps you leverage Cosmos DB's global scale and performance while using familiar APIs.

Key Points

  • Cosmos DB supports multiple APIs to fit different data models.
  • Each API lets you use familiar tools and query languages.
  • APIs include SQL, MongoDB, Cassandra, Gremlin, and Table.
  • Choosing the right API depends on your app's data and ecosystem.
  • All APIs benefit from Cosmos DB's global distribution and low latency.

Key Takeaways

Azure Cosmos DB offers multiple API types to support different data models and tools.
Use SQL API for JSON documents and rich queries.
MongoDB API is for apps using MongoDB drivers and syntax.
Cassandra API supports wide-column data models.
Gremlin API is designed for graph data, and Table API for key-value storage.