Integrate Java Apps with Azure Cosmos DB
Skill for Azure Cosmos DB's Java SDK: client setup, CRUD/query patterns, partition keys, consistency levels, and RU management.
Why it matters
Leverage the Azure Cosmos DB SDK for Java to build and manage globally distributed, highly available NoSQL databases. This asset facilitates seamless integration for Java applications, enabling efficient data operations and robust performance.
Outcomes
What it gets done
Connect to Azure Cosmos DB using key-based authentication.
Perform CRUD operations on Cosmos DB containers and items.
Query documents using SQL syntax with the Java SDK.
Implement asynchronous operations for high-throughput scenarios.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/ag-azure-cosmos-java | bash Overview
Azure Cosmos DB SDK for Java
A skill for Azure Cosmos DB's Java NoSQL SDK, covering client setup, CRUD/query patterns, partition key strategy, consistency levels, and RU-aware error handling. Use when building Java applications on Cosmos DB's NoSQL API; not for Cosmos DB's MongoDB/Cassandra/Gremlin APIs.
What it does
Azure Cosmos DB SDK for Java is a skill for the azure-cosmos client library covering Cosmos DB's NoSQL API, global distribution, and reactive programming patterns. It's installed via Maven (com.azure:azure-cosmos, or through the Azure SDK BOM for managed versioning), authenticated with COSMOS_ENDPOINT/COSMOS_KEY environment variables:
import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosClientBuilder;
CosmosClient client = new CosmosClientBuilder()
.endpoint(System.getenv("COSMOS_ENDPOINT"))
.key(System.getenv("COSMOS_KEY"))
.buildClient();
When to use - and when NOT to
Use it when building on Cosmos DB's NoSQL API in Java, whether synchronously (CosmosClient) or asynchronously (CosmosAsyncClient) via reactive chaining. The client can be customized with directMode/gatewayConnectionConfig, a ConsistencyLevel, connection sharing across clients, content-response-on-write, a user-agent suffix, and preferredRegions for geo-distributed apps. The client hierarchy follows account (CosmosClient) to database (CosmosDatabase) to container/item (CosmosContainer) operations. It is not the tool for other Cosmos DB APIs (MongoDB, Cassandra, Gremlin) - this SDK targets the NoSQL API specifically.
Inputs and outputs
Core workflow: createDatabaseIfNotExists sets up the database, and createContainerIfNotExists (taking a partition key path like /partitionKey) sets up the container - on the async client these chain via flatMap/map/subscribe to fetch the resulting CosmosAsyncDatabase/CosmosAsyncContainer handles once creation completes. CRUD then runs through createItem, readItem, replaceItem, and deleteItem, each scoped by a PartitionKey, and on the async client these calls are chained together (create, then read the same item back, update a field, then delete) before a final .block(). Queries use queryItems with a SQL-like string (SELECT * FROM c WHERE c.status = @status) and CosmosQueryRequestOptions, returning a CosmosPagedIterable that can be iterated directly. Every operation consumes Request Units, readable via response.getRequestCharge(). Partition keys should be chosen for high cardinality, even data/request distribution, and frequent use in queries. Cosmos DB's five consistency levels - Strong, Bounded Staleness, Session, Consistent Prefix, and Eventual - trade off linearizability guarantees against latency and cost.
Integrations
Errors surface as CosmosException, exposing status code, message, and request charge; the skill specifically calls out handling HTTP 409 (item already exists) and 429 (rate limited, with a getRetryAfterDuration() for backoff, though retry policies are built in by default). Its best practices: create and reuse one CosmosClient for the application's lifetime rather than per-operation, prefer the async client for high-throughput scenarios, enable content-response-on-write for immediate access to created items, configure preferred regions for geo-distributed deployments, and use direct mode for the lowest production latency.
Who it's for
Java developers building on Cosmos DB's NoSQL API who need the right client configuration (sync vs async, consistency level, regions), partition-key strategy, and RU-aware error handling for production workloads.
FAQ
Common questions
Discussion
Questions & comments ยท 0
Sign In Sign in to leave a comment.