cassandra java 例子_教程:Java 应用:将示例数据加载到 Azure Cosmos DB 的 Cassandra API 表中 | Microsoft Docs...

您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:将示例数据加载到 Azure Cosmos DB 中的 Cassandra API 表Tutorial: Load sample data into a Cassandra API table in Azure Cosmos DB

05/20/2019

本文内容

适用于:

2ec65b1e5f12235133e468e1f0850503.png

Cassandra API

作为开发人员,你可能具有使用键/值对的应用程序。As a developer, you might have applications that use key/value pairs. 你可以使用 Azure Cosmos DB 中的 Cassandra API 帐户来存储键/值数据。You can use Cassandra API account in Azure Cosmos DB to store and manage key/value data. 本教程展示了如何使用 Java 应用程序将示例用户数据加载到 Azure Cosmos DB 中的 Cassandra API 帐户中的表。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. 该 Java 应用程序使用 Java 驱动程序并加载用户数据,例如用户 ID、用户名和用户城市。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:

将数据加载到 Cassandra 表Load data into a Cassandra table

运行应用程序Run the app

如果还没有 Azure 订阅,可以在开始前创建一个免费帐户。If you don’t have an Azure subscription, create a free account before you begin.

先决条件Prerequisites

本文是由多个部分组成的教程。This article belongs to a multi-part tutorial.

将数据加载到表中Load data into the table

通过以下步骤将数据加载到 Cassandra API 表中:Use the following steps to load data into your Cassandra API table:

打开“src\main\java\com\azure\cosmosdb\cassandra”文件夹下的“UserRepository.java”文件并追加代码,以将 user_id、user_name 和 user_bcity 字段插入到表中: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);

}

打开“src\main\java\com\azure\cosmosdb\cassandra”文件夹下的“UserProfile.java”文件。Open the “UserProfile.java” file under the “src\main\java\com\azure\cosmosdb\cassandra” folder. 此类包含调用你早前定义的 createKeyspace 和 createTable 方法的主方法。This class contains the main method that calls the createKeyspace and createTable methods you defined earlier. 现在追加以下代码,以将某些示例数据插入到 Cassandra API 表中。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");

运行应用程序Run the app

打开一个命令提示符或终端窗口,并将文件夹路径更改到你在其中创建了项目的位置。Open a command prompt or terminal window and change the folder path to where you have created the project. 运行“mvn clean install”命令,以在目标文件夹中生成 cosmosdb cassandra examples.jar 文件并运行应用程序。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

现在可以在 Azure 门户中打开数据资源管理器,确认用户信息已添加到表中。You can now open Data Explorer in the Azure portal to confirm that the user information is added to the table.

后续步骤Next steps

在本教程中,你已了解如何将示例数据加载到 Azure Cosmos DB 中的 Cassandra API 帐户。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:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值