|
33 | 33 |
|
34 | 34 | public final class DynamoTableUtils { |
35 | 35 |
|
36 | | - private DynamoTableUtils() { |
37 | | - } |
38 | | - |
39 | | - public static KeySchemaElement createKeyElementSchema(Map<String,KeyType> keys) { |
40 | | - |
41 | | - KeySchemaElement.Builder keySchemaElementBuilder = KeySchemaElement.builder(); |
42 | | - |
43 | | - keys |
44 | | - .entrySet() |
45 | | - .forEach( |
46 | | - es -> { |
47 | | - keySchemaElementBuilder.attributeName(es.getKey()); |
48 | | - keySchemaElementBuilder.keyType(es.getValue()); |
49 | | - }); |
50 | | - |
51 | | - return keySchemaElementBuilder.build(); |
52 | | - } |
53 | | - |
54 | | - public static AttributeDefinition createAttributeDefinition(Map<String,ScalarAttributeType> attributes) { |
55 | | - |
56 | | - AttributeDefinition.Builder attributeDefinitionBuilder = AttributeDefinition.builder(); |
57 | | - |
58 | | - attributes |
59 | | - .entrySet() |
60 | | - .forEach( |
61 | | - es ->{ |
62 | | - attributeDefinitionBuilder.attributeName(es.getKey()); |
63 | | - attributeDefinitionBuilder.attributeType(es.getValue()); |
64 | | - } |
65 | | - ); |
66 | | - |
67 | | - return attributeDefinitionBuilder.build(); |
68 | | - } |
69 | | - |
70 | | - public static ProvisionedThroughput createProvisionedThroughput(Long readCapacityUnits , Long writeCapacityUnit) { |
71 | | - |
72 | | - ProvisionedThroughput.Builder provisionedThroughputBuilder = ProvisionedThroughput.builder(); |
73 | | - |
74 | | - if(readCapacityUnits != null && readCapacityUnits.longValue() > 0) |
75 | | - provisionedThroughputBuilder.readCapacityUnits(readCapacityUnits); |
76 | | - else |
77 | | - provisionedThroughputBuilder.readCapacityUnits(5l); |
78 | | - |
79 | | - |
80 | | - if(writeCapacityUnit != null && writeCapacityUnit.longValue() > 0) |
81 | | - provisionedThroughputBuilder.writeCapacityUnits(writeCapacityUnit); |
82 | | - else |
83 | | - provisionedThroughputBuilder.writeCapacityUnits(5l); |
84 | | - |
85 | | - return provisionedThroughputBuilder.build(); |
86 | | - } |
87 | | - |
88 | | - public static Map<String, KeyType> createKeyDefinition(){ |
89 | | - return Collections.singletonMap(KEY,KeyType.HASH); |
90 | | - } |
91 | | - |
92 | | - public static Map<String, ScalarAttributeType> createAttributesType(){ |
93 | | - return Collections.singletonMap(KEY, ScalarAttributeType.S); |
94 | | - } |
95 | | - |
96 | | - public static void manageTables(String tableName, DynamoDbClient client, Long readCapacityUnits , Long writeCapacityUnit) { |
97 | | - |
98 | | - boolean more_tables = true; |
99 | | - String last_name = null; |
100 | | - |
101 | | - while (more_tables) { |
102 | | - try { |
103 | | - ListTablesResponse response = null; |
104 | | - if (last_name == null) { |
105 | | - ListTablesRequest request = ListTablesRequest.builder().build(); |
106 | | - response = client.listTables(request); |
107 | | - } else { |
108 | | - ListTablesRequest request = ListTablesRequest.builder().exclusiveStartTableName(last_name).build(); |
109 | | - response = client.listTables(request); |
110 | | - } |
111 | | - |
112 | | - List<String> table_names = response.tableNames(); |
113 | | - |
114 | | - if (table_names.size() == 0) { |
115 | | - createTable(tableName, client,readCapacityUnits ,writeCapacityUnit); |
116 | | - } else { |
117 | | - last_name = response.lastEvaluatedTableName(); |
118 | | - if (last_name == null) { |
119 | | - more_tables = false; |
120 | | - } |
121 | | - } |
122 | | - } catch (DynamoDbException e) { |
123 | | - throw new RuntimeException(e); |
124 | | - } |
125 | | - } |
126 | | - } |
127 | | - |
128 | | - private static void createTable(String tableName, DynamoDbClient client , Long readCapacityUnits , Long writeCapacityUnit) { |
129 | | - |
130 | | - Map<String, KeyType> keyDefinition = createKeyDefinition(); |
131 | | - Map<String, ScalarAttributeType> attributeDefinition = createAttributesType(); |
132 | | - |
133 | | - client.createTable(CreateTableRequest.builder() |
134 | | - .tableName(tableName) |
135 | | - .provisionedThroughput(createProvisionedThroughput(readCapacityUnits, writeCapacityUnit)) |
136 | | - .keySchema(createKeyElementSchema(keyDefinition)) |
137 | | - .attributeDefinitions(createAttributeDefinition(attributeDefinition)) |
138 | | - .build()); |
139 | | - } |
| 36 | + private DynamoTableUtils() { |
| 37 | + } |
| 38 | + |
| 39 | + public static KeySchemaElement createKeyElementSchema(Map<String, KeyType> keys) { |
| 40 | + |
| 41 | + KeySchemaElement.Builder keySchemaElementBuilder = KeySchemaElement.builder(); |
| 42 | + |
| 43 | + keys |
| 44 | + .entrySet() |
| 45 | + .forEach( |
| 46 | + es -> { |
| 47 | + keySchemaElementBuilder.attributeName(es.getKey()); |
| 48 | + keySchemaElementBuilder.keyType(es.getValue()); |
| 49 | + }); |
| 50 | + |
| 51 | + return keySchemaElementBuilder.build(); |
| 52 | + } |
| 53 | + |
| 54 | + public static AttributeDefinition createAttributeDefinition(Map<String, ScalarAttributeType> attributes) { |
| 55 | + |
| 56 | + AttributeDefinition.Builder attributeDefinitionBuilder = AttributeDefinition.builder(); |
| 57 | + |
| 58 | + attributes |
| 59 | + .entrySet() |
| 60 | + .forEach( |
| 61 | + es -> { |
| 62 | + attributeDefinitionBuilder.attributeName(es.getKey()); |
| 63 | + attributeDefinitionBuilder.attributeType(es.getValue()); |
| 64 | + } |
| 65 | + ); |
| 66 | + |
| 67 | + return attributeDefinitionBuilder.build(); |
| 68 | + } |
| 69 | + |
| 70 | + public static ProvisionedThroughput createProvisionedThroughput(Long readCapacityUnits, Long writeCapacityUnit) { |
| 71 | + |
| 72 | + ProvisionedThroughput.Builder provisionedThroughputBuilder = ProvisionedThroughput.builder(); |
| 73 | + |
| 74 | + if (readCapacityUnits != null && readCapacityUnits.longValue() > 0) |
| 75 | + provisionedThroughputBuilder.readCapacityUnits(readCapacityUnits); |
| 76 | + else |
| 77 | + provisionedThroughputBuilder.readCapacityUnits(5l); |
| 78 | + |
| 79 | + |
| 80 | + if (writeCapacityUnit != null && writeCapacityUnit.longValue() > 0) |
| 81 | + provisionedThroughputBuilder.writeCapacityUnits(writeCapacityUnit); |
| 82 | + else |
| 83 | + provisionedThroughputBuilder.writeCapacityUnits(5l); |
| 84 | + |
| 85 | + return provisionedThroughputBuilder.build(); |
| 86 | + } |
| 87 | + |
| 88 | + public static Map<String, KeyType> createKeyDefinition() { |
| 89 | + return Collections.singletonMap(KEY, KeyType.HASH); |
| 90 | + } |
| 91 | + |
| 92 | + public static Map<String, ScalarAttributeType> createAttributesType() { |
| 93 | + return Collections.singletonMap(KEY, ScalarAttributeType.S); |
| 94 | + } |
| 95 | + |
| 96 | + public static void manageTables(String tableName, DynamoDbClient client, Long readCapacityUnits, Long writeCapacityUnit) { |
| 97 | + |
| 98 | + boolean more_tables = true; |
| 99 | + String last_name = null; |
| 100 | + |
| 101 | + while (more_tables) { |
| 102 | + try { |
| 103 | + ListTablesResponse response = null; |
| 104 | + if (last_name == null) { |
| 105 | + ListTablesRequest request = ListTablesRequest.builder().build(); |
| 106 | + response = client.listTables(request); |
| 107 | + } else { |
| 108 | + ListTablesRequest request = ListTablesRequest.builder().exclusiveStartTableName(last_name).build(); |
| 109 | + response = client.listTables(request); |
| 110 | + } |
| 111 | + |
| 112 | + List<String> table_names = response.tableNames(); |
| 113 | + |
| 114 | + if (table_names.size() == 0) { |
| 115 | + createTable(tableName, client, readCapacityUnits, writeCapacityUnit); |
| 116 | + } else { |
| 117 | + last_name = response.lastEvaluatedTableName(); |
| 118 | + if (last_name == null) { |
| 119 | + more_tables = false; |
| 120 | + } |
| 121 | + } |
| 122 | + } catch (DynamoDbException e) { |
| 123 | + throw new RuntimeException(e); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private static void createTable(String tableName, DynamoDbClient client, Long readCapacityUnits, Long writeCapacityUnit) { |
| 129 | + |
| 130 | + Map<String, KeyType> keyDefinition = createKeyDefinition(); |
| 131 | + Map<String, ScalarAttributeType> attributeDefinition = createAttributesType(); |
| 132 | + |
| 133 | + client.createTable(CreateTableRequest.builder() |
| 134 | + .tableName(tableName) |
| 135 | + .provisionedThroughput(createProvisionedThroughput(readCapacityUnits, writeCapacityUnit)) |
| 136 | + .keySchema(createKeyElementSchema(keyDefinition)) |
| 137 | + .attributeDefinitions(createAttributeDefinition(attributeDefinition)) |
| 138 | + .build()); |
| 139 | + } |
140 | 140 | } |
0 commit comments