forked from aws-samples/aws-dynamodb-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransactWriteItems.cs
105 lines (97 loc) · 4.73 KB
/
TransactWriteItems.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
using System.Linq;
namespace DotnetSamples.WorkingWithItems
{
public class TransactWriteItems
{
/// <summary>
/// Example that writes items in a transaction to a DynamoDB
/// </summary>
/// <returns></returns>
public static async Task TransactWriteItemsExample()
{
var client = new AmazonDynamoDBClient(RegionEndpoint.USWest2);
//Creation of the request. As an example, we will put an item on a secondary table, delete it from the original table and update the status on a third table if the current status is active
var request = new TransactWriteItemsRequest()
{
TransactItems = new List<TransactWriteItem>()
{
//Put
new TransactWriteItem()
{
Put = new Put()
{
TableName = "RetailDatabase2",
Item = new Dictionary<string, AttributeValue>
{
{"pk", new AttributeValue("[email protected]")},
{"sk", new AttributeValue("metadata")},
{"attribute1",new AttributeValue("Attribute1 value") }
//Add more attributes
}
}
},
//Delete
new TransactWriteItem()
{
Delete = new Delete()
{
TableName = "RetailDatabase",
Key = new Dictionary<string, AttributeValue>
{
{"pk", new AttributeValue("[email protected]")},
{"sk", new AttributeValue("metadata")},
},
//You could add conditional expresions if you need it
}
},
//Update
new TransactWriteItem()
{
Update = new Update()
{
TableName = "CustomerStatus",
Key = new Dictionary<string, AttributeValue>
{
{ "pk", new AttributeValue("[email protected]") },
},
//We update the #s (status attribute) if the condition is meeted
UpdateExpression = "set #s = :arch",
ConditionExpression = "#s = :a",
ExpressionAttributeNames = new Dictionary<string, string>
{
{ "#s", "status" },
},
ExpressionAttributeValues = new Dictionary<string, AttributeValue>
{
{ ":a", new AttributeValue("active") },
{ ":arch", new AttributeValue("archived") },
},
}
}
},
ReturnItemCollectionMetrics = "SIZE", //Statistics disabled by default with NONE, you could use SIZE to know statistics of the modifed items.
ReturnConsumedCapacity = "TOTAL"
};
try
{
var response = await client.TransactWriteItemsAsync(request);
Console.WriteLine($" Action finished with code: {response.HttpStatusCode}, info: {response.ItemCollectionMetrics.ToString()} ");
}
catch (TransactionCanceledException cancelation)
{
//We catch the error raised when the transaction is cancelled.
Console.WriteLine($"Transaction Cancelled: {string.Join(string.Empty, cancelation.CancellationReasons.Where(c=> c != null).Select(c=> c.Message))}"); //We show a message that list the conditions not met
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}