Search in sources :

Example 1 with GlobalSecondaryIndex

use of software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndex 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 GlobalSecondaryIndex

use of software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndex in project formkiq-core by formkiq.

the class DynamoDbHelper method createDocumentsTable.

/**
 * Create Documents Table.
 */
public void createDocumentsTable() {
    final Long capacity = Long.valueOf(10);
    KeySchemaElement pk = KeySchemaElement.builder().attributeName("PK").keyType(KeyType.HASH).build();
    KeySchemaElement sk = KeySchemaElement.builder().attributeName("SK").keyType(KeyType.RANGE).build();
    AttributeDefinition a1 = AttributeDefinition.builder().attributeName("PK").attributeType(ScalarAttributeType.S).build();
    AttributeDefinition a2 = AttributeDefinition.builder().attributeName("SK").attributeType(ScalarAttributeType.S).build();
    AttributeDefinition a3 = AttributeDefinition.builder().attributeName("GSI1PK").attributeType(ScalarAttributeType.S).build();
    AttributeDefinition a4 = AttributeDefinition.builder().attributeName("GSI1SK").attributeType(ScalarAttributeType.S).build();
    AttributeDefinition a5 = AttributeDefinition.builder().attributeName("GSI2PK").attributeType(ScalarAttributeType.S).build();
    AttributeDefinition a6 = AttributeDefinition.builder().attributeName("GSI2SK").attributeType(ScalarAttributeType.S).build();
    GlobalSecondaryIndex si1 = GlobalSecondaryIndex.builder().indexName("GSI1").keySchema(KeySchemaElement.builder().attributeName("GSI1PK").keyType(KeyType.HASH).build(), KeySchemaElement.builder().attributeName("GSI1SK").keyType(KeyType.RANGE).build()).projection(Projection.builder().projectionType(ProjectionType.INCLUDE).nonKeyAttributes("inserteddate", "documentId", "tagKey", "tagValue").build()).provisionedThroughput(ProvisionedThroughput.builder().writeCapacityUnits(capacity).readCapacityUnits(capacity).build()).build();
    GlobalSecondaryIndex si2 = GlobalSecondaryIndex.builder().indexName("GSI2").keySchema(KeySchemaElement.builder().attributeName("GSI2PK").keyType(KeyType.HASH).build(), KeySchemaElement.builder().attributeName("GSI2SK").keyType(KeyType.RANGE).build()).projection(Projection.builder().projectionType(ProjectionType.INCLUDE).nonKeyAttributes("inserteddate", "documentId", "tagKey", "tagValue").build()).provisionedThroughput(ProvisionedThroughput.builder().writeCapacityUnits(capacity).readCapacityUnits(capacity).build()).build();
    CreateTableRequest table = CreateTableRequest.builder().tableName(this.documentTable).keySchema(pk, sk).attributeDefinitions(a1, a2, a3, a4, a5, a6).globalSecondaryIndexes(si1, si2).provisionedThroughput(ProvisionedThroughput.builder().writeCapacityUnits(capacity).readCapacityUnits(capacity).build()).build();
    this.db.createTable(table);
}
Also used : AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) GlobalSecondaryIndex(software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndex) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest) KeySchemaElement(software.amazon.awssdk.services.dynamodb.model.KeySchemaElement)

Aggregations

AttributeDefinition (software.amazon.awssdk.services.dynamodb.model.AttributeDefinition)2 CreateTableRequest (software.amazon.awssdk.services.dynamodb.model.CreateTableRequest)2 GlobalSecondaryIndex (software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndex)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 HashMap (java.util.HashMap)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