Search in sources :

Example 1 with ProjectionType

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;
}
Also used : TableDescription(software.amazon.awssdk.services.dynamodb.model.TableDescription) KeysAndAttributes(software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes) LoggerFactory(org.slf4j.LoggerFactory) DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) StringUtils(org.apache.commons.lang3.StringUtils) StreamViewType(software.amazon.awssdk.services.dynamodb.model.StreamViewType) Map(java.util.Map) GlobalSecondaryIndex(software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndex) URI(java.net.URI) WaiterResponse(software.amazon.awssdk.core.waiters.WaiterResponse) Config(com.erudika.para.core.utils.Config) ScalarAttributeType(software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) ParaObject(com.erudika.para.core.ParaObject) ProjectionType(software.amazon.awssdk.services.dynamodb.model.ProjectionType) ScanResponse(software.amazon.awssdk.services.dynamodb.model.ScanResponse) KeyType(software.amazon.awssdk.services.dynamodb.model.KeyType) QueryResponse(software.amazon.awssdk.services.dynamodb.model.QueryResponse) ReturnConsumedCapacity(software.amazon.awssdk.services.dynamodb.model.ReturnConsumedCapacity) Collectors(java.util.stream.Collectors) List(java.util.List) BatchGetItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse) Annotation(java.lang.annotation.Annotation) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) Optional(java.util.Optional) Para(com.erudika.para.core.utils.Para) DefaultAwsRegionProviderChain(software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain) ListTablesResponse(software.amazon.awssdk.services.dynamodb.model.ListTablesResponse) App(com.erudika.para.core.App) QueryRequest(software.amazon.awssdk.services.dynamodb.model.QueryRequest) Replica(software.amazon.awssdk.services.dynamodb.model.Replica) DescribeTableResponse(software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse) ParaObjectUtils(com.erudika.para.core.utils.ParaObjectUtils) HashMap(java.util.HashMap) Pager(com.erudika.para.core.utils.Pager) StaticCredentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider) ServiceNamespace(software.amazon.awssdk.services.applicationautoscaling.model.ServiceNamespace) ProvisionedThroughputExceededException(software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughputExceededException) MetricType(software.amazon.awssdk.services.applicationautoscaling.model.MetricType) ScalableDimension(software.amazon.awssdk.services.applicationautoscaling.model.ScalableDimension) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest) ScanRequest(software.amazon.awssdk.services.dynamodb.model.ScanRequest) ApplicationAutoScalingClient(software.amazon.awssdk.services.applicationautoscaling.ApplicationAutoScalingClient) WriteRequest(software.amazon.awssdk.services.dynamodb.model.WriteRequest) LinkedList(java.util.LinkedList) Region(software.amazon.awssdk.regions.Region) GlobalSecondaryIndexDescription(software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndexDescription) Logger(org.slf4j.Logger) AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) PolicyType(software.amazon.awssdk.services.applicationautoscaling.model.PolicyType) BatchWriteItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchWriteItemResponse) ReplicaUpdate(software.amazon.awssdk.services.dynamodb.model.ReplicaUpdate) KeySchemaElement(software.amazon.awssdk.services.dynamodb.model.KeySchemaElement) BillingMode(software.amazon.awssdk.services.dynamodb.model.BillingMode) Projection(software.amazon.awssdk.services.dynamodb.model.Projection) CreateTableResponse(software.amazon.awssdk.services.dynamodb.model.CreateTableResponse) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) Collections(java.util.Collections) CreateTableResponse(software.amazon.awssdk.services.dynamodb.model.CreateTableResponse) AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) GlobalSecondaryIndex(software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndex) ProvisionedThroughputExceededException(software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughputExceededException)

Example 2 with ProjectionType

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;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Projection(software.amazon.awssdk.services.dynamodb.model.Projection) DynamoDbSecondarySortKey(software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSecondarySortKey) ProjectionType(software.amazon.awssdk.services.dynamodb.model.ProjectionType) DynamoDbSecondaryPartitionKey(software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSecondaryPartitionKey)

Example 3 with ProjectionType

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);
        }
    });
}
Also used : TableMetadata(software.amazon.awssdk.enhanced.dynamodb.TableMetadata) EnhancedLocalSecondaryIndex(software.amazon.awssdk.enhanced.dynamodb.model.EnhancedLocalSecondaryIndex) ArrayList(java.util.ArrayList) EnhancedGlobalSecondaryIndex(software.amazon.awssdk.enhanced.dynamodb.model.EnhancedGlobalSecondaryIndex) ProjectionType(software.amazon.awssdk.services.dynamodb.model.ProjectionType)

Aggregations

ProjectionType (software.amazon.awssdk.services.dynamodb.model.ProjectionType)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Projection (software.amazon.awssdk.services.dynamodb.model.Projection)2 App (com.erudika.para.core.App)1 ParaObject (com.erudika.para.core.ParaObject)1 Config (com.erudika.para.core.utils.Config)1 Pager (com.erudika.para.core.utils.Pager)1 Para (com.erudika.para.core.utils.Para)1 ParaObjectUtils (com.erudika.para.core.utils.ParaObjectUtils)1 Annotation (java.lang.annotation.Annotation)1 URI (java.net.URI)1 Collections (java.util.Collections)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Logger (org.slf4j.Logger)1