Skip to content
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Microsoft Azure Cosmos DB Sync Java SDK

This project provides a client tools or utilities in Java that makes it easy to interact with Azure CosmosDB. For documentation please see the Microsoft Azure Java Developer Center and the JavaDocs.


The implementation in this project is intended for reference purpose only.

Consuming the official Microsoft Azure DocumentDB Java SDK

To get the binaries of the latest official Microsoft Azure DocumentDB Java SDK as distributed by Microsoft, ready for use within your project, you can use Maven.



Version 2.4.4 have an important fix for the scenario where the SDK doesn't entertain partition split hint from server and results in incorrect client side routing caches refresh.

Minimum Requirements

  • Java Development Kit 7
  • (Optional) Maven


Dependencies will be added automatically if Maven is used. Otherwise, please download the dependencies from the pom.xml file and add them to your build path.


We have samples in form of small executable unit tests in documentdb-examples sub project.

  • Clone the Repo
git clone
cd azure-documentdb-java

You can run the samples either using Eclipse or from Command Line using Maven:


  • or you can simply put your endpoint credentials in
  • Now you can run the samples as JUnit tests in Eclipse.

Command line

The other way for running samples is to use maven:

  • Run Maven and pass your Azure Cosmos DB Endpoint credentials:


To use this SDK to call Azure DocumentDB, you need to first create an account.

You can follow this tutorial to help you get started.

import java.util.List;


public class HelloWorld {
    // Replace with your DocumentDB end point and master key.
    private static final String END_POINT = "[YOUR_ENDPOINT_HERE]";
    private static final String MASTER_KEY = "[YOUR_KEY_HERE]";
    // Define an id for your database and collection
    private static final String DATABASE_ID = "TestDB";
    private static final String COLLECTION_ID = "TestCollection";

    // We'll use Gson for POJO <=> JSON serialization for this sample.
    // Codehaus' Jackson is another great POJO <=> JSON serializer.
    private static Gson gson = new Gson();
    public static void main(String[] args) throws DocumentClientException,
            IOException {
        // Instantiate a DocumentClient w/ your DocumentDB Endpoint and AuthKey.
        DocumentClient documentClient = new DocumentClient(END_POINT,
                MASTER_KEY, ConnectionPolicy.GetDefault(),

        // Start from a clean state (delete database in case it already exists).
        try {
            documentClient.deleteDatabase("dbs/" + DATABASE_ID, null);
        catch (Exception e) {

        // Define a new database using the id above.
        Database myDatabase = new Database();

        // Create a new database.
        myDatabase = documentClient.createDatabase(myDatabase, null)

        System.out.println("Created a new database:");
        System.out.println("Press any key to continue..");;

        // Define a new collection using the id above.
        DocumentCollection myCollection = new DocumentCollection();

        // Set the provisioned throughput for this collection to be 1000 RUs.
        RequestOptions requestOptions = new RequestOptions();

        // Create a new collection.
        myCollection = documentClient.createCollection(
                "dbs/" + DATABASE_ID, myCollection, requestOptions)

        System.out.println("Created a new collection:");
        System.out.println("Press any key to continue..");;

        // Create an object, serialize it into JSON, and wrap it into a
        // document.
        SomePojo allenPojo = new SomePojo("123", "Allen Brewer", "allen [at]");
        String allenJson = gson.toJson(allenPojo);
        Document allenDocument = new Document(allenJson);

        // Create the 1st document.
        allenDocument = documentClient.createDocument(
                "dbs/" + DATABASE_ID + "/colls/" + COLLECTION_ID, allenDocument, null, false)

        System.out.println("Created 1st document:");
        System.out.println("Press any key to continue..");;

        // Create another object, serialize it into JSON, and wrap it into a
        // document.
        SomePojo lisaPojo = new SomePojo("456", "Lisa Andrews",
                "lisa [at]");
        String somePojoJson = gson.toJson(lisaPojo);
        Document lisaDocument = new Document(somePojoJson);

        // Create the 2nd document.
        lisaDocument = documentClient.createDocument(
                "dbs/" + DATABASE_ID + "/colls/" + COLLECTION_ID, lisaDocument, null, false)

        System.out.println("Created 2nd document:");
        System.out.println("Press any key to continue..");;

        // Query documents
        List<Document> results = documentClient
                        "dbs/" + DATABASE_ID + "/colls/" + COLLECTION_ID,
                        "SELECT * FROM myCollection WHERE = 'allen [at]'",

        System.out.println("Query document where e-mail address = 'allen [at]':");
        System.out.println("Press any key to continue..");;

        // Replace Document Allen with Percy
        allenPojo = gson.fromJson(results.get(0).toString(), SomePojo.class);
        allenPojo.setName("Percy Bowman");
        allenPojo.setEmail("Percy Bowman [at]");

        allenDocument = documentClient.replaceDocument(
                new Document(gson.toJson(allenPojo)), null)

        System.out.println("Replaced Allen's document with Percy's contact information");
        System.out.println("Press any key to continue..");;

        // Delete Percy's Document
        documentClient.deleteDocument(allenDocument.getSelfLink(), null);

        System.out.println("Deleted Percy's document");
        System.out.println("Press any key to continue..");;

        // Delete Database
        documentClient.deleteDatabase("dbs/" + DATABASE_ID, null);

        System.out.println("Deleted database");
        System.out.println("Press any key to continue..");;


The sample code above depends on a sample Plain Old Java Object (POJO) class, which is defined as follows:

class SomePojo {
    private String id;
    private String name;
    private String email;

    public SomePojo(String id, String name, String email) {
 = id;
 = name;
 = email;

    public String getEmail() {
          return email;

    public String getId() {
          return id;

    public String getName() {
          return name;

    public void setEmail(String email) {
 = email;

    public void setId(String id) {
 = id;

    public void setName(String name) {
 = name;

The following code Illustrates how to create a partitioned collection and use the partition key to access documents:

// Create a partition key definition that specifies the path to the property
// within a document that is used as the partition key.          
PartitionKeyDefinition partitionKeyDef = new PartitionKeyDefinition();
ArrayList<String> paths = new ArrayList<String>();

// Create a collection with the partition key definition and set the offer throughput
// to 10100 RU per second.
DocumentCollection myPartitionedCollection = new DocumentCollection();
RequestOptions options = new RequestOptions();
myPartitionedCollection = documentClient.createCollection(
    myDatabase.getSelfLink(), myCollection, options).getResource();

// Insert a document into the created collection.
String document = "{ 'id': 'document1', 'description': 'this is a test document.' }";
Document newDocument = new Document(document);
newDocument = documentClient.createDocument(myPartitionedCollection.getSelfLink(),
        newDocument, null, false).getResource();
 // Read the created document, specifying the required partition key in RequestOptions.
options = new RequestOptions();
options.setPartitionKey(new PartitionKey("document1"));
newDocument = documentClient.readDocument(newDocument.getSelfLink(), options).getResource();

Additional samples are provided in the unit tests.

Need Help?

Be sure to check out the Microsoft Azure Developer Forums on MSDN or the Developer Forums on Stack Overflow if you have trouble with the provided code.

Contribute Code or Provide Feedback

If you would like to become an active contributor to this project please follow the instructions provided in Azure Projects Contribution Guidelines.

If you encounter any bugs with the library please file an issue in the Issues section of the project.

Learn More

You can’t perform that action at this time.