Amazon DynamoDB에서 표현식 사용 및 AWS SDK for .NET - AWS SDK for .NET

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

Amazon DynamoDB에서 표현식 사용 및 AWS SDK for .NET

참고

이 항목의 정보는 .NET Framework 및 AWS SDK for .NET 버전 3.3 및 이전 버전을 기반으로 하는 프로젝트에만 해당됩니다.

다음 코드 예제는 를 사용하여 표현식과 함께 AWS SDK for .NET DynamoDB를 프로그래밍하는 방법을 보여줍니다. 표현식은 DynamoDB 테이블의 항목에서 읽을 속성을 나타냅니다. 또한 항목을 쓸 때도 표현식을 사용하여 충족해야 할 조건(조건부 업데이트라고도 함)을 나타내고 속성을 업데이트하는 방식을 나타낼 수 있습니다. 일부 업데이트 예에서는 속성을 새 값으로 바꾸거나 새 데이터를 목록이나 맵에 추가합니다. 자세한 내용은 표현식을 사용하여 항목 읽기 및 쓰기를 참조하십시오.

샘플 데이터

이 주제의 코드 예제는 ProductCatalog라는 DynamoDB 테이블에 있는 다음 두 가지 예제 항목에 의존합니다. 이 항목들은 가상의 자전거 점포 카탈로그에 있는 제품 항목에 대한 정보를 알려줍니다. 이러한 항목은 사례 연구: A ProductCatalog Item에 제공된 예제를 기반으로 합니다. BOOL, L, M, N, NS, S, SS과 같은 데이터 형식 서술자는 JSON 데이터 형식의 서술자와 일치합니다.

{ "Id": { "N": "205" }, "Title": { "S": "20-Bicycle 205" }, "Description": { "S": "205 description" }, "BicycleType": { "S": "Hybrid" }, "Brand": { "S": "Brand-Company C" }, "Price": { "N": "500" }, "Gender": { "S": "B" }, "Color": { "SS": [ "Red", "Black" ] }, "ProductCategory": { "S": "Bike" }, "InStock": { "BOOL": true }, "QuantityOnHand": { "N": "1" }, "RelatedItems": { "NS": [ "341", "472", "649" ] }, "Pictures": { "L": [ { "M": { "FrontView": { "S": "http://example/products/205_front.jpg" } } }, { "M": { "RearView": { "S": "http://example/products/205_rear.jpg" } } }, { "M": { "SideView": { "S": "http://example/products/205_left_side.jpg" } } } ] }, "ProductReviews": { "M": { "FiveStar": { "SS": [ "Excellent! Can't recommend it highly enough! Buy it!", "Do yourself a favor and buy this." ] }, "OneStar": { "SS": [ "Terrible product! Do not buy this." ] } } } }, { "Id": { "N": "301" }, "Title": { "S": "18-Bicycle 301" }, "Description": { "S": "301 description" }, "BicycleType": { "S": "Road" }, "Brand": { "S": "Brand-Company C" }, "Price": { "N": "185" }, "Gender": { "S": "F" }, "Color": { "SS": [ "Blue", "Silver" ] }, "ProductCategory": { "S": "Bike" }, "InStock": { "BOOL": true }, "QuantityOnHand": { "N": "3" }, "RelatedItems": { "NS": [ "801", "822", "979" ] }, "Pictures": { "L": [ { "M": { "FrontView": { "S": "http://example/products/301_front.jpg" } } }, { "M": { "RearView": { "S": "http://example/products/301_rear.jpg" } } }, { "M": { "SideView": { "S": "http://example/products/301_left_side.jpg" } } } ] }, "ProductReviews": { "M": { "FiveStar": { "SS": [ "My daughter really enjoyed this bike!" ] }, "ThreeStar": { "SS": [ "This bike was okay, but I would have preferred it in my color.", "Fun to ride." ] } } } }

표현식 및 항목의 기본 키를 사용한 단일 항목 조회

다음 예제에는 Amazon.DynamoDBv2.AmazonDynamoDBClient.GetItemId가 있는 항목을 조회하여 출력하는 데 사용할 205 메서드와 일련의 표현식이 포함되어 있습니다. 항목 속성 중에서 Id, Title, Description, Color, RelatedItems, Pictures, ProductReviews 속성만 반환됩니다.

// using Amazon.DynamoDBv2; // using Amazon.DynamoDBv2.Model; var client = new AmazonDynamoDBClient(); var request = new GetItemRequest { TableName = "ProductCatalog", ProjectionExpression = "Id, Title, Description, Color, #ri, Pictures, #pr", ExpressionAttributeNames = new Dictionary<string, string> { { "#pr", "ProductReviews" }, { "#ri", "RelatedItems" } }, Key = new Dictionary<string, AttributeValue> { { "Id", new AttributeValue { N = "205" } } }, }; var response = client.GetItem(request); // PrintItem() is a custom function. PrintItem(response.Item);

앞서 다룬 예제에서 ProjectionExpression 속성은 반환될 속성을 지정합니다. ExpressionAttributeNames 속성에서는 #pr 속성을 나타낼 자리표시자 ProductReviews#ri 속성을 나타낼 자리표시자 RelatedItems를 지정합니다. PrintItem에 대한 호출은 항목 인쇄의 설명과 같이 사용자 지정 함수를 참조합니다.

표현식 및 테이블의 기본 키를 사용한 다수 항목 조회

다음 예제에는 Amazon.DynamoDBv2.AmazonDynamoDBClient.Query의 값이 Id보다 큰 경우에 한해 301Price가 있는 항목을 조회하여 출력하는 데 사용할 150 메서드와 일련의 표현식이 포함되어 있습니다. 항목의 속성 중에서 반환되는 속성은 IdTitle, 그리고 ThreeStar의 모든 ProductReviews 속성뿐입니다.

// using Amazon.DynamoDBv2; // using Amazon.DynamoDBv2.Model; var client = new AmazonDynamoDBClient(); var request = new QueryRequest { TableName = "ProductCatalog", KeyConditions = new Dictionary<string,Condition> { { "Id", new Condition() { ComparisonOperator = ComparisonOperator.EQ, AttributeValueList = new List<AttributeValue> { new AttributeValue { N = "301" } } } } }, ProjectionExpression = "Id, Title, #pr.ThreeStar", ExpressionAttributeNames = new Dictionary<string, string> { { "#pr", "ProductReviews" }, { "#p", "Price" } }, ExpressionAttributeValues = new Dictionary<string,AttributeValue> { { ":val", new AttributeValue { N = "150" } } }, FilterExpression = "#p > :val" }; var response = client.Query(request); foreach (var item in response.Items) { // Write out the first page of an item's attribute keys and values. // PrintItem() is a custom function. PrintItem(item); Console.WriteLine("====="); }

앞서 다룬 예제에서 ProjectionExpression 속성은 반환될 속성을 지정합니다. ExpressionAttributeNames 속성은 ProductReviews 속성을 나타내는 자리 표시자 #prPrice 속성을 나타내는 자리 표시자 #p를 지정합니다. #pr.ThreeStarThreeStar 속성만 반환하도록 지정합니다. ExpressionAttributeValues 속성에서는 :val이라는 값을 나타낼 자리표시자 150을 지정합니다. FilterExpression 속성은 #p(Price)가 :val(150)보다 커야 한다는 조건을 지정합니다. PrintItem에 대한 호출은 항목 인쇄의 설명과 같이 사용자 지정 함수를 참조합니다.

표현식 및 기타 항목 속성을 사용한 다수 항목 조회

다음 예제에는 Amazon.DynamoDBv2.AmazonDynamoDBClient.ScanProductCategory가 있는 항목을 모두 조회하여 출력하는 데 사용할 Bike 메서드와 일련의 표현식이 포함되어 있습니다. 항목의 속성 중에서 반환되는 속성은 IdTitle, 그리고 ProductReviews의 모든 속성뿐입니다.

// using Amazon.DynamoDBv2; // using Amazon.DynamoDBv2.Model; var client = new AmazonDynamoDBClient(); var request = new ScanRequest { TableName = "ProductCatalog", ProjectionExpression = "Id, Title, #pr", ExpressionAttributeValues = new Dictionary<string,AttributeValue> { { ":catg", new AttributeValue { S = "Bike" } } }, ExpressionAttributeNames = new Dictionary<string, string> { { "#pr", "ProductReviews" }, { "#pc", "ProductCategory" } }, FilterExpression = "#pc = :catg", }; var response = client.Scan(request); foreach (var item in response.Items) { // Write out the first page/scan of an item's attribute keys and values. // PrintItem() is a custom function. PrintItem(item); Console.WriteLine("====="); }

앞서 다룬 예제에서 ProjectionExpression 속성은 반환될 속성을 지정합니다. ExpressionAttributeNames 속성에서는 #pr 속성을 나타낼 자리표시자 ProductReviews#pc 속성을 나타낼 자리표시자 ProductCategory를 지정합니다. ExpressionAttributeValues 속성에서는 :catg이라는 값을 나타낼 자리표시자 Bike을 지정합니다. FilterExpression 속성에서는 #pc(ProductCategory)가 :catg(Bike)와 같아야 한다는 조건을 지정합니다. PrintItem에 대한 호출은 항목 인쇄의 설명과 같이 사용자 지정 함수를 참조합니다.

항목 출력

다음 예제에서는 항목의 속성과 값을 출력하는 방법을 보여줍니다. 이 예제는 표현식 및 항목의 기본 키를 사용하여 단일 항목 가져오기, 표현식 및 테이블의 기본 키를 사용하여 다중 항목 가져오기, 표현식 및 기타 항목 속성을 사용하여 다중 항목 가져오기 방법을 보여주는 앞의 예제에서 사용됩니다.

// using Amazon.DynamoDBv2.Model; // Writes out an item's attribute keys and values. public static void PrintItem(Dictionary<string, AttributeValue> attrs) { foreach (KeyValuePair<string, AttributeValue> kvp in attrs) { Console.Write(kvp.Key + " = "); PrintValue(kvp.Value); } } // Writes out just an attribute's value. public static void PrintValue(AttributeValue value) { // Binary attribute value. if (value.B != null) { Console.Write("Binary data"); } // Binary set attribute value. else if (value.BS.Count > 0) { foreach (var bValue in value.BS) { Console.Write("\n Binary data"); } } // List attribute value. else if (value.L.Count > 0) { foreach (AttributeValue attr in value.L) { PrintValue(attr); } } // Map attribute value. else if (value.M.Count > 0) { Console.Write("\n"); PrintItem(value.M); } // Number attribute value. else if (value.N != null) { Console.Write(value.N); } // Number set attribute value. else if (value.NS.Count > 0) { Console.Write("{0}", string.Join("\n", value.NS.ToArray())); } // Null attribute value. else if (value.NULL) { Console.Write("Null"); } // String attribute value. else if (value.S != null) { Console.Write(value.S); } // String set attribute value. else if (value.SS.Count > 0) { Console.Write("{0}", string.Join("\n", value.SS.ToArray())); } // Otherwise, boolean value. else { Console.Write(value.BOOL); } Console.Write("\n"); }

위 예제에서 각 속성 값에는 data-type-specific 속성을 인쇄하는 데 사용할 올바른 형식을 결정하기 위해 평가할 수 있는 여러 속성이 있습니다. 이러한 속성에는 JSON 데이터 형식에 해당하는 B, BOOL, BS, L, M, N, NS, NULL, SSS가 포함됩니다. B, N, NULL, S와 같은 속성의 경우에는 상응하는 속성이 null이 아니라면 그 속성은 상응하는 null 아닌 데이터 형식에 대한 것입니다. BS,, L M NSSS, 및 와 같은 속성의 경우 Count 속성이 0보다 크면 속성은 해당 non-zero-value 데이터 유형입니다. 속성의 data-type-specific 모든 속성이 0 중 null 하나이거나 Count 같으면 속성은 BOOL 데이터 유형에 해당합니다.

표현식을 사용한 항목 생성 및 교체

다음 예제에는 Amazon.DynamoDBv2.AmazonDynamoDBClient.PutItemTitle이 있는 항목을 업데이트하는 데 사용할 18-Bicycle 301 메서드와 일련의 표현식이 포함되어 있습니다. 항목이 아직 없으면 새 항목이 추가됩니다.

// using Amazon.DynamoDBv2; // using Amazon.DynamoDBv2.Model; var client = new AmazonDynamoDBClient(); var request = new PutItemRequest { TableName = "ProductCatalog", ExpressionAttributeNames = new Dictionary<string, string> { { "#title", "Title" } }, ExpressionAttributeValues = new Dictionary<string, AttributeValue> { { ":product", new AttributeValue { S = "18-Bicycle 301" } } }, ConditionExpression = "#title = :product", // CreateItemData() is a custom function. Item = CreateItemData() }; client.PutItem(request);

앞서 다룬 예제에서 ExpressionAttributeNames 속성은 #title 속성을 나타낼 자리표시자 Title을 지정합니다. ExpressionAttributeValues 속성에서는 :product이라는 값을 나타낼 자리표시자 18-Bicycle 301을 지정합니다. ConditionExpression 속성에서는 #title(Title)가 :product(18-Bicycle 301)와 같아야 한다는 조건을 지정합니다. CreateItemData에 대한 호출은 다음과 같은 사용자 지정 함수를 참조합니다.

// using Amazon.DynamoDBv2.Model; // Provides a sample item that can be added to a table. public static Dictionary<string, AttributeValue> CreateItemData() { var itemData = new Dictionary<string, AttributeValue> { { "Id", new AttributeValue { N = "301" } }, { "Title", new AttributeValue { S = "18\" Girl's Bike" } }, { "BicycleType", new AttributeValue { S = "Road" } }, { "Brand" , new AttributeValue { S = "Brand-Company C" } }, { "Color", new AttributeValue { SS = new List<string>{ "Blue", "Silver" } } }, { "Description", new AttributeValue { S = "301 description" } }, { "Gender", new AttributeValue { S = "F" } }, { "InStock", new AttributeValue { BOOL = true } }, { "Pictures", new AttributeValue { L = new List<AttributeValue>{ { new AttributeValue { M = new Dictionary<string,AttributeValue>{ { "FrontView", new AttributeValue { S = "http://example/products/301_front.jpg" } } } } }, { new AttributeValue { M = new Dictionary<string,AttributeValue>{ { "RearView", new AttributeValue {S = "http://example/products/301_rear.jpg" } } } } }, { new AttributeValue { M = new Dictionary<string,AttributeValue>{ { "SideView", new AttributeValue { S = "http://example/products/301_left_side.jpg" } } } } } } } }, { "Price", new AttributeValue { N = "185" } }, { "ProductCategory", new AttributeValue { S = "Bike" } }, { "ProductReviews", new AttributeValue { M = new Dictionary<string,AttributeValue>{ { "FiveStar", new AttributeValue { SS = new List<string>{ "My daughter really enjoyed this bike!" } } }, { "OneStar", new AttributeValue { SS = new List<string>{ "Fun to ride.", "This bike was okay, but I would have preferred it in my color." } } } } } }, { "QuantityOnHand", new AttributeValue { N = "3" } }, { "RelatedItems", new AttributeValue { NS = new List<string>{ "979", "822", "801" } } } }; return itemData; }

앞서 다룬 예제에서 샘플 데이터가 있는 예제 항목은 호출자에게 반환됩니다. 일련의 속성과 해당 값은 JSON 데이터 형식에 해당하는 BOOL, L, M, N, NS, SSS 등의 데이터 유형을 사용하여 구성됩니다.

표현식을 사용한 항목 업데이트

다음 예제에는 Amazon.DynamoDBv2.AmazonDynamoDBClient.UpdateItemTitle가 있는 항목에 대해 18" Girl's BikeId로 변경하는 데 사용할 301 메서드와 일련의 표현식이 포함되어 있습니다.

// using Amazon.DynamoDBv2; // using Amazon.DynamoDBv2.Model; var client = new AmazonDynamoDBClient(); var request = new UpdateItemRequest { TableName = "ProductCatalog", Key = new Dictionary<string,AttributeValue> { { "Id", new AttributeValue { N = "301" } } }, ExpressionAttributeNames = new Dictionary<string, string> { { "#title", "Title" } }, ExpressionAttributeValues = new Dictionary<string, AttributeValue> { { ":newproduct", new AttributeValue { S = "18\" Girl's Bike" } } }, UpdateExpression = "SET #title = :newproduct" }; client.UpdateItem(request);

앞서 다룬 예제에서 ExpressionAttributeNames 속성은 #title 속성을 나타낼 자리표시자 Title을 지정합니다. ExpressionAttributeValues 속성에서는 :newproduct이라는 값을 나타낼 자리표시자 18" Girl's Bike을 지정합니다. UpdateExpression 속성에서는 #title(Title)을 :newproduct(18" Girl's Bike)로 변경하도록 지정합니다.

표현식을 사용한 항목 삭제

다음 예제에는 항목의 Title18-Bicycle 301인 경우에 한해 Id301인 항목을 삭제하는 데 사용할 Amazon.DynamoDBv2.AmazonDynamoDBClient.DeleteItem 메서드와 일련의 표현식이 포함되어 있습니다.

// using Amazon.DynamoDBv2; // using Amazon.DynamoDBv2.Model; var client = new AmazonDynamoDBClient(); var request = new DeleteItemRequest { TableName = "ProductCatalog", Key = new Dictionary<string,AttributeValue> { { "Id", new AttributeValue { N = "301" } } }, ExpressionAttributeNames = new Dictionary<string, string> { { "#title", "Title" } }, ExpressionAttributeValues = new Dictionary<string, AttributeValue> { { ":product", new AttributeValue { S = "18-Bicycle 301" } } }, ConditionExpression = "#title = :product" }; client.DeleteItem(request);

앞서 다룬 예제에서 ExpressionAttributeNames 속성은 #title 속성을 나타낼 자리표시자 Title을 지정합니다. ExpressionAttributeValues 속성에서는 :product이라는 값을 나타낼 자리표시자 18-Bicycle 301을 지정합니다. ConditionExpression 속성에서는 #title(Title)이 :product(18-Bicycle 301)와 같아야 한다는 조건을 지정합니다.

추가 정보

자세한 내용 및 코드 예제는 다음을 참조하십시오.