forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLowLevelTableExample.cs
More file actions
205 lines (179 loc) · 6.3 KB
/
LowLevelTableExample.cs
File metadata and controls
205 lines (179 loc) · 6.3 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[dynamodb.dotnetv3.LowLevelTableExample]
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
using Amazon.Runtime;
namespace LowLevelTableExample;
static class LowLevelTableExample
{
private static readonly IAmazonDynamoDB Client = new AmazonDynamoDBClient();
private const string ExampleTableName = "ExampleTable";
static async Task Main()
{
try
{
await CreateExampleTable();
await ListMyTables();
await GetTableInformation();
await UpdateExampleTable();
}
catch (AmazonDynamoDBException e)
{
Console.WriteLine(e.Message);
}
catch (AmazonServiceException e)
{
Console.WriteLine(e.Message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("To continue, press Enter");
Console.ReadLine();
await DeleteExampleTable();
}
}
// snippet-start:[dynamodb.dotnetv3.CreateExampleTable]
private static async Task CreateExampleTable()
{
Console.WriteLine("\n*** Creating table ***");
var response = await Client.CreateTableAsync(new CreateTableRequest
{
AttributeDefinitions = new List<AttributeDefinition>
{
new AttributeDefinition
{
AttributeName = "Id",
AttributeType = ScalarAttributeType.N
},
new AttributeDefinition
{
AttributeName = "ReplyDateTime",
AttributeType = ScalarAttributeType.N
}
},
KeySchema = new List<KeySchemaElement>
{
new KeySchemaElement
{
AttributeName = "Id",
KeyType = KeyType.HASH //Partition key
},
new KeySchemaElement
{
AttributeName = "ReplyDateTime",
KeyType = KeyType.RANGE //Sort key
}
},
TableName = ExampleTableName,
BillingMode = BillingMode.PAY_PER_REQUEST,
});
var tableDescription = response.TableDescription;
Console.WriteLine($"{tableDescription.TableName}: {tableDescription.TableStatus}");
Console.WriteLine($"{ExampleTableName} - {tableDescription.TableStatus}");
await WaitUntilTableReady(ExampleTableName);
}
// snippet-end:[dynamodb.dotnetv3.CreateExampleTable]
// snippet-start:[dynamodb.dotnetv3.ListTableExample]
private static async Task ListMyTables()
{
Console.WriteLine("\n*** Listing tables ***");
string lastTableNameEvaluated = null;
do
{
var response = await Client.ListTablesAsync(new ListTablesRequest
{
Limit = 2,
ExclusiveStartTableName = lastTableNameEvaluated
});
foreach (var name in response.TableNames)
{
Console.WriteLine(name);
}
lastTableNameEvaluated = response.LastEvaluatedTableName;
} while (lastTableNameEvaluated != null);
}
// snippet-end:[dynamodb.dotnetv3.ListTableExample]
// snippet-start:[dynamodb.dotnetv3.DescribeTableExample]
private static async Task GetTableInformation()
{
Console.WriteLine("\n*** Retrieving table information ***");
var response = await Client.DescribeTableAsync(new DescribeTableRequest
{
TableName = ExampleTableName
});
var table = response.Table;
Console.WriteLine($"Name: {table.TableName}");
Console.WriteLine($"# of items: {table.ItemCount}");
}
// snippet-end:[dynamodb.dotnetv3.DescribeTableExample]
// snippet-start:[dynamodb.dotnetv3.UpdateExampleTable]
private static async Task UpdateExampleTable()
{
Console.WriteLine("\n*** Updating table billing mode ***");
await Client.UpdateTableAsync(new UpdateTableRequest
{
TableName = ExampleTableName,
BillingMode = BillingMode.PROVISIONED,
ProvisionedThroughput = new ProvisionedThroughput
{
ReadCapacityUnits = 6,
WriteCapacityUnits = 7
}
});
await WaitUntilTableReady(ExampleTableName);
}
// snippet-end:[dynamodb.dotnetv3.UpdateExampleTable]
// snippet-start:[dynamodb.dotnetv3.DeleteExampleTable]
private static async Task DeleteExampleTable()
{
Console.WriteLine("\n*** Deleting table ***");
try
{
await Client.DeleteTableAsync(new DeleteTableRequest
{
TableName = ExampleTableName
});
Console.WriteLine("Table is being deleted...");
}
catch (ResourceNotFoundException)
{
// Something went wrong during CreateTable.
}
}
// snippet-end:[dynamodb.dotnetv3.DeleteExampleTable]
// snippet-start:[dynamodb.dotnetv3.WaitUntilTableReady]
private static async Task WaitUntilTableReady(string tableName)
{
string status = null;
// Wait until table is created. Call DescribeTable.
do
{
Thread.Sleep(5000); // Wait 5 seconds.
try
{
var res = await Client.DescribeTableAsync(new DescribeTableRequest
{
TableName = tableName
});
Console.WriteLine($"Table name: {res.Table.TableName}, status: {res.Table.TableStatus}");
status = res.Table.TableStatus;
}
catch (ResourceNotFoundException)
{
// DescribeTable is eventually consistent. So you might
// get resource not found. We handle the potential exception.
}
} while (status != "ACTIVE");
}
// snippet-end:[dynamodb.dotnetv3.WaitUntilTableReady]
}
// snippet-end:[dynamodb.dotnetv3.LowLevelTableExample]