CRUD Operations
Basic create, read, update and delete operations are performed through CosmosContainer. Below are common patterns.
Create Item▾
public void createItem(CosmosContainer container, MyItem item) { container.createItem(item, new PartitionKey(item.getCategory()), null); }
Read Item▾
public MyItem readItem(CosmosContainer container, String id, String partitionKey) { CosmosItemResponse<MyItem> response = container.readItem(id, new PartitionKey(partitionKey), MyItem.class); return response.getItem(); }
Replace (Update) Item▾
public MyItem replaceItem(CosmosContainer container, MyItem item) { CosmosItemResponse<MyItem> response = container.replaceItem(item, item.getId(), new PartitionKey(item.getCategory()), null); return response.getItem(); }
Delete Item▾
public void deleteItem(CosmosContainer container, String id, String partitionKey) { container.deleteItem(id, new PartitionKey(partitionKey), null); }
Querying Items
Use SQL API or LINQ to retrieve multiple items. The SDK provides CosmosPagedIterable for streaming results.
public List<MyItem> queryItems(CosmosContainer container, String category) { String sql = "SELECT * FROM c WHERE c.category = @cat"; CosmosQueryRequestOptions options = new CosmosQueryRequestOptions(); SqlParameter param = new SqlParameter("@cat", category); SqlQuerySpec querySpec = new SqlQuerySpec(sql, java.util.Collections.singletonList(param)); CosmosPagedIterable<MyItem> results = container.queryItems(querySpec, options, MyItem.class); return results.stream().collect(java.util.stream.Collectors.toList()); }
Transactional Batches
Execute multiple operations atomically within a partition key.
public void executeBatch(CosmosContainer container, String partitionKey) { TransactionalBatch batch = TransactionalBatch.createTransactionalBatch(new PartitionKey(partitionKey)); batch.createItem(new MyItem("1", "A")); batch.upsertItem(new MyItem("2", "B")); batch.deleteItem("3"); container.executeTransactionalBatch(batch); }
Throughput Management
Read and adjust RU/s or autoscale settings at container or database level.
public void replaceThroughput(CosmosContainer container, int ru) { ThroughputProperties props = ThroughputProperties.createManualThroughput(ru); container.replaceThroughput(props); }
Indexing Policies
Customize which paths are indexed and the indexing mode.
public void setIndexingPolicy(CosmosContainer container) { IndexingPolicy policy = new IndexingPolicy(); policy.setIndexingMode(IndexingMode.CONSISTENT); policy.setAutomatic(true); policy.setIncludedPaths(java.util.Collections.singletonList(new IncludedPath("/*"))); container.replaceContainerProperties(new CosmosContainerProperties(container.getId(), policy)); }
Consistency Levels
The client can be configured with a desired consistency level.
CosmosAsyncClient client = new CosmosClientBuilder() .endpoint("https://your-account.documents.azure.com:443") .key("YOUR_KEY") .consistencyLevel(ConsistencyLevel.SESSION) .buildAsyncClient();
Change Feed
Consume real‑time change feed using the SDK iterator.
public void readChangeFeed(CosmosContainer container) { CosmosChangeFeedRequestOptions options = new CosmosChangeFeedRequestOptions(); options.setStartFromBeginning(true); CosmosPagedIterable<MyItem> changeFeed = container.queryChangeFeed(options, MyItem.class); changeFeed.iterableByPage().forEach(page -> { page.getResults().forEach(item -> System.out.println(item)); }); }