Edit

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

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

  • A Git client.

  • cURL or a similar HTTP utility to test functionality.

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.

  1. Browse to the Azure portal at https://portal.azure.com/ and sign in.

  2. Select Create a resource, select Get started, and then select Azure Cosmos DB.

  3. On the Select API option screen, select Cassandra.

    Screenshot of the Azure portal API option page with Cassandra selected.

  4. 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.

    Screenshot of Azure Cosmos DB account settings in the Azure portal.

  5. When you enter all of the preceding information, select Review + create.

  6. 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

  1. Browse to the Azure portal at https://portal.azure.com/ and sign in.

  2. Select All Resources, and then select the Azure Cosmos DB account you created.

  3. Select Data Explorer, select the down arrow, and select New Keyspace. Enter a unique identifier for your Keyspace id, and then select OK.

    Screenshot of Data Explorer with the New Keyspace option selected.

    Screenshot of the dialog used to create an Azure Cosmos DB keyspace.

Retrieve the connection settings for your Azure Cosmos DB account

  1. Browse to the Azure portal at https://portal.azure.com/ and sign in.

  2. Select All Resources, and then select the Azure Cosmos DB account you created.

  3. 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.

    Screenshot of Azure Cosmos DB connection string settings in the Azure portal.

Configure the sample application

The following procedure configures the test application.

  1. 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
    
  2. Locate the application.properties file in the resources directory of the sample project, or create the file if it doesn't already exist.

  3. 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.
  4. 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.

  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 by using curl from 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/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'}.
    
  4. Retrieve all existing records by using curl from a command prompt. The following examples show how to retrieve records:

    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 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.