表中的掃描方法 AWS SDK for .NET - Amazon DynamoDB

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

表中的掃描方法 AWS SDK for .NET

Scan 方法會執行完整資料表掃描。它提供兩種多載。Scan 方法的唯一必要參數為掃描篩選條件,您可以使用以下多載提供。

範例
Scan(ScanFilter filter);

例如,假設您維持一個論壇主題資料表的運作,追蹤像是對話標題 (主要)、相關訊息、對話歸屬的論壇 IdTags 及其他相關資訊。假設標題為主索引鍵。

範例
Thread(Subject, Message, ForumId, Tags, LastPostedDateTime, .... )

這是您在 AWS 論壇上看到的論壇和討論串的簡化版本(請參閱討論區)。下列 C# 程式碼範例會查詢特定論壇 (ForumId = 101) 中標記為「sortkey」的所有執行緒。因 ForumId 不是主索引鍵,範例會掃描資料表。ScanFilter 包含兩個條件。該查詢會傳回所有同時符合這兩個條件的對話。

範例
string tableName = "Thread"; Table ThreadTable = Table.LoadTable(client, tableName); ScanFilter scanFilter = new ScanFilter(); scanFilter.AddCondition("ForumId", ScanOperator.Equal, 101); scanFilter.AddCondition("Tags", ScanOperator.Contains, "sortkey"); Search search = ThreadTable.Scan(scanFilter);

指定選用參數

您也可以為 Scan 指定選用參數,例如要擷取的特定屬性清單,或是是否要執行強烈一致讀取。若要指定選用參數,您必須建立 ScanOperationConfig 物件,其中包含必要和選用參數,並使用以下多載。

範例
Scan(ScanOperationConfig config);

以下 C# 程式碼範例會執行的查詢與前述相同 (尋找 ForumId101Tag 屬性包含「sortkey」關鍵字的論壇主題)。假設您希望新增一個選用參數,只擷取特定屬性清單。在此案例中,您必須藉由提供所有參數 (必要及選用參數) 建立 ScanOperationConfig 物件,如以下程式碼範例所示。

範例
string tableName = "Thread"; Table ThreadTable = Table.LoadTable(client, tableName); ScanFilter scanFilter = new ScanFilter(); scanFilter.AddCondition("ForumId", ScanOperator.Equal, forumId); scanFilter.AddCondition("Tags", ScanOperator.Contains, "sortkey"); ScanOperationConfig config = new ScanOperationConfig() { AttributesToGet = new List<string> { "Subject", "Message" } , Filter = scanFilter }; Search search = ThreadTable.Scan(config);

範例:使用 Table.Scan 方法的掃描

由於 Scan 操作會執行完整資料表掃描,因此屬於潛在上相當昂貴的操作。您應改為使用查詢。但是,有時候您可能需要對資料表執行掃描。例如,您的產品定價中可能含有資料項目錯誤,而必須掃描整個資料表,如以下 C# 程式碼範例所示。範例會掃描 ProductCatalog 資料表,尋找價格值低於 0 的產品。範例會示範使用兩個 Table.Scan 多載。

  • Table.Scan 會接受 ScanFilter 物件做為參數。

    您可以在只傳遞必要參數時,傳遞 ScanFilter 參數。

  • Table.Scan 會接受 ScanOperationConfig 物件做為參數。

    若您希望將任何選用參數傳遞到 ScanOperationConfig 方法,您必須使用 Scan 參數。

範例
using System; using System.Collections.Generic; using System.Linq; using Amazon.DynamoDBv2; using Amazon.DynamoDBv2.DocumentModel; namespace com.amazonaws.codesamples { class MidLevelScanOnly { private static AmazonDynamoDBClient client = new AmazonDynamoDBClient(); static void Main(string[] args) { Table productCatalogTable = Table.LoadTable(client, "ProductCatalog"); // Scan example. FindProductsWithNegativePrice(productCatalogTable); FindProductsWithNegativePriceWithConfig(productCatalogTable); Console.WriteLine("To continue, press Enter"); Console.ReadLine(); } private static void FindProductsWithNegativePrice(Table productCatalogTable) { // Assume there is a price error. So we scan to find items priced < 0. ScanFilter scanFilter = new ScanFilter(); scanFilter.AddCondition("Price", ScanOperator.LessThan, 0); Search search = productCatalogTable.Scan(scanFilter); List<Document> documentList = new List<Document>(); do { documentList = search.GetNextSet(); Console.WriteLine("\nFindProductsWithNegativePrice: printing ............"); foreach (var document in documentList) PrintDocument(document); } while (!search.IsDone); } private static void FindProductsWithNegativePriceWithConfig(Table productCatalogTable) { // Assume there is a price error. So we scan to find items priced < 0. ScanFilter scanFilter = new ScanFilter(); scanFilter.AddCondition("Price", ScanOperator.LessThan, 0); ScanOperationConfig config = new ScanOperationConfig() { Filter = scanFilter, Select = SelectValues.SpecificAttributes, AttributesToGet = new List<string> { "Title", "Id" } }; Search search = productCatalogTable.Scan(config); List<Document> documentList = new List<Document>(); do { documentList = search.GetNextSet(); Console.WriteLine("\nFindProductsWithNegativePriceWithConfig: printing ............"); foreach (var document in documentList) PrintDocument(document); } while (!search.IsDone); } private static void PrintDocument(Document document) { // count++; Console.WriteLine(); foreach (var attribute in document.GetAttributeNames()) { string stringValue = null; var value = document[attribute]; if (value is Primitive) stringValue = value.AsPrimitive().Value.ToString(); else if (value is PrimitiveList) stringValue = string.Join(",", (from primitive in value.AsPrimitiveList().Entries select primitive.Value).ToArray()); Console.WriteLine("{0} - {1}", attribute, stringValue); } } } }