Example usage for com.amazonaws.services.dynamodbv2.model CreateTableRequest withGlobalSecondaryIndexes

List of usage examples for com.amazonaws.services.dynamodbv2.model CreateTableRequest withGlobalSecondaryIndexes

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.model CreateTableRequest withGlobalSecondaryIndexes.

Prototype


public CreateTableRequest withGlobalSecondaryIndexes(
        java.util.Collection<GlobalSecondaryIndex> globalSecondaryIndexes) 

Source Link

Document

One or more global secondary indexes (the maximum is 20) to be created on the table.

Usage

From source file:com.clicktravel.infrastructure.persistence.aws.dynamodb.manager.DynamoDbTemplateInfrastructureManager.java

License:Apache License

public void init() {
    for (final AbstractDynamoDbTemplate dynamoDbTemplate : dynamoDbTemplates) {
        final Collection<String> tablesPendingCreation = new ArrayList<>();
        final DatabaseSchemaHolder databaseSchemaHolder = dynamoDbTemplate.databaseSchemaHolder();
        for (final ItemConfiguration itemConfiguration : databaseSchemaHolder.itemConfigurations()) {
            if (VariantItemConfiguration.class.isAssignableFrom(itemConfiguration.getClass())) {
                continue;
            }//w  w  w. jav a  2s.  co m
            final String tableName = databaseSchemaHolder.schemaName() + "." + itemConfiguration.tableName();
            if (!tablesPendingCreation.contains(tableName) && !isTableCreated(tableName)) {
                final List<AttributeDefinition> attributeDefinitions = new ArrayList<>();
                final PrimaryKeyDefinition primaryKeyDefinition = itemConfiguration.primaryKeyDefinition();
                final String hashKey = primaryKeyDefinition.propertyName();
                final ScalarAttributeType hashKeyType = getAttributeType(primaryKeyDefinition.propertyType());
                attributeDefinitions.add(
                        new AttributeDefinition().withAttributeName(hashKey).withAttributeType(hashKeyType));
                final List<KeySchemaElement> keySchema = new ArrayList<>();
                keySchema.add(new KeySchemaElement().withAttributeName(hashKey).withKeyType(KeyType.HASH));
                if (CompoundPrimaryKeyDefinition.class.isAssignableFrom(primaryKeyDefinition.getClass())) {
                    final CompoundPrimaryKeyDefinition compoundPrimaryKeyDefinition = (CompoundPrimaryKeyDefinition) primaryKeyDefinition;
                    final String rangeKey = compoundPrimaryKeyDefinition.supportingPropertyName();
                    final ScalarAttributeType rangeKeyType = getAttributeType(
                            compoundPrimaryKeyDefinition.supportingPropertyType());
                    attributeDefinitions.add(new AttributeDefinition().withAttributeName(rangeKey)
                            .withAttributeType(rangeKeyType));
                    keySchema
                            .add(new KeySchemaElement().withAttributeName(rangeKey).withKeyType(KeyType.RANGE));
                }

                final Collection<GlobalSecondaryIndex> globalSecondaryIndexes = new ArrayList<>();
                for (final IndexDefinition indexDefinition : itemConfiguration.indexDefinitions()) {
                    final ScalarAttributeType attributeType = getAttributeType(
                            primaryKeyDefinition.propertyType());

                    // if there are any indexes, we need to add attributes for them
                    attributeDefinitions
                            .add(new AttributeDefinition().withAttributeName(indexDefinition.propertyName())
                                    .withAttributeType(attributeType));

                    final ProvisionedThroughput indexProvisionedThroughput = new ProvisionedThroughput()
                            .withReadCapacityUnits((long) readThroughput)
                            .withWriteCapacityUnits((long) writeThroughput);
                    final GlobalSecondaryIndex globalSecondaryIndex = new GlobalSecondaryIndex()
                            .withIndexName(indexDefinition.propertyName() + "_idx")
                            .withProvisionedThroughput(indexProvisionedThroughput)
                            .withProjection(new Projection().withProjectionType("ALL"));

                    final ArrayList<KeySchemaElement> indexKeySchema = new ArrayList<KeySchemaElement>();

                    indexKeySchema.add(new KeySchemaElement().withAttributeName(indexDefinition.propertyName())
                            .withKeyType(KeyType.HASH));

                    globalSecondaryIndex.setKeySchema(indexKeySchema);
                    globalSecondaryIndexes.add(globalSecondaryIndex);
                }

                final ProvisionedThroughput tableProvisionedThroughput = new ProvisionedThroughput()
                        .withReadCapacityUnits((long) readThroughput)
                        .withWriteCapacityUnits((long) writeThroughput);

                CreateTableRequest request = new CreateTableRequest().withTableName(tableName)
                        .withAttributeDefinitions(attributeDefinitions).withKeySchema(keySchema)
                        .withProvisionedThroughput(tableProvisionedThroughput);

                if (!globalSecondaryIndexes.isEmpty()) {
                    request = request.withGlobalSecondaryIndexes(globalSecondaryIndexes);
                }

                logger.debug("Creating table " + tableName);
                createTable(request, globalSecondaryIndexes.isEmpty());
                tablesPendingCreation.add(tableName);
            }

            // Create tables for unique constraints
            if (!itemConfiguration.uniqueConstraints().isEmpty()) {
                final String uniqueConstraintTableName = databaseSchemaHolder.schemaName() + "-indexes."
                        + itemConfiguration.tableName();
                if (!isTableCreated(uniqueConstraintTableName)) {
                    final List<KeySchemaElement> keySchema = new ArrayList<>();
                    keySchema.add(new KeySchemaElement("property", KeyType.HASH));
                    keySchema.add(new KeySchemaElement("value", KeyType.RANGE));
                    final List<AttributeDefinition> attributeDefinitions = new ArrayList<>();
                    attributeDefinitions.add(new AttributeDefinition("property", ScalarAttributeType.S));
                    attributeDefinitions.add(new AttributeDefinition("value", ScalarAttributeType.S));
                    final ProvisionedThroughput tableProvisionedThroughput = new ProvisionedThroughput()
                            .withReadCapacityUnits((long) readThroughput)
                            .withWriteCapacityUnits((long) writeThroughput);
                    final CreateTableRequest createTableRequest = new CreateTableRequest()
                            .withTableName(uniqueConstraintTableName)
                            .withAttributeDefinitions(attributeDefinitions).withKeySchema(keySchema)
                            .withProvisionedThroughput(tableProvisionedThroughput);
                    createTable(createTableRequest, true);
                    tablesPendingCreation.add(uniqueConstraintTableName);
                }
            }
        }

        // Create table for sequences
        if (!databaseSchemaHolder.sequenceConfigurations().isEmpty()) {
            final String sequenceTableName = databaseSchemaHolder.schemaName() + "-sequences";
            if (!isTableCreated(sequenceTableName)) {
                final ProvisionedThroughput sequenceProvisionedThroughput = new ProvisionedThroughput()
                        .withReadCapacityUnits((long) readThroughput)
                        .withWriteCapacityUnits((long) writeThroughput);
                final List<KeySchemaElement> keySchema = new ArrayList<>();
                keySchema.add(new KeySchemaElement().withAttributeName("name").withKeyType(KeyType.HASH));
                final List<AttributeDefinition> attributeDefinitions = new ArrayList<>();
                attributeDefinitions
                        .add(new AttributeDefinition().withAttributeName("name").withAttributeType("S"));
                final CreateTableRequest request = new CreateTableRequest().withTableName(sequenceTableName)
                        .withAttributeDefinitions(attributeDefinitions).withKeySchema(keySchema)
                        .withProvisionedThroughput(sequenceProvisionedThroughput);
                logger.debug("Creating sequence table " + sequenceTableName);
                amazonDynamoDbClient.createTable(request);
                tablesPendingCreation.add(sequenceTableName);
            }
        }

        logger.debug("Waiting for table creation: " + tablesPendingCreation);
        final Collection<String> tableNames = new ArrayList<>(tablesPendingCreation);
        final long startTime = System.currentTimeMillis();
        while (System.currentTimeMillis() - startTime < TABLE_CREATION_TIMEOUT_MS) {
            for (final String tableName : tableNames) {
                if (isTableCreated(tableName)) {
                    logger.debug("Table " + tableName + " successfully created");
                    tablesPendingCreation.remove(tableName);
                }
                try {
                    Thread.sleep(1000);
                } catch (final InterruptedException e) {
                    throw new IllegalStateException(e);
                }
            }
            if (tablesPendingCreation.size() == 0) {
                break;
            }
            tableNames.clear();
            tableNames.addAll(tablesPendingCreation);
        }

        if (tablesPendingCreation.size() != 0) {
            throw new IllegalStateException(
                    "Unable to create tables for DynamoDb template: " + tablesPendingCreation);
        }

        // Seed the sequences with their starting values
        if (!databaseSchemaHolder.sequenceConfigurations().isEmpty()) {
            final ScanRequest scanRequest = new ScanRequest(databaseSchemaHolder.schemaName() + "-sequences");
            final ScanResult scanResult = amazonDynamoDbClient.scan(scanRequest);
            Map<String, AttributeValue> lastEvaluatedKey = null;
            final Map<String, Map<String, AttributeValue>> sequenceItems = new HashMap<>();
            do {
                for (final Map<String, AttributeValue> item : scanResult.getItems()) {
                    sequenceItems.put(item.get("name").getS(), item);
                }
                lastEvaluatedKey = scanResult.getLastEvaluatedKey();
            } while (lastEvaluatedKey != null);

            for (final SequenceConfiguration sequenceConfiguration : databaseSchemaHolder
                    .sequenceConfigurations()) {
                final Map<String, AttributeValue> sequenceItem = sequenceItems
                        .get(sequenceConfiguration.sequenceName());
                if (sequenceItem == null) {
                    final Map<String, ExpectedAttributeValue> expectedResults = new HashMap<>();
                    expectedResults.put("name", new ExpectedAttributeValue(false));
                    final Map<String, AttributeValue> attributeMap = new HashMap<>();
                    attributeMap.put("name", new AttributeValue().withS(sequenceConfiguration.sequenceName()));
                    attributeMap.put("currentValue", new AttributeValue()
                            .withN(String.valueOf(sequenceConfiguration.startingValue() - 1)));
                    final String tableName = databaseSchemaHolder.schemaName() + "-sequences";
                    final PutItemRequest itemRequest = new PutItemRequest().withTableName(tableName)
                            .withItem(attributeMap).withExpected(expectedResults);
                    amazonDynamoDbClient.putItem(itemRequest);
                }
            }
        }

        dynamoDbTemplate.initialize(amazonDynamoDbClient);
    }
}

From source file:com.makariev.dynamodb.data.tables.creators.UserPreferenceCreator.java

License:Apache License

@Override
public CreateTableRequest getCreateTableRequest() {
    final String tableName = UserPreference.TABLE_NAME;
    final String hashKeyName = "userNo";
    final ScalarAttributeType hashKeyType = ScalarAttributeType.N;
    final Long readCapacityUnits = 10L;
    final Long writeCapacityUnits = 10L;

    ////////////////
    final CreateTableRequest request = basicTable(tableName, readCapacityUnits, writeCapacityUnits, hashKeyName,
            hashKeyType);//from w  ww . jav  a  2 s. c o  m

    //define GlobalSecondaryIndex
    request.getAttributeDefinitions().add(
            new AttributeDefinition().withAttributeName("firstName").withAttributeType(ScalarAttributeType.S));

    request.getAttributeDefinitions().add(
            new AttributeDefinition().withAttributeName("lastName").withAttributeType(ScalarAttributeType.S));

    // NameIndex
    final GlobalSecondaryIndex nameIndex = new GlobalSecondaryIndex().withIndexName(UserPreference.NAME_INDEX)
            .withProvisionedThroughput(
                    new ProvisionedThroughput().withReadCapacityUnits(10L).withWriteCapacityUnits(10L))
            .withProjection(new Projection().withProjectionType("ALL"));

    final List<KeySchemaElement> indexKeySchema = new ArrayList<>();

    indexKeySchema.add(new KeySchemaElement().withAttributeName("firstName").withKeyType(KeyType.HASH));
    indexKeySchema.add(new KeySchemaElement().withAttributeName("lastName").withKeyType(KeyType.RANGE));

    nameIndex.setKeySchema(indexKeySchema);

    request.withGlobalSecondaryIndexes(nameIndex);
    return request;
}