Редактиране

Споделяне чрез


Tutorial: Load sample data into a API for Cassandra table in Azure Cosmos DB

APPLIES TO: Cassandra

As a developer, you might have applications that use key/value pairs. You can use API for Cassandra account in Azure Cosmos DB to store and manage key/value data. This tutorial shows how to load sample user data to a table in a API for Cassandra account in Azure Cosmos DB by using a Java application. The Java application uses the Java driver and loads user data such as user ID, user name, and user city.

This tutorial covers the following tasks:

  • Load data into a Cassandra table
  • Run the app

If you don’t have an Azure subscription, create a free account before you begin.

Prerequisites

Load data into the table

Use the following steps to load data into your API for Cassandra table:

  1. Open the “UserRepository.java” file under the “src\main\java\com\azure\cosmosdb\cassandra” folder and append the code to insert the user_id, user_name and user_bcity fields into the table:

    /**
    * Insert a row into user table
    *
    * @param id   user_id
    * @param name user_name
    * @param city user_bcity
    */
    public void insertUser(PreparedStatement statement, int id, String name, String city) {
         BoundStatement boundStatement = new BoundStatement(statement);
         session.execute(boundStatement.bind(id, name, city));
    }
    
    /**
    * Create a PrepareStatement to insert a row to user table
    *
    * @return PreparedStatement
    */
    public PreparedStatement prepareInsertStatement() {
       final String insertStatement = "INSERT INTO  uprofile.user (user_id, user_name , user_bcity) VALUES (?,?,?)";
    return session.prepare(insertStatement);
    }
    
  2. Open the “UserProfile.java” file under the “src\main\java\com\azure\cosmosdb\cassandra” folder. This class contains the main method that calls the createKeyspace and createTable methods you defined earlier. Now append the following code to insert some sample data into the API for Cassandra table.

    //Insert rows into user table
    PreparedStatement preparedStatement = repository.prepareInsertStatement();
      repository.insertUser(preparedStatement, 1, "JohnH", "Seattle");
      repository.insertUser(preparedStatement, 2, "EricK", "Spokane");
      repository.insertUser(preparedStatement, 3, "MatthewP", "Tacoma");
      repository.insertUser(preparedStatement, 4, "DavidA", "Renton");
      repository.insertUser(preparedStatement, 5, "PeterS", "Everett");
    

Run the app

Open a command prompt or terminal window and change the folder path to where you have created the project. Run the “mvn clean install” command to generate the cosmosdb-cassandra-examples.jar file within the target folder and run the application.

cd "cassandra-demo"

mvn clean install

java -cp target/cosmosdb-cassandra-examples.jar com.azure.cosmosdb.cassandra.examples.UserProfile

You can now open Data Explorer in the Azure portal to confirm that the user information is added to the table.

Next steps

In this tutorial, you've learned how to load sample data to a API for Cassandra account in Azure Cosmos DB. You can now proceed to the next article: