-
Notifications
You must be signed in to change notification settings - Fork 5
/
SdkBasicUsage.java
139 lines (119 loc) · 6.11 KB
/
SdkBasicUsage.java
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
package demo.basic;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.testcontainers.shaded.com.google.common.collect.ImmutableMap;
import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import com.amazonaws.services.dynamodbv2.model.DeleteItemRequest;
import com.amazonaws.services.dynamodbv2.model.DeleteItemResult;
import com.amazonaws.services.dynamodbv2.model.GetItemRequest;
import com.amazonaws.services.dynamodbv2.model.GetItemResult;
import com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex;
import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import com.amazonaws.services.dynamodbv2.model.KeyType;
import com.amazonaws.services.dynamodbv2.model.Projection;
import com.amazonaws.services.dynamodbv2.model.ProjectionType;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import com.amazonaws.services.dynamodbv2.model.PutItemRequest;
import com.amazonaws.services.dynamodbv2.model.PutItemResult;
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
import com.amazonaws.services.dynamodbv2.util.TableUtils;
/**
* Create table / Create item / Get item / Update item / Delete item / Drop table.
*/
public class SdkBasicUsage extends AbstractDynamoBasic {
@Test
@Disabled
public void runTests() {
runCreateTable();
runCreateItem();
runGetItem();
runUpdateItem();
runDeleteItem();
}
private void runCreateTable() {
System.out.println("## Create table");
final CreateTableRequest createTableRequest = new CreateTableRequest()
.withAttributeDefinitions(
new AttributeDefinition("id", ScalarAttributeType.S),
new AttributeDefinition("mentionId", ScalarAttributeType.N),
new AttributeDefinition("createdAt", ScalarAttributeType.S)
).withTableName("Comment").withKeySchema(
new KeySchemaElement("id", KeyType.HASH)
).withGlobalSecondaryIndexes(
(new GlobalSecondaryIndex())
.withIndexName("byMentionId")
.withKeySchema(
new KeySchemaElement("mentionId", KeyType.HASH),
new KeySchemaElement("createdAt", KeyType.RANGE))
.withProjection(
(new Projection()).withProjectionType(ProjectionType.ALL))
.withProvisionedThroughput(new ProvisionedThroughput(1L, 1L))
).withProvisionedThroughput(
new ProvisionedThroughput(1L, 1L)
);
final boolean result = TableUtils.createTableIfNotExists(client, createTableRequest);
assertThat(result).isTrue();
}
private void runCreateItem() {
System.out.println("## Create item");
final Map<String, AttributeValue> item = new HashMap<>();
item.put("id", new AttributeValue().withS("uuid"));
item.put("name", new AttributeValue().withS("comment name"));
item.put("mentionId", new AttributeValue().withN("1"));
item.put("content", new AttributeValue().withS("comment content"));
item.put("deleted", new AttributeValue().withBOOL(false));
item.put("createdAt", new AttributeValue().withS("1836-03-07T02:21:30.536Z"));
final PutItemRequest request = new PutItemRequest()
.withTableName("Comment")
.withItem(item);
final PutItemResult result = client.putItem(request);
assertThat(result.getSdkHttpMetadata().getHttpStatusCode()).isEqualTo(200);
}
private void runGetItem() {
System.out.println("## Read item");
final Map<String, AttributeValue> key = new HashMap<>();
key.put("id", new AttributeValue().withS("uuid"));
final GetItemRequest request = new GetItemRequest()
.withTableName("Comment")
.withKey(key);
final GetItemResult result = client.getItem(request);
final Map<String, AttributeValue> item = result.getItem();
assertThat(item.get("id").getS()).isEqualTo("uuid");
System.out.println(item);
}
private void runUpdateItem() {
System.out.println("## Update item");
final String updateContent = "modified comment content";
final Map<String, AttributeValue> item = new HashMap<>();
item.put("id", new AttributeValue().withS("uuid"));
item.put("name", new AttributeValue().withS("comment name"));
item.put("mentionId", new AttributeValue().withN("1"));
item.put("content", new AttributeValue().withS(updateContent));
item.put("deleted", new AttributeValue().withBOOL(false));
item.put("createdAt", new AttributeValue().withS("1836-03-07T02:21:30.536Z"));
final PutItemRequest request = new PutItemRequest()
.withTableName("Comment")
.withItem(item);
final PutItemResult result = client.putItem(request);
assertThat(result.getSdkHttpMetadata().getHttpStatusCode()).isEqualTo(200);
runGetItem();
}
private void runDeleteItem() {
final Map<String, AttributeValue> key = new HashMap<>();
key.put("id", new AttributeValue().withS("uuid"));
final DeleteItemRequest request = new DeleteItemRequest()
.withTableName("Comment")
.withKey(key);
final DeleteItemResult result = client.deleteItem(request);
assertThat(result.getSdkHttpMetadata().getHttpStatusCode()).isEqualTo(200);
final GetItemRequest getItemRequest = new GetItemRequest()
.withTableName("Comment")
.withKey(ImmutableMap.of("id", new AttributeValue().withS("uuid")));
assertThat(client.getItem(getItemRequest).getItem()).isNull();
}
}