Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This article shows how to use Spring Data with Azure Cosmos DB for Apache Cassandra to create a sample Java application that stores and retrieves data.
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 by using the Azure portal
Note
For more information about creating accounts, see the Azure Cosmos DB documentation.
Browse to the Azure portal at https://portal.azure.com/ and sign in.
Select Create a resource, select 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 is 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 enter all of the preceding information, select Review + create.
If everything looks correct on the review page, select 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, and then select the Azure Cosmos DB account you created.
Select Data Explorer, select the down arrow, and select New Keyspace. Enter a unique identifier for your Keyspace id, and 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, and 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 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 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-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.xml 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 by using
curlfrom a command prompt. The following examples show how to create records: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 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 existing records by using
curlfrom a command prompt. The following examples show how to retrieve records:curl -s http://localhost:8080/petsYour 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 by using Azure Cosmos DB for Apache Cassandra.
Clean up resources
When you no longer need the resources, use the Azure portal to delete them. Deleting the resources helps you 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.