How to use Spring Data with Azure Cosmos DB for Apache Cassandra
This article demonstrates creating a sample application that uses Spring Data to store and retrieve information using Azure Cosmos DB for Apache Cassandra.
Prerequisites
An Azure subscription - create one for free.
Java Development Kit (JDK), version 8 or higher.
Create an Azure Cosmos DB account
The following procedure creates and configures an Azure Cosmos DB account in the Azure portal.
Create an Azure Cosmos DB account using the Azure portal
Note
You can read more detailed information about creating accounts in the 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 Azure Cosmos DB account; this name 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've entered all of the above information, click Review + create.
If everything looks correct on the review page, click Create.
It takes 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 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 created.
Select Connection strings, and copy the values for the Contact Point, Port, Username, and Primary Password fields; you'll 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.git
Locate the application.properties file in the resources directory of the sample project, or create the file if it doesn't 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-points
Specifies the Contact Point from earlier in this article. spring.data.cassandra.port
Specifies the Port from earlier in this article. spring.data.cassandra.username
Specifies your Username from earlier in this article. spring.data.cassandra.password
Specifies 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.xml file to build and test the application.
Build the sample application with Maven; for example:
mvn clean package
Start the sample application; for example:
java -jar target/spring-data-cassandra-on-azure-0.1.0-SNAPSHOT.jar
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 example:
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
curl
from a command prompt like the following examples:curl -s http://localhost:8080/pets
Your application should return values like the following examples:
[{"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 Azure Cosmos DB for Apache Cassandra.
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.
See also
For more information about using Azure with Java, see the Azure for Java Developers and the Working with Azure DevOps and Java.