Amazon SDK for .NET 的 Table.Scan 方法 - Amazon DynamoDB
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

Amazon SDK for .NET 的 Table.Scan 方法

Scan 方法执行全表扫描。它提供两个重载。Scan 方法只有一个必需参数:扫描筛选条件。您可以使用以下重载提供扫描筛选条件。

Scan(ScanFilter filter);

例如,假如您维护了一个表,其中记录论坛话题跟踪信息(例如话题主题(主要)、相关消息、话题所属的论坛 IdTags 和其他信息)。同时假定主题就是主键。

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

这就是您在 Amazon 论坛(请参阅讨论论坛)上看到的论坛和话题的简化版本。以下 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.ScanScanFilter 数据元作为参数。

    您可以在只传入必需参数时传递 ScanFilter 参数。

  • Table.ScanScanOperationConfig 数据元作为参数。

    如果您要向 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); } } } }