How to use Spring Data API for Apache Cassandra with Azure Cosmos DB for Apache Cassandra

APPLIES TO: Cassandra

This article demonstrates creating a sample application that uses Spring Data to store and retrieve information using the Azure Cosmos DB for Apache Cassandra.


The following prerequisites are required in order to complete the steps in this article:


The samples mentioned below implement custom extensions for a better experience when using Azure Cosmos DB for Apache Cassandra. They include custom retry and load balancing policies, as well as implementing recommended connection settings. For a more extensive exploration of how the custom policies are used, see Java samples for version 3 and version 4.

Create an Azure Cosmos DB for Apache Cassandra account

  1. From the Azure portal menu or the Home page, select Create a resource.

  2. On the New page, search for and select Azure Cosmos DB.

  3. On the Azure Cosmos DB page, select Create.

  4. In the Create Azure Cosmos DB Account page, enter the basic settings for the new Azure Cosmos DB account.

    Setting Value Description
    Subscription Your subscription Select the Azure subscription that you want to use for this Azure Cosmos DB account.
    Resource Group Create new

    Then enter the same name as Account Name
    Select Create new. Then enter a new resource group name for your account. For simplicity, use the same name as your Azure Cosmos DB account name.
    Account Name Enter a unique name Enter a unique name to identify your Azure Cosmos DB account. Your account URI will be appended to your unique account name.

    The account name can use only lowercase letters, numbers, and hyphens (-), and must be between 3 and 31 characters long.
    API Cassandra The API determines the type of account to create. Azure Cosmos DB provides five APIs: NoSQL for document databases, Gremlin for graph databases, MongoDB for document databases, Azure Table, and Cassandra. You must create a separate account for each API.

    Select Cassandra, because in this quickstart you are creating a table that works with the API for Cassandra.

    Learn more about the API for Cassandra.
    Location The region closest to your users Select a geographic location to host your Azure Cosmos DB account. Use the location that is closest to your users to give them the fastest access to the data.
    Capacity mode Provisioned throughput or Serverless Select Provisioned throughput to create an account in provisioned throughput mode. Select Serverless to create an account in serverless mode.
    Apply Azure Cosmos DB free tier discount Apply or Do not apply With Azure Cosmos DB free tier, you will get the first 1000 RU/s and 25 GB of storage for free in an account. Learn more about free tier.


    You can have up to one free tier Azure Cosmos DB account per Azure subscription and must opt-in when creating the account. If you do not see the option to apply the free tier discount, this means another account in the subscription has already been enabled with free tier.

    The new account page for Azure Cosmos DB for Apache Cassandra

  5. In the Global Distribution tab, configure the following details. You can leave the default values for the purpose of this quickstart:

    Setting Value Description
    Geo-Redundancy Disable Enable or disable global distribution on your account by pairing your region with a pair region. You can add more regions to your account later.
    Multi-region Writes Disable Multi-region writes capability allows you to take advantage of the provisioned throughput for your databases and containers across the globe.


    The following options are not available if you select Serverless as the Capacity mode:

    • Apply Free Tier Discount
    • Geo-redundancy
    • Multi-region Writes
  6. Optionally you can configure additional details in the following tabs:

    • Networking - Configure access from a virtual network.
    • Backup Policy - Configure either periodic or continuous backup policy.
    • Encryption - Use either service-managed key or a customer-managed key.
    • Tags - Tags are name/value pairs that enable you to categorize resources and view consolidated billing by applying the same tag to multiple resources and resource groups.
  7. Select Review + create.

  8. Review the account settings, and then select Create. It takes a few minutes to create the account. Wait for the portal page to display Your deployment is complete.

    The Azure portal Notifications pane

  9. Select Go to resource to go to the Azure Cosmos DB account page.

Configure the sample application

The following procedure configures the test application.

  1. Open a command shell and clone either of the following examples:

    For Java version 3 driver and corresponding Spring version:

    git clone

    For Java version 4 driver and corresponding Spring version:

    git clone


    Although the usage described below is identical for both Java version 3 and version 4 samples above, the way in which they have been implemented in order to include custom retry and load balancing policies is different. We recommend reviewing the code to understand how to implement custom policies if you are making changes to an existing Spring Java application.

  2. Locate the file in the resources directory of the sample project, or create the file if it does not already exist.

  3. Open the file in a text editor, and add or configure the following lines in the file, and replace the sample values with the appropriate values from earlier:<Account Name><Account Name>********


    Parameter Description Specifies the Contact Point from earlier in this article. Specifies the Port from earlier in this article. Specifies your Username from earlier in this article. Specifies your Primary Password from earlier in this article.
  4. Save and close the file.

Package and test the sample application

Browse to the directory that contains the .pom file to build and test the application.

  1. Build the sample application with Maven; for example:

    mvn clean package
  2. Start the sample application; for example:

    java -jar target/spring-data-cassandra-on-azure-0.1.0-SNAPSHOT.jar
  3. Create new records using curl from a command prompt like the following examples:

    curl -s -d "{\"name\":\"dog\",\"species\":\"canine\"}" -H "Content-Type: application/json" -X POST http://localhost:8080/pets
    curl -s -d "{\"name\":\"cat\",\"species\":\"feline\"}" -H "Content-Type: application/json" -X POST http://localhost:8080/pets

    Your application should return values like the following:

    Added Pet{id=60fa8cb0-0423-11e9-9a70-39311962166b, name='dog', species='canine'}.
    Added Pet{id=72c1c9e0-0423-11e9-9a70-39311962166b, name='cat', species='feline'}.
  4. Retrieve all of the existing records using curl from a command prompt like the following examples:

    curl -s http://localhost:8080/pets

    Your application should return values like the following:


Clean up resources

When you're done with your app and Azure Cosmos DB account, you can delete the Azure resources you created so you don't incur more charges. To delete the resources:

  1. In the Azure portal Search bar, search for and select Resource groups.

  2. From the list, select the resource group you created for this quickstart.

    Select the resource group to delete

  3. On the resource group Overview page, select Delete resource group.

    Delete the resource group

  4. In the next window, enter the name of the resource group to delete, and then select Delete.

Next steps

To learn more about Spring and Azure, continue to the Spring on Azure documentation center.

Additional Resources

For more information about using Azure with Java, see the Azure for Java Developers and the Working with Azure DevOps and Java.