Jak używać usług Azure Table Storage i Azure Cosmos DB dla tabel w języku C++

DOTYCZY: Tabeli

Napiwek

Zawartość tego artykułu dotyczy usług Azure Table Storage i Azure Cosmos DB for Table. Interfejs API dla tabel to oferta premium dla magazynu tabel, która oferuje tabele zoptymalizowane pod kątem przepływności, dystrybucję globalną i automatyczne indeksy pomocnicze.

W tym przewodniku przedstawiono typowe scenariusze przy użyciu usługi Azure Table Storage lub usługi Azure Cosmos DB dla tabel. Przykłady są napisane w języku C++ i korzystają z biblioteki klienta usługi Azure Table Storage dla języka C++. W tym artykule omówiono następujące scenariusze:

  • Tworzenie i usuwanie tabeli
  • Praca z jednostkami tabeli

Uwaga

Ten przewodnik jest przeznaczony do użycia z biblioteką klienta usługi Azure Storage dla języka C++ w wersji 1.0.0 lub wyższej. Zalecaną wersją jest biblioteka klienta usługi Storage 2.2.0, która jest dostępna przy użyciu narzędzia NuGet lub GitHub.

Tworzenie kont

Tworzenie konta usługi Azure

Możesz pracować z tabelami przy użyciu usługi Azure Table Storage lub usługi Azure Cosmos DB. Aby dowiedzieć się więcej o różnicach między ofertami tabel w tych dwóch usługach, zobacz omówienie interfejsu API dla tabel. Aby móc korzystać z wybranej usługi, musisz w niej utworzyć konto. W poniższych sekcjach pokazano, jak utworzyć zarówno usługę Azure Table Storage, jak i konto usługi Azure Cosmos DB, jednak możesz po prostu użyć jednego z nich.

Tworzenie konta usługi Azure Storage

Najprostszym sposobem utworzenia konta usługi Azure Storage jest użycie witryny Azure Portal. Więcej informacji można znaleźć w temacie Tworzenie konta magazynu.

Możesz utworzyć konto usługi Azure Storage przy użyciu programu Azure PowerShell lub interfejsu wiersza polecenia platformy Azure.

Jeśli w tej chwili nie chcesz tworzyć konta magazynu, możesz również użyć emulatora usługi Azure Storage do uruchomienia i przetestowania kodu w środowisku lokalnym. Aby uzyskać więcej informacji, zobacz Use the Azure Storage Emulator for development and testing (Używanie emulatora usługi Azure Storage do programowania i testowania).

Tworzenie konta usługi Azure Cosmos DB dla tabel

Aby uzyskać instrukcje dotyczące tworzenia konta usługi Azure Cosmos DB dla tabel, zobacz Tworzenie konta bazy danych.

Tworzenie aplikacji języka C++

W tym przewodniku użyjesz funkcji magazynu z poziomu aplikacji języka C++. W tym celu zainstaluj bibliotekę klienta usługi Azure Storage dla języka C++.

Aby zainstalować bibliotekę klienta usługi Azure Storage dla języka C++, użyj następujących metod:

.\vcpkg.exe install azure-storage-cpp

Przewodnik dotyczący tworzenia kodu źródłowego i eksportowania go do narzędzia Nuget można znaleźć w pliku README .

Konfigurowanie dostępu do biblioteki klienta usługi Table Storage

Aby uzyskać dostęp do tabel przy użyciu interfejsów API usługi Azure Storage, dodaj następujące include instrukcje na początku pliku C++:

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

Klient usługi Azure Storage lub klient usługi Azure Cosmos DB używa parametry połączenia do przechowywania punktów końcowych i poświadczeń w celu uzyskania dostępu do usług zarządzania danymi. Po uruchomieniu aplikacji klienckiej należy podać parametry połączenia magazynu lub usługę Azure Cosmos DB parametry połączenia w odpowiednim formacie.

Konfigurowanie parametrów połączenia usługi Azure Storage

W tym przykładzie pokazano, jak zadeklarować pole statyczne do przechowywania parametry połączenia usługi Azure Storage:

// 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>"));

Użyj nazwy konta usługi Storage dla .<your_storage_account> W przypadku <your_storage_account_key> użyj klucza dostępu dla konta magazynu wymienionego w witrynie Azure Portal. Aby uzyskać informacje na temat kont magazynu i kluczy dostępu, zobacz Tworzenie konta magazynu.

Konfigurowanie parametrów połączenia usługi Azure Cosmos DB

W tym przykładzie pokazano, jak zadeklarować pole statyczne do przechowywania parametry połączenia usługi Azure Cosmos DB:

// 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>"));

Użyj nazwy konta usługi Azure Cosmos DB dla polecenia <your_cosmos_db_account>. Wprowadź klucz podstawowy dla <your_cosmos_db_account_key>. Wprowadź punkt końcowy wymieniony w witrynie Azure Portal dla elementu <your_cosmos_db_endpoint>.

Aby przetestować aplikację na lokalnym komputerze z systemem Windows, możesz użyć emulatora usługi Azure Storage zainstalowanego przy użyciu zestawu Azure SDK. Emulator magazynu to narzędzie, które symuluje usługi Azure Blob, Queue i Table dostępne na lokalnej maszynie programistycznej. W poniższym przykładzie pokazano, jak zadeklarować pole statyczne do przechowywania parametry połączenia w lokalnym emulatorze magazynu:

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

Aby uruchomić emulator usługi Azure Storage, na pulpicie systemu Windows wybierz przycisk Start lub klucz systemu Windows. Wprowadź i uruchom emulator usługi Microsoft Azure Storage. Aby uzyskać więcej informacji, zobacz Use the Azure Storage Emulator for development and testing (Używanie emulatora usługi Azure Storage do programowania i testowania).

Pobieranie parametrów połączenia

Możesz użyć cloud_storage_account klasy do reprezentowania informacji o koncie magazynu. Aby pobrać informacje o koncie magazynu z parametry połączenia magazynu, użyj parse metody .

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

Następnie uzyskaj odwołanie do cloud_table_client klasy. Ta klasa umożliwia uzyskiwanie obiektów referencyjnych dla tabel i jednostek przechowywanych w usłudze Table Storage. Poniższy kod tworzy cloud_table_client obiekt przy użyciu pobranego wcześniej obiektu konta magazynu:

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

Tworzenie i dodawanie jednostek do tabeli

Utwórz tabelę

Obiekt cloud_table_client umożliwia uzyskiwanie obiektów referencyjnych dla tabel i jednostek. Poniższy kod tworzy cloud_table_client obiekt i używa go do utworzenia nowej tabeli.

// 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();  

Dodawanie jednostki do tabeli

Aby dodać jednostkę do tabeli, utwórz nowy table_entity obiekt i przekaż go do table_operation::insert_entityelementu . W poniższym kodzie imię klienta jest używane jako klucz wiersza, a nazwisko jako klucz partycji. Razem klucz partycji i klucz wiersza jednostki jednoznacznie identyfikują jednostkę w tabeli. Jednostki z tym samym kluczem partycji mogą być odpytywane szybciej niż jednostki z różnymi kluczami partycji. Użycie różnych kluczy partycji umożliwia większą skalowalność operacji równoległych. Aby uzyskać więcej informacji, zobacz temat Microsoft Azure Storage Performance and Scalability Checklist (Lista kontrolna dotycząca wydajności i skalowalności usługi Microsoft Azure Storage).

Poniższy kod tworzy nowe wystąpienie z table_entity danymi klienta do przechowywania. Kod obok wywołuje table_operation::insert_entity metodę table_operation tworzenia obiektu w celu wstawienia jednostki do tabeli i kojarzy z nią nową jednostkę tabeli. Na koniec kod wywołuje metodę executecloud_table w obiekcie . Nowa table_operation funkcja wysyła żądanie do usługi Table Service w celu wstawienia nowej jednostki klienta do people tabeli.

// 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);

Zbiorcze wstawianie jednostek

Możesz wstawić partię jednostek do usługi tabel w ramach jednej operacji zapisu. Poniższy kod tworzy table_batch_operation obiekt, a następnie dodaje do niego trzy operacje wstawiania. Każda operacja wstawiania jest dodawana przez utworzenie nowego obiektu jednostki, ustawienie jego wartości, a następnie wywołanie insert metody w table_batch_operation obiekcie w celu skojarzenia jednostki z nową operacją wstawiania. Następnie kod wywołuje polecenie cloud_table.execute , aby uruchomić operację.

// 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);

Kilka uwag dotyczących operacji zbiorczych:

  • W jednej partii można wykonać maksymalnie 100 insertoperacji, , deletereplacemerge, , insert-or-mergei insert-or-replace w dowolnej kombinacji.
  • Operacja wsadowa może mieć operację pobierania, jeśli jest to jedyna operacja w partii.
  • Wszystkie jednostki w jednej operacji zbiorczej muszą mieć ten sam klucz partycji.
  • Maksymalny rozmiar ładunku danych operacji zbiorczej to 4 MB.

Wykonywanie zapytań i modyfikowanie jednostek

Pobieranie wszystkich jednostek w partycji

Aby wysłać zapytanie do tabeli dla wszystkich jednostek w partycji, użyj table_query obiektu . Poniższy przykład kodu określa filtr dla jednostek, w których Smith jest klucz partycji. W tym przykładzie drukowane są pola każdej jednostki w wynikach zapytania w konsoli.

Uwaga

Te metody języka C++ nie są obecnie obsługiwane w przypadku usługi 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;
}  

Zapytanie w tym przykładzie zwraca wszystkie jednostki zgodne z kryteriami filtrowania. Jeśli masz duże tabele i często pobierasz jednostki z tabel, zalecamy zamiast tego przechowywanie danych w obiektach blob w usłudze Azure Storage.

Pobieranie zakresu jednostek w partycji

Jeśli nie chcesz wykonywać zapytań dotyczących wszystkich jednostek w partycji, możesz określić zakres. Połącz filtr klucza partycji z filtrem klucza wiersza. Poniższy przykład kodu używa dwóch filtrów, aby uzyskać wszystkie jednostki w partycji Smith , w których klucz wiersza (imię) zaczyna się od litery wcześniejszej niż E w alfabecie, a następnie wyświetla wyniki zapytania.

Uwaga

Te metody języka C++ nie są obecnie obsługiwane w przypadku usługi 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;
}  

Pobieranie pojedynczej jednostki

Można napisać zapytanie do pobrania jednej, określonej jednostki. Poniższy kod używa table_operation::retrieve_entity metody do określenia klienta Jeff Smith. Ta metoda zwraca tylko jedną jednostkę, a nie kolekcję, a zwracana wartość ma wartość .table_result Określenie kluczy partycji i wiersza w pojedynczym zapytaniu jest najszybszym sposobem na pobranie jednej jednostki z usługi tabel.

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;

Zastępowanie jednostki

Aby zastąpić jednostkę, pobierz ją z usługi Table Storage, zmodyfikuj obiekt jednostki, a następnie zapisz zmiany w usłudze Table Storage. Poniższy kod zmienia numer telefonu i adres e-mail istniejącego klienta. Zamiast wywoływać table_operation::insert_entitymetodę , ten kod używa metody table_operation::replace_entity. Takie podejście powoduje, że jednostka zostanie w pełni zastąpiona na serwerze, chyba że jednostka na serwerze uległa zmianie od czasu jej pobrania. Jeśli operacja została zmieniona, operacja zakończy się niepowodzeniem. Ten błąd uniemożliwia aplikacji zastąpienie zmiany wprowadzonej między pobieraniem i aktualizacją przez inny składnik. Właściwą obsługą tego błędu jest ponowne pobranie jednostki, wprowadzenie zmian, jeśli nadal jest prawidłowe, a następnie wykonanie innej table_operation::replace_entity operacji.

// 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);

Wstawianie lub zastępowanie jednostki

table_operation::replace_entity operacje kończą się niepowodzeniem, jeśli jednostka została zmieniona, ponieważ została pobrana z serwera. Ponadto należy najpierw pobrać jednostkę z serwera, aby table_operation::replace_entity zakończyć się powodzeniem. Czasami nie wiadomo, czy jednostka istnieje na serwerze. Bieżące wartości przechowywane w nim są nieistotne, ponieważ aktualizacja powinna zastąpić je wszystkie. Aby osiągnąć ten wynik, użyj table_operation::insert_or_replace_entity operacji. Ta operacja wstawia jednostkę, jeśli nie istnieje. Operacja zastępuje jednostkę, jeśli istnieje. W poniższym przykładzie kodu jednostka klienta dla Jeff Smith programu jest nadal pobierana, ale następnie jest zapisywana z powrotem na serwerze przy użyciu polecenia table_operation::insert_or_replace_entity. Wszelkie aktualizacje wprowadzone w jednostce między operacjami pobrania i aktualizacji zostaną zastąpione.

// 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);

Tworzenie zapytania do podzbioru właściwości jednostki

Za pomocą zapytania wykonywanego względem tabeli można pobrać tylko kilka właściwości z jednostki. Zapytanie w poniższym kodzie używa table_query::set_select_columns metody , aby zwrócić tylko adresy e-mail jednostek w tabeli.

// 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;
}

Uwaga

Pobieranie wybranych właściwości z jednostki za pomocą zapytania będzie bardziej wydajne niż pobieranie wszystkich właściwości.

Usuń zawartość

Usuwanie encji

Jednostkę można usunąć po jej pobraniu. Po pobraniu jednostki wywołaj table_operation::delete_entity metodę , aby usunąć jednostkę. Następnie wywołaj metodę cloud_table.execute . Poniższy kod pobiera i usuwa jednostkę z kluczem Smith partycji i kluczem wiersza .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);  

Usuń tabelę

Poniższy przykład kodu usuwa tabelę z konta magazynu. Usunięta tabela jest niedostępna do ponownego utworzenia po usunięciu.

// 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";
}

Rozwiązywanie problemów

W przypadku programu Visual Studio Community Edition, jeśli projekt otrzymuje błędy kompilacji z powodu dołączania plików storage_account.h i table.h, usuń przełącznik /permissive- compiler:

  1. Kliknij prawym przyciskiem myszy projekt w Eksploratorze rozwiązań i wybierz pozycję Właściwości.
  2. W oknie dialogowym Strony właściwości rozwiń węzeł Właściwości konfiguracji, rozwiń węzeł C/C++, a następnie wybierz pozycję Język.
  3. Zmień ustawienie Tryb zgodności na Nie.

Następne kroki

Microsoft Azure Storage Explorer jest bezpłatną aplikacją autonomiczną oferowaną przez firmę Microsoft, która umożliwia wizualną pracę z danymi w usłudze Azure Storage w systemach Windows, macOS i Linux.

Skorzystaj z poniższych linków, aby dowiedzieć się więcej o usłudze Azure Storage i interfejsie API dla tabel w usłudze Azure Cosmos DB: