Cassandra API Overview
The Cassandra API in Azure Cosmos DB provides wire protocol compatibility with Apache Cassandra™. You can use existing Cassandra drivers, tools, and applications with Cosmos DB without code changes.
Getting Started
Use the Azure portal or Azure CLI to create a Cosmos DB account with Cassandra API enabled.
Azure Portal
Azure CLI
1. Sign in to the Azure portal.
2. Click **Create a resource** → **Databases** → **Azure Cosmos DB**.
3. In the **API** dropdown, select **Cassandra**.
4. Fill in the required fields (Resource Group, Account Name, Region).
5. Click **Review + create**, then **Create**.
az group create --name MyResourceGroup --location eastus
az cosmosdb create \
--name MyCassandraAccount \
--resource-group MyResourceGroup \
--kind GlobalDocumentDB \
--capabilities EnableCassandra
Sample CQL Queries
Connect using the Cassandra driver of your choice. Below is a Java example using the DataStax driver.
import com.datastax.oss.driver.api.core.CqlSession;
import java.net.InetSocketAddress;
public class CosmosCassandraDemo {
public static void main(String[] args) {
try (CqlSession session = CqlSession.builder()
.addContactPoint(new InetSocketAddress(".documents.azure.com", 10350))
.withAuthCredentials("", "")
.withLocalDatacenter("datacenter1")
.build()) {
session.execute("CREATE KEYSPACE IF NOT EXISTS demo WITH replication = {'class':'SimpleStrategy','replication_factor':1}");
session.execute("CREATE TABLE IF NOT EXISTS demo.users (id uuid PRIMARY KEY, name text, age int)");
session.execute("INSERT INTO demo.users (id, name, age) VALUES (uuid(), 'Alice', 30)");
session.execute("SELECT * FROM demo.users").forEach(row -> {
System.out.println(row.getUuid("id") + " | " + row.getString("name") + " | " + row.getInt("age"));
});
}
}
}
Key Concepts
- Keyspaces & Tables: Direct equivalents to Cosmos DB containers.
- Throughput: Provisioned RU/s applies to the entire account; you can also use autoscale.
- Consistency Levels: Strong, Bounded Staleness, Session, Consistent Prefix, and Eventual are supported.
- Indexes: Primary key and secondary indexes work as in Cassandra.
- Security: TLS, IP firewall, and Azure AD integration available.