Events
17 Mar, 9 pm - 21 Mar, 10 am
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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:
If you don’t have an Azure subscription, create a free account before you begin.
Use the following steps to load data into your API for Cassandra 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 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");
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 API for Cassandra account in Azure Cosmos DB. You can now proceed to the next article:
Events
17 Mar, 9 pm - 21 Mar, 10 am
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Build a Java app with cloud-scale NoSQL Cosmos DB - Training
Learn how to create a Java application to store and query data in Azure Cosmos DB
Certification
Microsoft Certified: Azure Cosmos DB Developer Specialty - Certifications
Write efficient queries, create indexing policies, manage, and provision resources in the SQL API and SDK with Microsoft Azure Cosmos DB.