将表达式用于 Amazon DynamoDB 以及Amazon SDK for .NET - Amazon SDK for .NET
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

将表达式用于 Amazon DynamoDB 以及Amazon SDK for .NET

注意

本主题中的信息特定于基于 .NET 框架和Amazon SDK for .NET版本 3.3 及更早版本。

以下代码示例演示了如何使用Amazon SDK for .NET用表达式对 DynamoDB 进行编程。表达式表示您希望从 DynamoDB 表中的某个项目读取到的属性。您还可以在编写项目时使用表达式指示任意必需满足的条件(也称为有条件更新),以及需要如何更新属性。一些更新示例使用新值替换属性,或者将新数据添加到列表或映射。有关更多信息,请参阅使用表达式读取和写入项目

示例数据

此主题中的代码示例依赖于名为的 DynamoDB 表中的以下两个示例项目。ProductCatalog. 这些项目描述一家虚构自行车商店目录中产品条目的相关信息。这些项目以中提供的示例为基础。案例研究:一个 ProductCatalog Item. 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 时,获取并输出 Price150 的项目。只返回项目的以下属性: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 属性指定占位符 #pr(表示 ProductReviews 属性)和占位符 #p(表示 Price 属性)。#pr.ThreeStar 指定只返回 ThreeStar 属性。ExpressionAttributeValues 属性指定占位符 :val 来表示值 150FilterExpression 属性指定 #p (Price) 必须大于 :val (150)。对 PrintItem 的调用引用自定义函数,如输出项目中所述。

使用表达式和其他项目属性获取多个项目

以下示例介绍了 Amazon.DynamoDBv2.AmazonDynamoDBClient.Scan 方法和一组表达式,用于获取并输出 ProductCategoryBike 的所有项目。只返回项目的以下属性: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 数据类型。对于诸如这样的属性BSLMNS, 和SS,如果Count大于零,那么属性是相应的 non-zero-value 数据类型。如果所有属性 data-type-specific 属性要么null或者Count等于零,那么属性对应于BOOL数据类型。

使用表达式创建或替换项目

以下示例介绍了 Amazon.DynamoDBv2.AmazonDynamoDBClient.PutItem 方法和一组表达式,用于更新 Title18-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 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; }

在上述示例中,具有示例数据的示例项目返回到调用方。构建了一系列属性和对应值,使用 BOOLLMNNSSSS 等数据类型,它们对应于 JSON 数据格式中的那些类型。

使用表达式更新项目

以下示例介绍了 Amazon.DynamoDBv2.AmazonDynamoDBClient.UpdateItem 方法和一组表达式,用于将 Title18" 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 BikeUpdateExpression 属性指定将 #title (Title) 更改为 :newproduct (18" Girl's Bike)。

使用表达式删除项目

以下示例介绍了 Amazon.DynamoDBv2.AmazonDynamoDBClient.DeleteItem 方法和一组表达式,仅在项目的 Title18-Bicycle 301 时,删除 Id301 的项目。

// 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)。

更多信息

有关更多信息以及代码示例,请参阅: