March 9th, 2020

Azure Cosmos DB Java SDK v4 – New Java SDK Quickstart Guide and Sample Code!

Andy Feldman
Senior Program Manager

The Azure Cosmos DB Java SDK for SQL API (hereafter “Java SDK”) provides support for Java applications with Azure Cosmos DB.

Java SDK 4.0 General Availability is coming soon! To help customers prepare for this and take advantage of all the new great features, I’ll be writing a series of blog posts around this as well as including helpful samples. This is the first in the series, entitled Azure Cosmos DB Java SDK v4 in which I will connect you with docs and samples for Java SDK 4.0 and guide you along a learning path for developing a new Java application or migrating an existing Java application to this Java SDK version. If you are a new or current Java SDK user for Cosmos DB – this guide is for you 😊

The purpose of today’s post is to introduce (1) a Quickstart Guide for managing databases, containers and items with Java SDK 4.0 and (2) additional Sample Code for indexing, stored procedures and Change Feed. You may want to review databases, containers and items in Azure Cosmos DB, as well as indexing, stored procedures and the Azure Cosmos DB Change Feed in order to put these docs and samples in context. This is also a great time to review Azure Cosmos DB documentation on throughput provisioning and best-practices for data modeling and partitioning, which help you to build a performant and efficient application regardless of which API or SDK you are using to interact with Azure Cosmos DB containers.

A good starting-point to familiarize yourself with Java SDK 4.0 is to play around with the public preview release and get a handle on basics like setting up and accessing Azure Cosmos DB containers from your Java application. The Quickstart Guide starts with a walk through of setting up an Azure Cosmos DB account in the Azure Portal, and then follows up with code snippets for database, container, and item creation. Finally, there are code snippets showing how to make point reads and writes to a container using the Java SDK, and how to issue queries. Even better, the Quickstart Guide covers these topics for both synchronous and asynchronous programming – because unlike previous versions, Java SDK 4.0 bundles both Sync and Async APIs into the same SDK! The Quickstart Guide comes with a Getting Started Maven project that you can build and run, and which is the source of the code snippets referenced in the Guide. A quick look at pom.xml in the aforementioned project shows you how to reference the Java SDK 4.0 public preview Maven artifact in a Java application. Follow the instructions in the Quickstart Guide, and we expect that your application will be setting up and accessing Azure Cosmos DB containers in no time.

A quick note on Sync and Async APIs – the Java SDK 4.0 Async API is designed for a Reactive Programming model and employs Project Reactor (or just “Reactor”) as its underlying Reactive Programming framework. “Reactive Programming” and “Reactor” may strike you as unfamiliar terms, especially if you are a net-new user of the Java SDK or if you have been using the Legacy Sync Java SDK to interact with Azure Cosmos DB. If you are a user of the Async Java SDK 2.x.x then you may already be familiar with Reactive Programming and Reactive Extensions for the JVM (or just “RxJava”) which was the framework used in that version of the SDK, and which is not the same as Reactor. I will address all of these concerns in the next post in the series. For now, I recommend you to step through the Quickstart Guide and see first-hand that design patterns for carrying out database-, container-, and item-related tasks with Java SDK 4.0 differ between synchronous and asynchronous implementations.

When you are finished with the Quickstart Guide, head on over to GitHub and clone the Azure Cosmos DB Java SDK for SQL API samples repo so that you can take a look at the sample code for index management, stored procedures, and Change Feed with Java SDK 4.0. Or, feel free to simply browse the source code online. When you clone the samples repo you will find a Maven project containing the three aforementioned samples, which you may build and run individually. Once you have gone through the Quickstart Guide and Sample Code, it’s time to start developing your Java application – happy coding 😊

Check out the next post in the series, Azure Cosmos DB Java SDK v4 – Exploring the new Async API

Author

Andy Feldman
Senior Program Manager

4 comments

Discussion is closed. Login to edit/delete existing comments.

  • Oleg Burov

    Hello 👋, is there a plan to release Golang SDK for Cosmos DB?

    • Mark BrownMicrosoft employee

      This is not currently on our roadmap. However a Cosmos DB customer did implement their own GO SDK for Cosmos and released as open source on GitHub. https://github.com/vippsas/go-cosmosdb. This may work for your needs.

      Thanks.

  • Luis BosquezMicrosoft employee

    This is such a great blog post, can’t wait to use the Java SDK!