title | description | author | ms.service | ms.component | ms.topic | ms.date | ms.author | ms.reviewer | Customer intent |
---|---|---|---|---|---|---|---|---|---|
Tutorial: Load sample data into a Cassandra API table in Azure Cosmos DB by using a Java application |
This tutorial shows how to load sample user data to a Cassandra API table in Azure Cosmos DB by using a java application. |
kanshiG |
cosmos-db |
cosmosdb-cassandra |
tutorial |
12/03/2018 |
govindk |
sngun |
As a developer, I want to build a Java application to load data to a Cassandra API table in Azure Cosmos DB so that customers can store and manage the key/value data and utilize the global distribution, elastic scaling, multi-master, and other capabilities offered by Azure Cosmos DB. |
As a developer, you might have applications that use key/value pairs. You can use Cassandra API 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 Cassandra API 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:
[!div class="checklist"]
- Load data into a Cassandra table
- Run the app
If you don’t have an Azure subscription, create a free account before you begin.
- This article belongs to a multi-part tutorial. Before you start with this doc, make sure to create the Cassandra API account, keyspace, and table.
Use the following steps to load data into your Cassandra API table:
-
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); }
-
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 Cassandra API 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");
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.
In this tutorial, you've learned how to load sample data to a Cassandra API account in Azure Cosmos DB. You can now proceed to the next article:
[!div class="nextstepaction"] Query data from the Cassandra API account