How to use Spring Data Apache Cassandra API with Azure Cosmos DB
This article demonstrates creating a sample application that uses Spring Data to store and retrieve information using the Azure Cosmos DB Cassandra API.
Prerequisites
The following prerequisites are required in order to complete the steps in this article:
- An Azure subscription; if you don't already have an Azure subscription, you can activate your MSDN subscriber benefits or sign up for a free Azure account.
- A supported Java Development Kit (JDK). For more information about the JDKs available for use when developing on Azure, see Java support on Azure and Azure Stack.
- Apache Maven, version 3.0 or later.
- Curl or similar HTTP utility to test functionality.
- A Git client.
Create an Azure Cosmos DB account
The following procedure creates and configures a Cosmos account in the Azure portal.
Create a Cosmos DB account using the Azure portal
Note
You can read more detailed information about creating Azure Cosmos DB accounts in Azure Cosmos DB Documentation.
Browse to the Azure portal at https://portal.azure.com/ and sign in.
Select Create a resource, then Get started, and then select Azure Cosmos DB.

On the Select API option screen, select Cassandra.

Specify the following information:
- Subscription: Specify your Azure subscription to use.
- Resource group: Specify whether to create a new resource group, or choose an existing resource group.
- Account name: Choose a unique name for your Cosmos DB account; this will be used to create a fully-qualified domain name like wingtiptoyscassandra.documents.azure.com.
- API: Specify Cassandra for this tutorial.
- Location: Specify the closest geographic region for your database.

When you have entered all of the above information, click Review + create.
If everything looks correct on the review page, click Create.

It will take a few minutes to deploy the database.
Add a keyspace to your Azure Cosmos DB account
Browse to the Azure portal at https://portal.azure.com/ and sign in.
Select All Resources, then select the Azure Cosmos DB account you just created.
Select Data Explorer, select down arrow and select New Keyspace. Enter a unique identifier for your Keyspace id, then select OK.


Retrieve the connection settings for your Azure Cosmos DB account
Browse to the Azure portal at https://portal.azure.com/ and sign in.
Select All Resources, then select the Azure Cosmos DB account you just created.
Select Connection strings, and copy the values for the Contact Point, Port, Username, and Primary Password fields; you will use those values to configure your application later.

Configure the sample application
The following procedure configures the test application.
Open a command shell and clone the sample project using a git command like the following example:
git clone https://github.com/Azure-Samples/spring-data-cassandra-on-azure.gitLocate the application.properties file in the resources directory of the sample project, or create the file if it does not already exist.
Open the application.properties 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:
spring.data.cassandra.contact-points=wingtiptoyscassandra.cassandra.cosmos.azure.com spring.data.cassandra.port=10350 spring.data.cassandra.username=wingtiptoyscassandra spring.data.cassandra.password=********Where:
Parameter Description spring.data.cassandra.contact-pointsSpecifies the Contact Point from earlier in this article. spring.data.cassandra.portSpecifies the Port from earlier in this article. spring.data.cassandra.usernameSpecifies your Username from earlier in this article. spring.data.cassandra.passwordSpecifies your Primary Password from earlier in this article. Save and close the application.properties file.
Package and test the sample application
Browse to the directory that contains the .pom file to build and test the application.
Build the sample application with Maven; for example:
mvn clean packageStart the sample application; for example:
java -jar target/spring-data-cassandra-on-azure-0.1.0-SNAPSHOT.jarCreate new records using
curlfrom 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/petsYour 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'}.Retrieve all of the existing records using
curlfrom a command prompt like the following examples:curl -s http://localhost:8080/petsYour application should return values like the following:
[{"id":"60fa8cb0-0423-11e9-9a70-39311962166b","name":"dog","species":"canine"},{"id":"72c1c9e0-0423-11e9-9a70-39311962166b","name":"cat","species":"feline"}]
Summary
In this tutorial, you created a sample Java application that uses Spring Data to store and retrieve information using the Azure Cosmos DB Cassandra API.
Clean up resources
When no longer needed, use the Azure portal to delete the resources created in this article to avoid unexpected charges.
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.