use of software.amazon.awssdk.services.dynamodb.model.ProjectionType in project para by Erudika.
the class AWSDynamoUtils method createSharedTable.
/**
* Creates a table in AWS DynamoDB which will be shared between apps.
* @param readCapacity read capacity
* @param writeCapacity write capacity
* @return true if created
*/
public static boolean createSharedTable(long readCapacity, long writeCapacity) {
if (StringUtils.isBlank(SHARED_TABLE) || StringUtils.containsWhitespace(SHARED_TABLE) || existsTable(SHARED_TABLE)) {
return false;
}
String table = getTableNameForAppid(SHARED_TABLE);
try {
GlobalSecondaryIndex secIndex = GlobalSecondaryIndex.builder().indexName(getSharedIndexName()).provisionedThroughput(b -> b.readCapacityUnits(1L).writeCapacityUnits(1L)).projection(Projection.builder().projectionType(ProjectionType.ALL).build()).keySchema(KeySchemaElement.builder().attributeName(Config._APPID).keyType(KeyType.HASH).build(), KeySchemaElement.builder().attributeName(Config._ID).keyType(KeyType.RANGE).build()).build();
AttributeDefinition[] attributes = new AttributeDefinition[] { AttributeDefinition.builder().attributeName(Config._KEY).attributeType(ScalarAttributeType.S).build(), AttributeDefinition.builder().attributeName(Config._APPID).attributeType(ScalarAttributeType.S).build(), AttributeDefinition.builder().attributeName(Config._ID).attributeType(ScalarAttributeType.S).build() };
CreateTableResponse tbl = getClient().createTable(b -> b.tableName(table).keySchema(KeySchemaElement.builder().attributeName(Config._KEY).keyType(KeyType.HASH).build()).sseSpecification(b2 -> b2.enabled(ENCRYPTION_AT_REST_ENABLED)).attributeDefinitions(attributes).globalSecondaryIndexes(secIndex).provisionedThroughput(b6 -> b6.readCapacityUnits(readCapacity).writeCapacityUnits(writeCapacity)));
logger.info("Waiting for DynamoDB table to become ACTIVE...");
waitForActive(table, AWS_REGION);
logger.info("Created shared table '{}', status {}.", table, tbl.tableDescription().tableStatus());
if (BACKUPS_ENABLED) {
logger.info("Enabling backups for shared table '{}'...", table);
getClient().updateContinuousBackups((t) -> t.tableName(table).pointInTimeRecoverySpecification((p) -> p.pointInTimeRecoveryEnabled(true)));
}
} catch (Exception e) {
logger.error(null, e);
return false;
}
return true;
}
use of software.amazon.awssdk.services.dynamodb.model.ProjectionType in project micronaut-aws-sdk by agorapulse.
the class DefaultDynamoDbService method getProjectionTypes.
private Map<String, ProjectionType> getProjectionTypes() {
Map<String, ProjectionType> types = new HashMap<>();
BeanIntrospection<T> introspection = EntityIntrospection.getBeanIntrospection(table);
introspection.getBeanProperties().forEach(p -> {
AnnotationValue<com.agorapulse.micronaut.amazon.awssdk.dynamodb.annotation.Projection> projectionAnnotation = p.getAnnotation(com.agorapulse.micronaut.amazon.awssdk.dynamodb.annotation.Projection.class);
if (projectionAnnotation == null) {
return;
}
AnnotationValue<DynamoDbSecondarySortKey> secondaryIndex = p.getAnnotation(DynamoDbSecondarySortKey.class);
Collection<String> indexNames = new ArrayList<>();
if (secondaryIndex != null) {
indexNames.addAll(Arrays.asList(secondaryIndex.stringValues("indexNames")));
}
AnnotationValue<DynamoDbSecondaryPartitionKey> secondaryGlobalIndex = p.getAnnotation(DynamoDbSecondaryPartitionKey.class);
if (secondaryGlobalIndex != null) {
indexNames.addAll(Arrays.asList(secondaryGlobalIndex.stringValues("indexNames")));
}
if (indexNames.isEmpty()) {
return;
}
ProjectionType type = projectionAnnotation.enumValue(ProjectionType.class).orElse(ProjectionType.KEYS_ONLY);
for (String name : indexNames) {
types.put(name, type);
}
});
return types;
}
use of software.amazon.awssdk.services.dynamodb.model.ProjectionType in project micronaut-aws-sdk by agorapulse.
the class DefaultDynamoDbService method createTable.
@Override
public void createTable() {
Map<String, ProjectionType> types = getProjectionTypes();
TableMetadata tableMetadata = table.tableSchema().tableMetadata();
table.createTable(b -> {
List<EnhancedLocalSecondaryIndex> localSecondaryIndices = new ArrayList<>();
List<EnhancedGlobalSecondaryIndex> globalSecondaryIndices = new ArrayList<>();
tableMetadata.indices().forEach(i -> {
if (TableMetadata.primaryIndexName().equals(i.name())) {
return;
}
ProjectionType type = types.getOrDefault(i.name(), ProjectionType.KEYS_ONLY);
if (tableMetadata.primaryPartitionKey().equals(tableMetadata.indexPartitionKey(i.name()))) {
localSecondaryIndices.add(EnhancedLocalSecondaryIndex.create(i.name(), Projection.builder().projectionType(type).build()));
} else {
globalSecondaryIndices.add(EnhancedGlobalSecondaryIndex.builder().indexName(i.name()).projection(Projection.builder().projectionType(type).build()).build());
}
});
if (!localSecondaryIndices.isEmpty()) {
b.localSecondaryIndices(localSecondaryIndices);
}
if (!globalSecondaryIndices.isEmpty()) {
b.globalSecondaryIndices(globalSecondaryIndices);
}
});
}
Aggregations