如何通过 C++ 使用 Azure 表存储或 Azure Cosmos DB 表 APIHow to use Azure Table storage and Azure Cosmos DB Table API with C++

Tip

本文中的内容适用于 Azure 表存储和 Azure Cosmos DB 表 API。The content in this article applies to Azure Table storage and the Azure Cosmos DB Table API. Azure Cosmos DB 表 API 是表存储的高级产品,可提供吞吐量优化表、全局分发和自动辅助索引。The Azure Cosmos DB Table API is a premium offering for table storage that offers throughput-optimized tables, global distribution, and automatic secondary indexes.

本指南通过使用 Azure 表存储服务或 Azure Cosmos DB 表 API 来演示常见方案。This guide shows you common scenarios by using the Azure Table storage service or Azure Cosmos DB Table API. 示例采用 C++ 编写,并使用了适用于 C++ 的 Azure 存储客户端库The samples are written in C++ and use the Azure Storage Client Library for C++. 本文涵盖以下方案:This article covers the following scenarios:

  • 创建和删除表Create and delete a table
  • 处理表实体Work with table entities

Note

本指南主要面向适用于 C++ 的 Azure 存储客户端库 1.0.0 版及更高版本。This guide targets the Azure Storage Client Library for C++ version 1.0.0 and above. 推荐版本:存储客户端库 2.2.0(可通过使用 NuGetGitHub 获得)。The recommended version is Storage Client Library 2.2.0, which is available by using NuGet or GitHub.

创建帐户Create accounts

创建 Azure 服务帐户Create an Azure service account

可以通过 Azure 表存储或 Azure Cosmos DB 使用表。You can work with tables using Azure Table storage or Azure Cosmos DB. 若要了解有关服务之间差异的详细信息,请参阅表产品To learn more about the differences between the services, see Table offerings. 需要为所要使用的服务创建一个帐户。You'll need to create an account for the service you're going to use.

创建 Azure 存储帐户Create an Azure storage account

创建 Azure 存储帐户的最简单方法是使用 Azure 门户The easiest way to create an Azure storage account is by using the Azure portal. 若要了解更多信息,请参阅 创建存储帐户To learn more, see Create a storage account.

也可以使用 Azure PowerShellAzure CLI 创建 Azure 存储帐户。You can also create an Azure storage account by using Azure PowerShell or Azure CLI.

如果暂时不想创建存储帐户,也可以使用 Azure 存储模拟器在本地环境中运行和测试代码。If you prefer not to create a storage account at this time, you can also use the Azure storage emulator to run and test your code in a local environment. 有关详细信息,请参阅使用 Azure 存储模拟器进行开发和测试For more information, see Use the Azure storage emulator for development and testing.

创建 Azure Cosmos DB 表 API 帐户Create an Azure Cosmos DB Table API account

有关创建 Azure Cosmos DB 表 API 帐户的说明,请参阅创建数据库帐户For instructions on creating an Azure Cosmos DB Table API account, see Create a database account.

创建 C++ 应用程序Create a C++ application

在本指南中,你会使用 C++ 应用程序的存储功能。In this guide, you use storage features from a C++ application. 为此,请安装适用于 C++ 的 Azure 存储客户端库。To do so, install the Azure Storage Client Library for C++.

若要安装适用于 C++ 的 Azure 存储客户端库,请使用以下方法:To install the Azure Storage Client Library for C++, use the following methods:

可以在自述文件中找到有关如何生成源代码和导出到 Nuget 的指南。You can find a guide for how to build the source code and export to Nuget in the README file.

配置对表客户端库的访问权限Configure access to the Table client library

若要使用 Azure 存储 API 来访问表,则将以下 include 语句添加到 C++ 文件的顶部:To use the Azure storage APIs to access tables, add the following include statements to the top of the C++ file:

#include <was/storage_account.h>
#include <was/table.h>

Azure 存储客户端或 Cosmos DB 客户端使用连接字符串来存储用于访问数据管理服务的终结点和凭据。An Azure Storage client or Cosmos DB client uses a connection string to store endpoints and credentials to access data management services. 运行客户端应用程序时,必须提供相应格式的存储连接字符串或 Azure Cosmos DB 连接字符串。When you run a client application, you must provide the storage connection string or Azure Cosmos DB connection string in the appropriate format.

设置 Azure 存储连接字符串Set up an Azure Storage connection string

此示例演示如何声明一个静态字段以保存 Azure 存储连接字符串:This example shows how to declare a static field to hold the Azure Storage connection string:

// Define the Storage connection string with your values.
const utility::string_t storage_connection_string(U("DefaultEndpointsProtocol=https;AccountName=<your_storage_account>;AccountKey=<your_storage_account_key>;EndpointSuffix=core.chinacloudapi.cn"));

使用 <your_storage_account> 的存储帐户名称。Use the name of your Storage account for <your_storage_account>. 对于 <your_storage_account_key>,请使用 Azure 门户中列出的存储帐户的访问密钥。For <your_storage_account_key>, use the access key for the Storage account listed in the Azure portal. 有关存储帐户和访问密钥的信息,请参阅创建存储帐户For information on Storage accounts and access keys, see Create a storage account.

设置 Azure Cosmos DB 连接字符串Set up an Azure Cosmos DB connection string

此示例演示如何声明一个静态字段以保存 Azure Cosmos DB 连接字符串:This example shows how to declare a static field to hold the Azure Cosmos DB connection string:

// Define the Azure Cosmos DB connection string with your values.
const utility::string_t storage_connection_string(U("DefaultEndpointsProtocol=https;AccountName=<your_cosmos_db_account>;AccountKey=<your_cosmos_db_account_key>;TableEndpoint=<your_cosmos_db_endpoint>"));

使用 <your_cosmos_db_account> 的 Azure Cosmos DB 帐户名称。Use the name of your Azure Cosmos DB account for <your_cosmos_db_account>. 输入 <your_cosmos_db_account_key> 的主密钥。Enter your primary key for <your_cosmos_db_account_key>. 对于 <your_cosmos_db_endpoint>,请输入 Azure 门户中列出的终结点。Enter the endpoint listed in the Azure portal for <your_cosmos_db_endpoint>.

若要在基于 Windows 的本地计算机中测试应用程序,可以使用随 Azure SDK 一起安装的 Azure 存储模拟器。To test your application in your local Windows-based computer, you can use the Azure storage emulator that is installed with the Azure SDK. 存储模拟器是一种用于模拟本地开发计算机上提供的 Azure Blob、队列和表服务的实用程序。The storage emulator is a utility that simulates the Azure Blob, Queue, and Table services available on your local development machine. 以下示例演示如何声明一个静态字段以将连接字符串保存到本地存储模拟器:The following example shows how to declare a static field to hold the connection string to your local storage emulator:

// Define the connection string with Azure storage emulator.
const utility::string_t storage_connection_string(U("UseDevelopmentStorage=true;"));  

若要启动 Azure 存储模拟器,请从 Windows 桌面选择“开始”按钮或 Windows 键 。To start the Azure storage emulator, from your Windows desktop, select the Start button or the Windows key. 输入并运行“Azure 存储模拟器” 。Enter and run Azure Storage Emulator. 有关详细信息,请参阅使用 Azure 存储模拟器进行开发和测试For more information, see Use the Azure storage emulator for development and testing.

检索连接字符串Retrieve your connection string

可使用 cloud_storage_account 类来表示存储帐户信息。You can use the cloud_storage_account class to represent your storage account information. 若要从存储连接字符串中检索存储帐户信息,则使用 parse 方法。To retrieve your storage account information from the storage connection string, use the parse method.

// Retrieve the storage account from the connection string.
azure::storage::cloud_storage_account storage_account = azure::storage::cloud_storage_account::parse(storage_connection_string);

接下来,获取对 cloud_table_client 类的引用。Next, get a reference to a cloud_table_client class. 使用该类即可获取表存储服务中存储的表和实体的引用对象。This class lets you get reference objects for tables and entities stored within the Table storage service. 以下代码使用你在之前检索到的存储帐户对象来创建 cloud_table_client 对象:The following code creates a cloud_table_client object by using the storage account object you retrieved previously:

// Create the table client.
azure::storage::cloud_table_client table_client = storage_account.create_cloud_table_client();

创建实体并将其添加到表Create and add entities to a table

创建表Create a table

使用 cloud_table_client 对象,可以获得表和实体的引用对象。A cloud_table_client object lets you get reference objects for tables and entities. 下面的代码创建 cloud_table_client 对象并使用它来创建一个新表。The following code creates a cloud_table_client object and uses it to create a new table.

// Retrieve the storage account from the connection string.
azure::storage::cloud_storage_account storage_account = azure::storage::cloud_storage_account::parse(storage_connection_string);  

// Create the table client.
azure::storage::cloud_table_client table_client = storage_account.create_cloud_table_client();

// Retrieve a reference to a table.
azure::storage::cloud_table table = table_client.get_table_reference(U("people"));

// Create the table if it doesn't exist.
table.create_if_not_exists();  

将实体添加到表Add an entity to a table

若要将实体添加到表,请创建新的 table_entity 对象并将其传递给 table_operation::insert_entityTo add an entity to a table, create a new table_entity object and pass it to table_operation::insert_entity. 以下代码使用客户的名字作为行键,并使用姓氏作为分区键。The following code uses the customer's first name as the row key and last name as the partition key. 实体的分区键和行键共同唯一地标识表中的实体。Together, an entity's partition and row key uniquely identify the entity in the table. 相比于查询分区键不同的实体,分区键相同的实体的查询速度更快。Entities with the same partition key can be queried faster than entities with different partition keys. 使用不同的分区键可提升并行操作可伸缩性。Using diverse partition keys allows for greater parallel operation scalability. 有关详细信息,请参阅 Azure 存储性能和可伸缩性核对清单For more information, see Azure storage performance and scalability checklist.

下面的代码将创建 table_entity 的新实例,其中包含一些可存储的客户数据。The following code creates a new instance of table_entity with some customer data to store. 接下来,该代码调用 table_operation::insert_entity 来创建 table_operation 对象,以便将实体插入表中,并将新的表实体与之关联。The code next calls table_operation::insert_entity to create a table_operation object to insert an entity into a table, and associates the new table entity with it. 最后,该代码会在 cloud_table 对象上调用 execute 方法。Finally, the code calls the execute method on the cloud_table object. 新的 table_operation 向表服务发送请求,以此将新的客户实体插入 people 表中。The new table_operation sends a request to the Table service to insert the new customer entity into the people table.

// Retrieve the storage account from the connection string.
azure::storage::cloud_storage_account storage_account = azure::storage::cloud_storage_account::parse(storage_connection_string);

// Create the table client.
azure::storage::cloud_table_client table_client = storage_account.create_cloud_table_client();

// Retrieve a reference to a table.
azure::storage::cloud_table table = table_client.get_table_reference(U("people"));

// Create the table if it doesn't exist.
table.create_if_not_exists();

// Create a new customer entity.
azure::storage::table_entity customer1(U("Harp"), U("Walter"));

azure::storage::table_entity::properties_type& properties = customer1.properties();
properties.reserve(2);
properties[U("Email")] = azure::storage::entity_property(U("Walter@contoso.com"));

properties[U("Phone")] = azure::storage::entity_property(U("425-555-0101"));

// Create the table operation that inserts the customer entity.
azure::storage::table_operation insert_operation = azure::storage::table_operation::insert_entity(customer1);

// Execute the insert operation.
azure::storage::table_result insert_result = table.execute(insert_operation);

插入一批实体Insert a batch of entities

可通过一个写入操作将一批条目插入到表服务。You can insert a batch of entities to the Table service in one write operation. 以下代码创建 table_batch_operation 对象,并向其中添加三个插入操作。The following code creates a table_batch_operation object, and then adds three insert operations to it. 每个插入操作的添加方法如下:创建一个新的实体对象,设置它的值,然后在 table_batch_operation 对象上调用 insert 方法以将实体与新插入操作相关联。Each insert operation is added by creating a new entity object, setting its values, and then calling the insert method on the table_batch_operation object to associate the entity with a new insert operation. 然后,该代码会调用 cloud_table.execute 来运行操作。Then, the code calls cloud_table.execute to run the operation.

// Retrieve the storage account from the connection string.
azure::storage::cloud_storage_account storage_account = azure::storage::cloud_storage_account::parse(storage_connection_string);

// Create the table client.
azure::storage::cloud_table_client table_client = storage_account.create_cloud_table_client();

// Create a cloud table object for the table.
azure::storage::cloud_table table = table_client.get_table_reference(U("people"));

// Define a batch operation.
azure::storage::table_batch_operation batch_operation;

// Create a customer entity and add it to the table.
azure::storage::table_entity customer1(U("Smith"), U("Jeff"));

azure::storage::table_entity::properties_type& properties1 = customer1.properties();
properties1.reserve(2);
properties1[U("Email")] = azure::storage::entity_property(U("Jeff@contoso.com"));
properties1[U("Phone")] = azure::storage::entity_property(U("425-555-0104"));

// Create another customer entity and add it to the table.
azure::storage::table_entity customer2(U("Smith"), U("Ben"));

azure::storage::table_entity::properties_type& properties2 = customer2.properties();
properties2.reserve(2);
properties2[U("Email")] = azure::storage::entity_property(U("Ben@contoso.com"));
properties2[U("Phone")] = azure::storage::entity_property(U("425-555-0102"));

// Create a third customer entity to add to the table.
azure::storage::table_entity customer3(U("Smith"), U("Denise"));

azure::storage::table_entity::properties_type& properties3 = customer3.properties();
properties3.reserve(2);
properties3[U("Email")] = azure::storage::entity_property(U("Denise@contoso.com"));
properties3[U("Phone")] = azure::storage::entity_property(U("425-555-0103"));

// Add customer entities to the batch insert operation.
batch_operation.insert_or_replace_entity(customer1);
batch_operation.insert_or_replace_entity(customer2);
batch_operation.insert_or_replace_entity(customer3);

// Execute the batch operation.
std::vector<azure::storage::table_result> results = table.execute_batch(batch_operation);

批处理操作的注意事项如下:Some things to note on batch operations:

  • 在单次批处理中,可以执行最多 100 个任意组合的 insertdeletemergereplaceinsert-or-mergeinsert-or-replace 操作。You can do up to 100 insert, delete, merge, replace, insert-or-merge, and insert-or-replace operations in any combination in a single batch.
  • 批处理操作也可以包含检索操作,但前提是检索操作是批处理中仅有的操作。A batch operation can have a retrieve operation, if it's the only operation in the batch.
  • 单次批处理操作中的所有条目都必须具有相同的分区键。All entities in a single batch operation must have the same partition key.
  • 批处理操作的数据负载限制为 4MB。A batch operation is limited to a 4-MB data payload.

查询和修改实体Query and modify entities

检索分区中的所有条目Retrieve all entities in a partition

若要查询表以获取某个分区中的所有实体,请使用 table_query 对象。To query a table for all entities in a partition, use a table_query object. 以下代码示例指定了一个筛选器,以筛选分区键为 Smith 的实体。The following code example specifies a filter for entities where Smith is the partition key. 此示例会将查询结果中每个实体的字段输出到控制台。This example prints the fields of each entity in the query results to the console.

Note

Azure Cosmos DB 中的 C++ 目前不支持这些方法。These methods are not currently supported for C++ in Azure Cosmos DB.

// Retrieve the storage account from the connection string.
azure::storage::cloud_storage_account storage_account = azure::storage::cloud_storage_account::parse(storage_connection_string);

// Create the table client.
azure::storage::cloud_table_client table_client = storage_account.create_cloud_table_client();

// Create a cloud table object for the table.
azure::storage::cloud_table table = table_client.get_table_reference(U("people"));

// Construct the query operation for all customer entities where PartitionKey="Smith".
azure::storage::table_query query;

query.set_filter_string(azure::storage::table_query::generate_filter_condition(U("PartitionKey"), azure::storage::query_comparison_operator::equal, U("Smith")));

// Execute the query.
azure::storage::table_query_iterator it = table.execute_query(query);

// Print the fields for each customer.
azure::storage::table_query_iterator end_of_results;
for (; it != end_of_results; ++it)
{
    const azure::storage::table_entity::properties_type& properties = it->properties();

    std::wcout << U("PartitionKey: ") << it->partition_key() << U(", RowKey: ") << it->row_key()
        << U(", Property1: ") << properties.at(U("Email")).string_value()
        << U(", Property2: ") << properties.at(U("Phone")).string_value() << std::endl;
}  

此示例中的查询会返回与筛选条件匹配的所有实体。The query in this example returns all the entities that match the filter criteria. 如果有大型表并需要经常下载表条目,建议改为将数据存储在 Azure 存储 Blob 中。If you have large tables and need to download the table entities often, we recommend that you store your data in Azure storage blobs instead.

检索分区中的一部分条目Retrieve a range of entities in a partition

如果不想查询分区中的所有实体,则可以指定范围。If you don't want to query all the entities in a partition, you can specify a range. 合并分区键筛选器和行键筛选器。Combine the partition key filter with a row key filter. 以下代码示例使用两个筛选器来获取 Smith 分区中的、行键(名字)以字母 E 前面的字母开头的所有实体,并打印查询结果。The following code example uses two filters to get all entities in partition Smith where the row key (first name) starts with a letter earlier than E in the alphabet, and then prints the query results.

Note

Azure Cosmos DB 中的 C++ 目前不支持这些方法。These methods are not currently supported for C++ in Azure Cosmos DB.

// Retrieve the storage account from the connection string.
azure::storage::cloud_storage_account storage_account = azure::storage::cloud_storage_account::parse(storage_connection_string);

// Create the table client.
azure::storage::cloud_table_client table_client = storage_account.create_cloud_table_client();

// Create a cloud table object for the table.
azure::storage::cloud_table table = table_client.get_table_reference(U("people"));

// Create the table query.
azure::storage::table_query query;

query.set_filter_string(azure::storage::table_query::combine_filter_conditions(
    azure::storage::table_query::generate_filter_condition(U("PartitionKey"),
    azure::storage::query_comparison_operator::equal, U("Smith")),
    azure::storage::query_logical_operator::op_and,
    azure::storage::table_query::generate_filter_condition(U("RowKey"), azure::storage::query_comparison_operator::less_than, U("E"))));

// Execute the query.
azure::storage::table_query_iterator it = table.execute_query(query);

// Loop through the results, displaying information about the entity.
azure::storage::table_query_iterator end_of_results;
for (; it != end_of_results; ++it)
{
    const azure::storage::table_entity::properties_type& properties = it->properties();

    std::wcout << U("PartitionKey: ") << it->partition_key() << U(", RowKey: ") << it->row_key()
        << U(", Property1: ") << properties.at(U("Email")).string_value()
        << U(", Property2: ") << properties.at(U("Phone")).string_value() << std::endl;
}  

检索单个实体Retrieve a single entity

可以编写查询以检索单个特定实体。You can write a query to retrieve a single, specific entity. 以下代码使用 table_operation::retrieve_entity 来指定客户 Jeff SmithThe following code uses table_operation::retrieve_entity to specify the customer Jeff Smith. 此方法只返回一个实体,而不是返回一个集合,并且返回的值在 table_result 中。This method returns just one entity, rather than a collection, and the returned value is in table_result. 在查询中指定分区键和行键是从表服务中检索单个实体的最快方法。Specifying both partition and row keys in a query is the fastest way to retrieve a single entity from the Table service.

azure::storage::cloud_storage_account storage_account = azure::storage::cloud_storage_account::parse(storage_connection_string);

// Create the table client.
azure::storage::cloud_table_client table_client = storage_account.create_cloud_table_client();

// Create a cloud table object for the table.
azure::storage::cloud_table table = table_client.get_table_reference(U("people"));

// Retrieve the entity with partition key of "Smith" and row key of "Jeff".
azure::storage::table_operation retrieve_operation = azure::storage::table_operation::retrieve_entity(U("Smith"), U("Jeff"));
azure::storage::table_result retrieve_result = table.execute(retrieve_operation);

// Output the entity.
azure::storage::table_entity entity = retrieve_result.entity();
const azure::storage::table_entity::properties_type& properties = entity.properties();

std::wcout << U("PartitionKey: ") << entity.partition_key() << U(", RowKey: ") << entity.row_key()
    << U(", Property1: ") << properties.at(U("Email")).string_value()
    << U(", Property2: ") << properties.at(U("Phone")).string_value() << std::endl;

替换条目Replace an entity

要替换条目,请从表服务中检索它,修改条目对象,然后将更改保存回表服务。To replace an entity, retrieve it from the Table service, modify the entity object, and then save the changes back to the Table service. 以下代码更改现有客户的电话号码和电子邮件地址。The following code changes an existing customer's phone number and email address. 此代码并不调用 table_operation::insert_entity,而是使用 table_operation::replace_entityInstead of calling table_operation::insert_entity, this code uses table_operation::replace_entity. 这一方法会导致在服务器上完全替换该实体,除非服务器上的该实体自检索到它以后就发生更改。This approach causes the entity to be fully replaced on the server, unless the entity on the server has changed since it was retrieved. 如果已更改,则操作失败。If it has been changed, the operation fails. 操作失败可防止应用程序覆盖另一个组件在检索与更新之间所做的更改。This failure prevents your application from overwriting a change made between the retrieval and update by another component. 正确处理此失败问题的方法是再次检索实体,进行更改(如果仍有效),然后再次执行 table_operation::replace_entity 操作。The proper handling of this failure is to retrieve the entity again, make your changes, if still valid, and then do another table_operation::replace_entity operation.

// Retrieve the storage account from the connection string.
azure::storage::cloud_storage_account storage_account = azure::storage::cloud_storage_account::parse(storage_connection_string);

// Create the table client.
azure::storage::cloud_table_client table_client = storage_account.create_cloud_table_client();

// Create a cloud table object for the table.
azure::storage::cloud_table table = table_client.get_table_reference(U("people"));

// Replace an entity.
azure::storage::table_entity entity_to_replace(U("Smith"), U("Jeff"));
azure::storage::table_entity::properties_type& properties_to_replace = entity_to_replace.properties();
properties_to_replace.reserve(2);

// Specify a new phone number.
properties_to_replace[U("Phone")] = azure::storage::entity_property(U("425-555-0106"));

// Specify a new email address.
properties_to_replace[U("Email")] = azure::storage::entity_property(U("JeffS@contoso.com"));

// Create an operation to replace the entity.
azure::storage::table_operation replace_operation = azure::storage::table_operation::replace_entity(entity_to_replace);

// Submit the operation to the Table service.
azure::storage::table_result replace_result = table.execute(replace_operation);

插入或替换实体Insert or replace an entity

如果该实体自从服务器中检索到它以后就发生更改,则 table_operation::replace_entity 操作会失败。table_operation::replace_entity operations fail if the entity has been changed since it was retrieved from the server. 此外,你必须首先从服务器中检索该实体,table_operation::replace_entity 才能成功。Furthermore, you must retrieve the entity from the server first in order for table_operation::replace_entity to be successful. 有时,你不知道实体是否存在于服务器上。Sometimes, you don't know if the entity exists on the server. 这与当前存储在其中的值无关,因为更新操作会将它们全部覆盖。The current values stored in it are irrelevant, because your update should overwrite them all. 若要完成此结果,请使用 table_operation::insert_or_replace_entity 操作。To accomplish this result, use a table_operation::insert_or_replace_entity operation. 如果实体不存在,该操作会插入实体。This operation inserts the entity if it doesn't exist. 如果实体存在,该操作会替换实体。The operation replaces the entity if it exists. 在以下代码示例中,仍然会检索 Jeff Smith 的客户实体,但稍后会使用 table_operation::insert_or_replace_entity 将其保存回服务器。In the following code example, the customer entity for Jeff Smith is still retrieved, but it's then saved back to the server by using table_operation::insert_or_replace_entity. 将覆盖在检索与更新操作之间对实体进行的任何更新。Any updates made to the entity between the retrieval and update operation will be overwritten.

// Retrieve the storage account from the connection string.
azure::storage::cloud_storage_account storage_account = azure::storage::cloud_storage_account::parse(storage_connection_string);

// Create the table client.
azure::storage::cloud_table_client table_client = storage_account.create_cloud_table_client();

// Create a cloud table object for the table.
azure::storage::cloud_table table = table_client.get_table_reference(U("people"));

// Insert or replace an entity.
azure::storage::table_entity entity_to_insert_or_replace(U("Smith"), U("Jeff"));
azure::storage::table_entity::properties_type& properties_to_insert_or_replace = entity_to_insert_or_replace.properties();

properties_to_insert_or_replace.reserve(2);

// Specify a phone number.
properties_to_insert_or_replace[U("Phone")] = azure::storage::entity_property(U("425-555-0107"));

// Specify an email address.
properties_to_insert_or_replace[U("Email")] = azure::storage::entity_property(U("Jeffsm@contoso.com"));

// Create an operation to insert or replace the entity.
azure::storage::table_operation insert_or_replace_operation = azure::storage::table_operation::insert_or_replace_entity(entity_to_insert_or_replace);

// Submit the operation to the Table service.
azure::storage::table_result insert_or_replace_result = table.execute(insert_or_replace_operation);

查询条目属性的子集Query a subset of entity properties

对表的查询可以只检索条目的几个属性。A query to a table can retrieve just a few properties from an entity. 以下代码中的查询使用 table_query::set_select_columns 方法,仅返回表中实体的电子邮件地址。The query in the following code uses the table_query::set_select_columns method to return only the email addresses of entities in the table.

// Retrieve the storage account from the connection string.
azure::storage::cloud_storage_account storage_account = azure::storage::cloud_storage_account::parse(storage_connection_string);

// Create the table client.
azure::storage::cloud_table_client table_client = storage_account.create_cloud_table_client();

// Create a cloud table object for the table.
azure::storage::cloud_table table = table_client.get_table_reference(U("people"));

// Define the query, and select only the Email property.
azure::storage::table_query query;
std::vector<utility::string_t> columns;

columns.push_back(U("Email"));
query.set_select_columns(columns);

// Execute the query.
azure::storage::table_query_iterator it = table.execute_query(query);

// Display the results.
azure::storage::table_query_iterator end_of_results;
for (; it != end_of_results; ++it)
{
    std::wcout << U("PartitionKey: ") << it->partition_key() << U(", RowKey: ") << it->row_key();

    const azure::storage::table_entity::properties_type& properties = it->properties();
    for (auto prop_it = properties.begin(); prop_it != properties.end(); ++prop_it)
    {
        std::wcout << ", " << prop_it->first << ": " << prop_it->second.str();
    }

    std::wcout << std::endl;
}

Note

查询实体的几个属性是比检索所有属性更高效的操作。Querying a few properties from an entity is a more efficient operation than retrieving all properties.

删除内容Delete content

删除条目Delete an entity

检索到实体后可以将其删除。You can delete an entity after you retrieve it. 检索到实体后,对要删除的实体调用 table_operation::delete_entityAfter you retrieve an entity, call table_operation::delete_entity with the entity to delete. 然后调用 cloud_table.execute 方法。Then call the cloud_table.execute method. 以下代码检索并删除分区键为 Smith、行键为 Jeff 的实体。The following code retrieves and deletes an entity with a partition key of Smith and a row key of Jeff.

// Retrieve the storage account from the connection string.
azure::storage::cloud_storage_account storage_account = azure::storage::cloud_storage_account::parse(storage_connection_string);

// Create the table client.
azure::storage::cloud_table_client table_client = storage_account.create_cloud_table_client();

// Create a cloud table object for the table.
azure::storage::cloud_table table = table_client.get_table_reference(U("people"));

// Create an operation to retrieve the entity with partition key of "Smith" and row key of "Jeff".
azure::storage::table_operation retrieve_operation = azure::storage::table_operation::retrieve_entity(U("Smith"), U("Jeff"));
azure::storage::table_result retrieve_result = table.execute(retrieve_operation);

// Create an operation to delete the entity.
azure::storage::table_operation delete_operation = azure::storage::table_operation::delete_entity(retrieve_result.entity());

// Submit the delete operation to the Table service.
azure::storage::table_result delete_result = table.execute(delete_operation);  

删除表Delete a table

最后,以下代码示例从存储帐户中删除表。Finally, the following code example deletes a table from a storage account. 在删除表之后的一段时间内无法重新创建表。A table that has been deleted is unavailable to be re-created for some time following the deletion.

// Retrieve the storage account from the connection string.
azure::storage::cloud_storage_account storage_account = azure::storage::cloud_storage_account::parse(storage_connection_string);

// Create the table client.
azure::storage::cloud_table_client table_client = storage_account.create_cloud_table_client();

// Create a cloud table object for the table.
azure::storage::cloud_table table = table_client.get_table_reference(U("people"));

// Delete the table if it exists
if (table.delete_table_if_exists())
{
    std::cout << "Table deleted!";
}
else
{
    std::cout << "Table didn't exist";
}

故障排除Troubleshooting

对于 Visual Studio Community 版本,如果项目因包含文件 storage_account.h 和 table.h 而显示生成错误,请删除 /permissive- 编译器开关 :For Visual Studio Community Edition, if your project gets build errors because of the include files storage_account.h and table.h, remove the /permissive- compiler switch:

  1. 在“解决方案资源管理器” 中,右键单击项目,并选择“属性” 。In Solution Explorer, right-click your project and select Properties.
  2. 在“属性页” 对话框中,依次展开“配置属性” 、“C/C++” ,然后选择“语言” 。In the Property Pages dialog box, expand Configuration Properties, expand C/C++, and select Language.
  3. 将“符合模式” 设置为“否” 。Set Conformance mode to No.

后续步骤Next steps

Azure 存储资源管理器是 Azure 免费提供的独立应用,用于在 Windows、macOS 和 Linux 上以可视方式处理 Azure 存储数据。Azure Storage Explorer is a free, standalone app from Azure that enables you to work visually with Azure Storage data on Windows, macOS, and Linux.

请打开以下链接了解有关 Azure 存储和 Azure Cosmos DB 中的表 API 的详细信息:Follow these links to learn more about Azure Storage and the Table API in Azure Cosmos DB: