本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
在 Amazon DynamoDB 上使用表达式和 Amazon SDK for .NET
注意
本主题中的信息特定于基于的项目。 NET框架和 3.3 及更早 Amazon SDK for .NET 版本。
以下代码示例演示如何使用通过表达式对 DynamoDB Amazon SDK for .NET 进行编程。表达式表示您希望从 DynamoDB 表中的某个项目读取到的属性。您还可以在编写项目时使用表达式指示任意必需满足的条件(也称为有条件更新),以及需要如何更新属性。一些更新示例使用新值替换属性,或者将新数据添加到列表或映射。有关更多信息,请参阅使用表达式读取和写入项目。
示例数据
此主题中的代码示例依赖于名为 ProductCatalog
的 DynamoDB 表中的以下两个示例项目。这些项目描述一家虚构自行车商店目录中产品条目的相关信息。这些项目基于案例研究:A Ite ProductCatalog m 中提供的示例。诸如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.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
以及所有 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
属性指定占位符 #pr
(表示 ProductReviews
属性)和占位符 #p
(表示 Price
属性)。#pr.ThreeStar
指定只返回 ThreeStar
属性。ExpressionAttributeValues
属性指定占位符 :val
来表示值 150
。FilterExpression
属性指定 #p
(Price
) 必须大于 :val
(150
)。对 PrintItem
的调用引用自定义函数,如输出项目中所述。
使用表达式和其他项目属性获取多个项目
以下示例介绍了 Amazon.DynamoDBv2.AmazonDynamoDBClient.Scan
方法和一组表达式,用于获取并输出 ProductCategory
为 Bike
的所有项目。只返回项目的以下属性: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数据格式中的属性。对于 B
、N
、NULL
和 S
等属性,如果对应属性不是 null
,则属性是对应的非 null
数据类型。对于诸如BS
、、L
、M
NS
SS
、和之类的属性,如果Count
大于零,则该属性属于相应 non-zero-value的数据类型。如果该属性的所有属性均为null
或Count
等于零,则该属性对应于BOOL
数据类型。 data-type-specific
使用表达式创建或替换项目
以下示例介绍了 Amazon.DynamoDBv2.AmazonDynamoDBClient.PutItem
方法和一组表达式,用于更新 Title
为 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; }
在上述示例中,具有示例数据的示例项目返回到调用方。使用与 “数据格式” 中的数据类型相对应的数据类型(例如BOOL
、L
、M
、N
、NS
S
SS
、和)构造一系列属性和对应的JSON值。
使用表达式更新项目
以下示例介绍了 Amazon.DynamoDBv2.AmazonDynamoDBClient.UpdateItem
方法和一组表达式,用于将 Title
为 18" Girl's Bike
的项目的 Id
更改为 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
)。
使用表达式删除项目
以下示例介绍了 Amazon.DynamoDBv2.AmazonDynamoDBClient.DeleteItem
方法和一组表达式,仅在项目的 Title
为 18-Bicycle 301
时,删除 Id
为 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
)。
更多信息
有关更多信息以及代码示例,请参阅: