本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
搭配 Amazon DynamoDB 和 使用運算式 AWS SDK for .NET
注意
本主題中的資訊專屬於以 .NET 為基礎的專案 架構和 3.3 AWS SDK for .NET 版及更早版本。
下列程式碼範例示範如何使用 AWS SDK for .NET 來程式設計 DynamoDB 與表達式。運算式表示您要從 DynamoDB 資料表中的項目讀取的屬性。您也可以在寫入項目時使用表達式來表示必須符合的任何條件 (也稱為條件更新) 及屬性的更新方式。有些更新範例是以新值取代屬性,或新增資料到清單或對應表。如需詳細資訊,請參閱使用運算式讀取和寫入項目。
主題
範例資料
本主題中的程式碼範例依賴名為 的 DynamoDB 資料表中的下列兩個範例項目ProductCatalog
。這些項目描述有關產品項目存放在虛構自行車目錄的資訊。這些項目是根據案例研究: ProductCatalog 項目中提供的範例。資料類型描述詞,例如 BOOL
、L
、M
、N
NS
、、 S
和 ,與 JSON Data Format 中的描述詞SS
相對應。
{ "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
方法和一組可取得並列印具有 Id
的 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
301
的項目,但只限於 Price
值大於 150
時。只有下列項目的屬性會傳回:Id
、Title
和所有在 ProductReviews
的 ThreeStar
屬性。
// 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
代表值 150
。FilterExpression
屬性指定 #p
(Price
) 必須大於 :val
(150
)。對 PrintItem
的呼叫意指自訂功能,如列印項目中所述。
使用運算式和其他項目屬性來取得多重項目
以下範例具備 Amazon.DynamoDBv2.AmazonDynamoDBClient.Scan
方法和一組可取得的運算式,然後列印所有具有 Bike
的 ProductCategory
項目。只有下列項目的屬性會傳回:Id
、Title
和所有在 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 屬性,可進行評估,以判斷列印屬性的正確格式。這些屬性包括 B
、BOOL
、BS
、L
、M
、N
NS
、NULL
、、S
、 和 SS
,這些屬性對應於 JSON Data Format 中的屬性。對於像 B
、N
、NULL
和 S
的屬性,如果對應的屬性不是 null
,則屬性是對應的非 null
資料類型。對於 BS
、、NS
、 L
M
和 等屬性SS
,如果Count
大於零,則屬性為對應的 non-zero-value 資料類型。如果所有屬性的 data-type-specific 屬性都是 null
或Count
等於零,則屬性會對應至BOOL
資料類型。
使用運算式建立或取代項目
以下範例具備 Amazon.DynamoDBv2.AmazonDynamoDBClient.PutItem
方法和一組可取得的運算式,可更新具有 18-Bicycle
301
的 Title
的項目。如果項目尚未存在,則會新增新的項目。
// 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; }
在上述範例中,具範例資料的範例項目會傳回給呼叫者。系統會使用 BOOL
、L
、、、S
、M
N
NS
、 和 等資料類型建構一系列屬性和對應的值SS
,這些資料類型對應於 JSON Data Format 中的這些類型。
使用運算式更新項目
以下範例具備 Amazon.DynamoDBv2.AmazonDynamoDBClient.UpdateItem
方法和一組運算式,為具有 Id
301
的項目變更 Title
到 18" 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 Bike
。UpdateExpression
屬性指定變更 #title
(Title
) 為 :newproduct
(18" Girl's
Bike
)。
使用運算式刪除項目
以下範例具備 Amazon.DynamoDBv2.AmazonDynamoDBClient.DeleteItem
方法和一組表達式,可刪除具有 Id
301
的項目,但只有項目的 Title
為 18-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 301
。ConditionExpression
屬性指定 #title
(Title
) 必須等於 :product
(18-Bicycle
301
)。
詳細資訊
如需詳細資訊和編碼範例,請參閱: