AWS SDK for .NET의 Table.Scan 메서드 - Amazon DynamoDB

AWS SDK for .NET의 Table.Scan 메서드

Scan 메서드는 전체 테이블 스캔을 수행합니다. 이는 두 개의 오버로드를 제공합니다. Scan 메서드에 필요한 유일한 파라미터는 다음 오버로드를 사용하여 제공할 수 있는 스캔 필터입니다.

Scan(ScanFilter filter);

예를 들어, 스레드 제목(기본), 관련 메시지, 스레드가 속한 포럼 Id, Tags 및 기타 정보를 추적하는 포럼 스레드 테이블을 관리하는 경우 주제는 기본 키라고 가정합니다.

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

이는 AWS 포럼에서 볼 수 있는 포럼 및 스레드의 단순화된 버전입니다(토론 포럼 참조). 다음 C# 코드 예제에서는 "sortkey"라는 태그가 지정된 특정 포럼(ForumId = 101)의 모든 스레드를 쿼리합니다. 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# 코드 예제에서는 앞과 동일한 쿼리를 실행합니다(ForumId101이고 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보다 작은 제품을 찾습니다. 이 예에서는 두 개의 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); } } } }