將運算式與 Amazon DynamoDB 和 AWS SDK for .NET - AWS SDK for .NET

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

將運算式與 Amazon DynamoDB 和 AWS SDK for .NET

注意

本主題中的資訊特定於以 .NET Framework 和 3.3 AWS SDK for .NET 版及更早版本為基礎的專案。

下列程式碼範例示範如何使用透過運算式 AWS SDK for .NET 對 DynamoDB 進行程式設計。運算式表示您想要從 DynamoDB 表格中的項目讀取的屬性。您也可以在寫入項目時使用表達式來表示必須符合的任何條件 (也稱為條件更新) 及屬性的更新方式。有些更新範例是以新值取代屬性,或新增資料到清單或對應表。如需詳細資訊,請參閱使用運算式讀取和寫入項目

範例資料

本主題中的程式碼範例取決於 DynamoDB 表格中名為的下列兩個範例項目。ProductCatalog這些項目描述有關產品項目存放在虛構自行車目錄的資訊。這些項目以案例研究:A ProductCatalog 項目中提供的範例為基礎。資料類型描述項如 BOOLLMNNSSSS,對應到 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.GetItem 方法和一組可取得並列印具有 Id205 項目的運算式。只有下列項目的屬性會傳回:IdTitleDescriptionColorRelatedItemsPicturesProductReviews

// 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 301 的項目,但只限於 Price 值大於 150 時。只有下列項目的屬性會傳回:IdTitle 和所有在 ProductReviewsThreeStar 屬性。

// 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 屬性指定預留位置 #pr 代表 ProductReviews 屬性,而預留位置 #p 代表 Price 屬性。#pr.ThreeStar 指定只傳回 ThreeStar 屬性。ExpressionAttributeValues 屬性指定預留位置 :val 代表值 150FilterExpression 屬性指定 #p (Price) 必須大於 :val (150)。對 PrintItem 的呼叫意指自訂功能,如列印項目中所述。

使用運算式和其他項目屬性來取得多重項目

以下範例具備 Amazon.DynamoDBv2.AmazonDynamoDBClient.Scan 方法和一組可取得的運算式,然後列印所有具有 BikeProductCategory 項目。只有下列項目的屬性會傳回: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 代表值 BikeFilterExpression 屬性指定 #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 屬性,可進行評估,以決定列印屬性的正確格式。這些屬性包括 BBOOLBSLMNNSNULLSSS,這些分別對應於那些JSON 資料格式。對於像 BNNULLS 的屬性,如果對應的屬性不是 null,則屬性是對應的非 null 資料類型。對於諸如BS、、LMNS、和之類的屬性SS,如果大Count於零,則屬性為對應的 non-zero-value 數據類型。如果屬 data-type-specific 性的所有內容都是nullCount等於零,則屬性對應於資BOOL料類型。

使用運算式建立或取代項目

以下範例具備 Amazon.DynamoDBv2.AmazonDynamoDBClient.PutItem 方法和一組可取得的運算式,可更新具有 18-Bicycle 301Title 的項目。如果項目尚未存在,則會新增新的項目。

// 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 301ConditionExpression 屬性指定 #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; }

在上述範例中,具範例資料的範例項目會傳回給呼叫者。建構一系列屬性和對應的值,使用資料類型例如 BOOLLMNNSS、和SS、分別對應於那些 JSON 資料格式

使用運算式更新項目

以下範例具備 Amazon.DynamoDBv2.AmazonDynamoDBClient.UpdateItem 方法和一組運算式,為具有 Id 301 的項目變更 Title18" Girl's Bike

// 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 BikeUpdateExpression 屬性指定變更 #title (Title) 為 :newproduct (18" Girl's Bike)。

使用運算式刪除項目

以下範例具備 Amazon.DynamoDBv2.AmazonDynamoDBClient.DeleteItem 方法和一組表達式,可刪除具有 Id 301 的項目,但只有項目的 Title18-Bicycle 301

// 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 301ConditionExpression 屬性指定 #title (Title) 必須等於 :product (18-Bicycle 301)。

詳細資訊

如需詳細資訊和編碼範例,請參閱: