كيفية استخدام تخزين Azure Table وواجهة برمجة تطبيقات الجدول Azure Cosmos DB بالاستعانة بلغة C++
ينطبق على:
واجهة برمجة تطبيقات Table
تلميح
ينطبق المحتوى في هذه المقالة على Azure Table storage، وAzure Cosmos DB Table API. إن Azure Cosmos DB Table API هي عرض مميز لمساحة تخزين الجدول الذي يوفر جداول محسنة لسرعة معدل النقل والتوزيع على مستوى عالمي والفهارس الثانوية التلقائية.
يوضح لك هذا الدليل السيناريوهات الشائعة باستخدام خدمة تخزين Azure Table أو واجهة برمجة تطبيقات الجدول Azure Cosmos DB. تمت كتابة العينات بلغة C++ واستخدام مكتبة عميل تخزين Azure لـC ++. تتناول هذه المقالة السيناريوهات التالية:
- إنشاء جدول وحذفه
- العمل مع كيانات الجدول
ملاحظة
يستهدف هذا الدليل مكتبة عميل تخزين Azure الإصدار C++ 1.0.0 وما بعده. الإصدار المُوصى به هو مكتبة Storage Client Library الإصدار 2.2.0، الذي يتوفر باستخدام NuGet أو GitHub.
إنشاء حسابات
أنشئ حساب تخزين في Azure
يُمكن التعامل مع الجداول باستخدامAzure Table storage أوAZURE Cosmos DB. لمعرفة المزيد حول الاختلافات بين عروض الجدول في هاتين الخدمتين، يُرجى الرجوع إلى مقال عروض الجدول. يلزم إنشاء حساب للخدمة المراد استخدامها. تُوضح الأقسام التالية كيفية إنشاء كل منAzure Table storage وAzure Cosmos DB account، ولكن يُمكن استخدام واحد منها فقط.
إنشاء حساب تخزين Azure
تتمثل أسهل طريقة لإنشاء حساب تخزين Azure في استخدام مدخل Azure. لمعرفة المزيد، اطلع على إنشاء حساب تخزين.
ويمكنك أيضاً إنشاء حساب تخزين Azure باستخدام Azure PowerShell أو Azure CLI.
وإذا كنت تفضل عدم إنشاء حساب تخزين في الوقت الحالي، فيمكنك أيضاً استخدام Azure Storage Emulator لتشغيل التعليمات البرمجية واختبارها في بيئة محلية. لمزيد من المعلومات، اطلع على استخدام Azure Storage Emulator للتطوير والاختبار.
إنشاء حساب برمجة التطبيقات للجداول في Azure Cosmos DB
للحصول على إرشادات حول Azure Cosmos DB Table API account، يُرجى الرجوع إلى إنشاء حساب قاعدة البيانات.
إنشاء تطبيق C++ جديد
في هذا الدليل، يمكنك استخدام ميزات التخزين من تطبيق C++. للقيام بذلك، بادر بتثبيت مكتبة عميل التخزين Azure ل C++.
لتثبيت مكتبة عميل التخزين Azure ل C++، استخدم الطرق التالية:
- Linux: اتبع الإرشادات الواردة في مكتبة عميل التخزين Azure لملف C++ التمهيدي: الشروع في العمل على صفحة Linux.
- Windows: على Windows، استخدم vcpkg كمدير التبعية. اتبع البدء السريع لتهيئة vcpkg. ثم استخدم الأمر التالي لتثبيت المكتبة:
.\vcpkg.exe install azure-storage-cpp
يمكنك العثور على دليل بشأن كيفية إنشاء الكود المصدري والتصدير إلى Nuget في ملف README.
تكوين الوصول إلى مكتبة عميل الجدول
لاستخدام واجهات برمجة تطبيقات تخزين Azure للوصول إلى الجداول، أضف عبارات include التالية إلى أعلى ملف C++:
#include <was/storage_account.h>
#include <was/table.h>
يستخدم عميل تخزين Azure أو Cosmos DB سلسلة اتصال لتخزين نقاط النهاية وبيانات الاعتماد للوصول إلى خدمات إدارة البيانات. عند تشغيل تطبيق عميل، يجب توفير سلسلة اتصال التخزين أو سلسلة اتصال Azure Cosmos DB بالتنسيق المناسب.
بإعداد سلسلة اتصال Azure Storage
يوضح هذا المثال كيفية إعلان حقل ثابت للاحتفاظ بسلسلة اتصال 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>"));
استخدم اسم حساب التخزين الخاص بك على <your_storage_account>. بالنسبة إلى <your_storage_account_key>، استخدم مفتاح الاختصار لحساب التخزين المدرج في مدخل Azure. للحصول على معلومات بشأن حسابات التخزين ومفاتيح الوصول، راجع إنشاء حساب تخزين.
إعداد سلسلة اتصال Azure Cosmos DB
يوضح هذا المثال كيفية إعلان حقل ثابت للاحتفاظ بسلسلة اتصال 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>"));
استخدم اسم حساب Azure Cosmos DB الخاص بك على <your_cosmos_db_account>. أدخل مفتاحك الأساسي لـ<your_cosmos_db_account_key>. أدخل نقطة النهاية المدرجة في مدخل Azure لـ<your_cosmos_db_endpoint>.
لاختبار التطبيق الخاص بك على جهاز الكمبيوتر المحلي الذي يعمل بنظام التشغيل Windows، يمكنك استخدام Azure Storage Emulator المثبت مع Azure SDK. يعد Storage Emulator أداة مساعدة تحاكي خدمات Azure Blob وQueue والجدول المتوفرة على جهاز التطوير المحلي لديك. يوضح المثال التالي كيفية إعلان حقل ثابت للاحتفاظ بسلسلة الاتصال بمحاكي التخزين المحلي لديك:
// Define the connection string with Azure Storage Emulator.
const utility::string_t storage_connection_string(U("UseDevelopmentStorage=true;"));
لبدء Azure Storage Emulator، من سطح مكتب Windows، حدد زر البدء أو مفتاح Windows. أدخل Microsoft Azure Storage Emulator وقم بتشغيله. لمزيد من المعلومات، راجع استخدام Azure Storage Emulator للتطوير والاختبار.
استرداد سلسلة الاتصال الخاصة بك
يمكنك استخدام فئة cloud_storage_account لتمثيل معلومات حساب التخزين الخاص بك. لاسترداد معلومات حساب التخزين الخاص بك من سلسلة اتصال التخزين، استخدم parse الطريقة.
// 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. تتيح لك هذه الفئة الحصول على كائنات مرجعية للجداول والكيانات المخزنة في خدمة تخزين الجدول. تُنشئ التعليمات البرمجية التالية كائن cloud_table_client باستخدام كائن حساب التخزين الذي استردته سابقًا:
// Create the table client.
azure::storage::cloud_table_client table_client = storage_account.create_cloud_table_client();
إنشاء كيانات وإضافتها إلى جدول
إنشاء جدول
يتيح لك كائن cloud_table_client الحصول على كائنات مرجعية للجداول والكيانات. تُنشئ التعليمات البرمجية التالية كائن cloud_table_client وتستخدمه لإنشاء جدول جديد.
// 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();
أضف كيانًا إلى جدول
لإضافة كيان إلى جدول، قم بإنشاء كائن table_entity جديد وقم بتمريره إلى table_operation::insert_entity. تستخدم التعليمات البرمجية التالية الاسم الأول للعميل كمفتاح الصف واسم العائلة كمفتاح القسم. يعمل قسم الصف ومفتاح الكيان معًا على تحديد الكيان في الجدول بشكل فريد. يمكن الاستعلام عن الكيانات التي لها مفتاح القسم نفسه بشكل أسرع من الكيانات التي لها مفاتيح أقسام مختلفة. يسمح استخدام مفاتيح الأقسام المتنوعة بإمكانية توسيع عملية متوازية أكبر. لمزيد من المعلومات، راجع قائمة التحقق من أداء التخزين وقابلية التوسع في Microsoft Azure.
تنشئ التعليمة البرمجية التالية مثيلاً جديدًا من table_entity مع تخزين بعض بيانات العميل. تستدعي التعليمة البرمجية بعد ذلك table_operation::insert_entity لإنشاء كائن table_operation لإدراج كيان في جدول، وتربط كيان الجدول الجديد به. وأخيرًا، تستدعي التعليمة البرمجية طريقة execute على كائن cloud_table. يرسل table_operation الجديد طلبًا إلى خدمة الجدول لإدراج كيان العميل الجديد في جدول people.
// 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);
إدراج مجموعة من الكيانات
يمكنك إدراج مجموعة من الكيانات في خدمة الجدول في عملية كتابة واحدة. تقوم التعليمة البرمجية التالية بإنشاء كائن table_batch_operation، ثم تضيف ثلاث عمليات إدراج إليه. تتم إضافة كل عملية إدراج عن طريق إنشاء كائن كيان جديد، وتعيين قيمه، ثم استدعاء طريقة insert على الكائن table_batch_operation لإقران الكيان بعملية إدراج جديدة. بعد ذلك، تستدعي التعليمة البرمجية cloud_table.execute لبدء العملية.
// 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);
بعض الأشياء التي يجب ملاحظتها في العمليات المجمعة:
- يمكنك القيام بما يصل إلى 100 عملية
insert وdelete وmerge وreplace وinsert-or-merge وinsert-or-replace في أي مجموعة دفعة واحدة. - يمكن أن تحتوي العملية الدفعية على عملية استرداد، إذا كانت العملية الوحيدة في الدُفعة.
- يجب أن يكون لجميع الكيانات في عملية دفعة واحدة نفس مفتاح القسم.
- تقتصر العملية الدفعية على حمولة بيانات 4 ميغابايت.
الاستعلام عن الكيانات وتعديلها
استرداد كل الكيانات في قسم
للاستعلام عن جدول لجميع الكيانات في القسم، استخدم كائن table_query. تحدد التعليمة البرمجية التالية المذكورة كمثال عامل تصفية للكيانات حيث يكون Smith هو مفتاح القسم. يطبع هذا المثال حقول كل كيان في نتائج الاستعلام إلى وحدة التحكم.
ملاحظة
هذه الطرق غير مدعومة حاليًا لـ C++ في 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;
}
يعرض الاستعلام في هذا المثال جميع الكيانات التي تطابق معايير التصفية. إذا كانت لديك جداول كبيرة وتحتاج إلى تنزيل كيانات الجدول بشكل متكرر، فإننا نوصي بتخزين بياناتك في كتل تخزين Azure الكبيرة بدلاً من ذلك.
استرداد مجموعة من الكيانات في قسم
إذا كنت لا تريد الاستعلام عن جميع الكيانات في القسم، فإنه يمكنك تحديد نطاق. ادمج مرشح مفتاح القسم مع مرشح مفتاح الصف. يستخدم مثال التعليمة البرمجية التالية مرشحين للحصول على جميع الكيانات في قسم Smith حيث يبدأ مفتاح الصف (الاسم الأول) بحرف سابق لحروف E من الأبجدية، ثم يطبع نتائج الاستعلام.
ملاحظة
هذه الطرق غير مدعومة حاليًا لـ C++ في 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;
}
استرداد كيان واحد
يمكنك كتابة استعلام لاسترداد كيان واحد محدد. تستخدم التعليمة البرمجية التالية table_operation::retrieve_entity لتحديد العميل Jeff Smith. تسترد هذه الطريقة كيانًا واحدًا فقط، بدلاً من مجموعة، وتكون القيمة المُعادة في table_result. يعد تحديد كل من مفاتيح الأقسام والصف في الاستعلام أسرع طريقة لاسترداد كيان واحد من خدمة الجدول.
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;
استبدال كيان
لاستبدال كيان، استرده من خدمة الجدول، وعدّل كائن الكيان، ثم احفظ التغييرات مرة أخرى في خدمة الجدول. تغيّر التعليمة البرمجية التالية رقم هاتف العميل الحالي وعنوان بريده الإلكتروني. بدلاً من استدعاء table_operation::insert_entity، تستخدم هذه التعليمة البرمجية table_operation::replace_entity. يؤدي هذا الأسلوب إلى استبدال الكيان بالكامل على الخادم، ما لم يتغير الكيان الموجود على الخادم منذ استرداده. إذا تم تغييره، تفشل العملية. يمنع هذا الفشل تطبيقك من استبدال تغيير تم إجراؤه بين الاسترداد والتحديث بواسطة مكون آخر. تتمثل المعالجة المناسبة لهذا الفشل في استرداد الكيان مرة أخرى، وإجراء التغييرات، إذا كانت لا تزال صالحة، ثم إجراء عملية table_operation::replace_entity أخرى.
// 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);
إدراج كيان أو استبداله
تفشل عمليات table_operation::replace_entity إذا تم تغيير الكيان منذ استرداده من الخادم. علاوة على ذلك، يجب عليك استرداد الكيان من الخادم أولاً لنجاح table_operation::replace_entity. في بعض الأحيان، لا تعرف ما إذا كان الكيان موجودًا على الخادم. القيم الحالية المخزنة فيه ليست ذات صلة؛ لأن تحديثك يجب أن يستبدلهم جميعًا. لإنجاز هذه النتيجة، استخدم عملية table_operation::insert_or_replace_entity. تُدرج هذه العملية الكيان إذا لم يكن موجودًا. تحل العملية محل الكيان إذا كان موجودًا. في مثال التعليمة البرمجية التالية، لا يزال يتم استرداد كيان العميل لـJeff Smith، لكن يُحفظ مرة أخرى على الخادم باستخدام table_operation::insert_or_replace_entity. ستُستبدل أي تحديثات يتم إجراؤها على الكيان بين عملية الاسترداد والتحديث.
// 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);
الاستعلام عن مجموعة فرعية من خصائص الكيان
يمكن لاستعلام إلى جدول استرداد بعض الخصائص فقط من كيان. يستخدم الاستعلام في التعليمة البرمجية التالية الطريقة table_query::set_select_columns لإرجاع عناوين البريد الإلكتروني للكيانات في الجدول فقط.
// 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;
}
ملاحظة
يعد الاستعلام عن بعض الخصائص من كيان عملية أكثر كفاءة من استرداد كافة الخصائص.
حذف المحتوى
حذف كيان
يمكنك حذف كيان بعد استرداده. بعد استرداد كيان، استدع table_operation::delete_entity بالكيان لحذفه. ثم بادر باستدعاء أسلوب cloud_table.execute. تسترد التعليمة البرمجية التالية وتحذف كيانًا به مفتاح قسم Smith ومفتاح صف 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);
حذف جدول
وأخيرًا، تحذف التعليمة البرمجية التالية المذكورة كمثال جدولاً من حساب التخزين. ويكون الجدول المحذوف غير متاح لإعادة الإنشاء لبعض الوقت بعد الحذف.
// 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";
}
استكشاف الأخطاء وإصلاحها
أما Visual Studio Community Edition، فإذا تعرض مشروعك لأخطاء بناء بسبب تضمين ملفات storage_account.h وtable.h، فبادر بإزالة /permissive- محول التحويل البرمجي:
- في Solution Explorer، انقر بزر الماوس الأيمن فوق مشروعك، وحدد Properties.
- في مربع الحوار Property Pages، قم بتوسيع Configuration Properties، ثم توسيع C/C++، وحدد Language.
- اضبط وضع التوافق على لا.
الخطوات التالية
Microsoft Azure Storage Explorer تطبيق مجاني مستقل من Microsoft يمكّنك من العمل بشكل مرئي مع بيانات Azure Storage على أنظمة التشغيل Windows وmacOS وLinux.
اتبع هذه الارتباطات لمعرفة المزيد عن Azure Storage وواجهة برمجة تطبيقات الجدول في Azure Cosmos DB: