-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.java
88 lines (74 loc) · 3.48 KB
/
Util.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
package com.welflex.aws.dynamodb.repository;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.model.*;
import com.welflex.aws.dynamodb.model.Note;
import java.util.Arrays;
import java.util.function.Consumer;
public class Util {
public static <T> void createTable(AmazonDynamoDB dynamoDB, Class<T> clazz, boolean enableStreams) {
DynamoDBMapper dynamoDBMapper = new DynamoDBMapper(dynamoDB);
createTable(clazz, dynamoDBMapper, dynamoDB, enableStreams);
}
private static <T> void createTable(Class clazz, DynamoDBMapper dynamoDBMapper, AmazonDynamoDB dynamoDB, boolean enableStreams) {
System.out.println("Dynamo DB:" + dynamoDB);
CreateTableRequest createTableRequest = dynamoDBMapper.generateCreateTableRequest(clazz);
createTableRequest.withProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
if (createTableRequest.getGlobalSecondaryIndexes() != null) {
for (GlobalSecondaryIndex gsi : createTableRequest.getGlobalSecondaryIndexes()) {
gsi.withProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
gsi.withProjection(new Projection().withProjectionType(ProjectionType.ALL));
}
}
if (createTableRequest.getLocalSecondaryIndexes() != null) {
for (LocalSecondaryIndex lsi : createTableRequest.getLocalSecondaryIndexes()) {
lsi.withProjection(new Projection().withProjectionType(ProjectionType.ALL));
}
}
if (enableStreams) {
StreamSpecification streamSpecification = new StreamSpecification();
streamSpecification.setStreamEnabled(true);
streamSpecification.setStreamViewType(StreamViewType.NEW_IMAGE);
createTableRequest.withStreamSpecification(streamSpecification);
}
if (!tableExists(dynamoDB, createTableRequest)) {
dynamoDB.createTable(createTableRequest);
waitForTableCreation(createTableRequest.getTableName(), dynamoDB);
}
}
private static boolean tableExists(AmazonDynamoDB dynamoDB, CreateTableRequest createTableRequest) {
try {
dynamoDB.describeTable(createTableRequest.getTableName());
return true;
}
catch (ResourceNotFoundException e) {
return false;
}
}
public static void deleteTable(final AmazonDynamoDB dynamoDB, String ...tableNames) {
Arrays.stream(tableNames).forEach(s -> dynamoDB.deleteTable(new DeleteTableRequest().withTableName(s)));
}
private static void waitForTableCreation(String tableName, AmazonDynamoDB dynamoDB) {
while (true) {
try {
Thread.sleep(500);
DescribeTableResult tds = dynamoDB.describeTable(new DescribeTableRequest(tableName));
if (tds == null) {
continue;
}
TableDescription td = tds.getTable();
String tableStatus = td.getTableStatus();
if (tableStatus.equals(TableStatus.ACTIVE.toString())) {
return;
}
}
catch (ResourceNotFoundException e) {
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
}
}