Delen via


Azure Cosmos DB libraries for Java

Overview

Store and query key-value, JSON document, graph, and columnar data in a globally distributed database with Azure Cosmos DB.

To get started with Azure Cosmos DB, see Azure Cosmos DB: Build an API app with Java and the Azure portal.

Client library

Connect to Azure Cosmos DB using the SQL API client library to work with JSON data with SQL query syntax.

Add a dependency to your Maven pom.xml file to use the Cosmos DB client library in your project.

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-documentdb</artifactId>
    <version>1.12.0</version>
</dependency>

Example

Select matching JSON documents in Cosmos DB using SQL query syntax.

DocumentClient client = new DocumentClient("https://contoso.documents.azure.com:443",
                "contosoCosmosDBKey", 
                new ConnectionPolicy(),
                ConsistencyLevel.Session);

List<Document> results = client.queryDocuments("dbs/" + DATABASE_ID + "/colls/" + COLLECTION_ID,
        "SELECT * FROM myCollection WHERE myCollection.email = 'allen [at] contoso.com'",
        null)
    .getQueryIterable()
    .toList();

Samples

Develop a Java app using Azure Cosmos DB MongoDB API
Develop a Java app using Azure Cosmos DB Graph API
Develop a Java app using Azure Cosmos DB SQL API

Explore more sample Java code for Azure Cosmos DB you can use in your apps.