| « PreviousNext » | |
![]() ![]() ![]() | Did this page help you? Yes | No | Tell us about it... |
The Scan method performs a full table scan. It provides two
overloads. The only parameter required by the Scan method is the scan
filter which you can provide using the following overload.
Scan(ScanFilter filter);
For example, assume that you maintain a table of forum threads tracking information such as thread subject (primary), the related message, forum Id to which the thread belongs, Tags, a multivalued attribute for keywords, and other information. Assume that the subject is the primary key.
Thread(Subject, Message, ForumId, Tags, LastPostedDateTime, .... )
This is a simplified version of forums and threads that you see on AWS forums (see
Discussion
Forums). The following C# code snippet queries all threads in a specific
forum (ForumId = 101) that are tagged "rangekey". Because the ForumId is not a
primary key, the example scans the table. The ScanFilter includes two
conditions. Query returns all the threads that satisfy both of the
conditions.
string tableName = "Thread";
Table ThreadTable = Table.LoadTable(client, tableName);
ScanFilter scanFilter = new ScanFilter();
scanFilter.AddCondition("ForumId", ScanOperator.Equal, 101);
scanFilter.AddCondition("Tags", ScanOperator.Contains, "rangekey");
Search search = ThreadTable.Scan(scanFilter);
You can also specify optional parameters to Scan, such as a
specific list of attributes to retrieve or whether to perform a strongly consistent read. To specify optional
parameters, you must create a ScanOperationConfig object that
includes both the required and optional parameters and use the following
overload.
Scan(ScanOperationConfig config);
The following C# code snippet executes the same preceding query (find forum
threads in which the ForumId is 101 and the Tag attribute contains the
"rangekey" keyword). However, this time assume that you want to add an optional
parameter to retrieve only a specific attribute list. In this case, you must
create a ScanOperationConfig object by providing all the
parameters, required and optional as shown in the following code example.
string tableName = "Thread";
Table ThreadTable = Table.LoadTable(client, tableName);
ScanFilter scanFilter = new ScanFilter();
scanFilter.AddCondition("ForumId", ScanOperator.Equal, forumId);
scanFilter.AddCondition("Tags", ScanOperator.Contains, "rangekey");
ScanOperationConfig config = new ScanOperationConfig()
{
AttributesToGet = new List<string> { "Subject", "Message" } ,
Filter = scanFilter
};
Search search = ThreadTable.Scan(config);
The Scan operation performs a full table scan making it a
potentially expensive operation. You should use queries instead. However, there
are times when you might need to execute a scan against a table. For example,
you might have a data entry error in the product pricing and you must scan the
table as shown in the following C# code example. The example scans the
ProductCatalog table to find products for which the price value is less than 0.
The example illustrates the use of the two Table.Scan overloads.
Table.Scan that takes the ScanFilter
object as a parameter.
You can pass the ScanFilter parameter when passing
in only the required parameters.
Table.Scan that takes the
ScanOperationConfig object as a parameter.
You must use the ScanOperationConfig parameter if
you want to pass any optional parameters to the Scan
method.
using System;
using System.Collections.Generic;
using System.Linq;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DocumentModel;
namespace Amazon.DynamoDBv2.Documentation
{
class Program
{
private static AmazonDynamoDBClient client;
static void Main(string[] args)
{
var config = new AmazonDynamoDBConfig();
config.ServiceURL = System.Configuration.ConfigurationManager.AppSettings["ServiceURL"];
client = new AmazonDynamoDBClient(config);
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);
}
}
}
}