AWS SDK for .NET での Table.Scan メソッド
Scan
メソッドではテーブル全体のスキャンが実行されます。ここでは 2 つのオーバーロードを使用できます。Scan
メソッドで必要とされるパラメータは、次のオーバーロードを使用して指定できる、スキャンフィルタだけです。
例
Scan(ScanFilter filter);
たとえば、フォーラムスレッドのテーブルを維持していて、そこではスレッドの件名 (プライマリ)、関連メッセージ、スレッドが属するフォーラムの Id
、Tags
などの情報を追跡しているとします。スレッドの件名がプライマリキーであるとします。
例
Thread(Subject, Message, ForumId, Tags, LastPostedDateTime, .... )
これは、AWS フォーラムで見られるフォーラムやスレッドを簡略化したものです (「ディスカッションフォーラムForumId
はプライマリキーではないため、この例ではテーブルをスキャンしています。ScanFilter
には 2 つの条件が含まれています。クエリによって、両方の条件を満たすすべてのスレッドが返されます。
例
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# コード例では、前述のものと同じ (ForumId
が 101
であり、Tag
属性に「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 未満である製品を検索しています。この例は、2 つの 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); } } } }