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

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

Introduction

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

Prototype


public void setLocalSecondaryIndexes(java.util.Collection<LocalSecondaryIndex> localSecondaryIndexes) 

Source Link

Document

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

Usage

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

License:Apache License

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

    final String rangeKeyName = "ReplyDateTime";
    final ScalarAttributeType rangeKeyType = ScalarAttributeType.S;

    ////////////////
    final CreateTableRequest request = basicTable(tableName, readCapacityUnits, writeCapacityUnits, hashKeyName,
            hashKeyType);/*from  w  ww . j a  v a  2  s .  c o m*/
    ///////////////////////
    //defining range key 
    request.getKeySchema()
            .add(new KeySchemaElement().withAttributeName(rangeKeyName).withKeyType(KeyType.RANGE));
    //defining range key 
    request.getAttributeDefinitions()
            .add(new AttributeDefinition().withAttributeName(rangeKeyName).withAttributeType(rangeKeyType));
    //////////////////////

    //follows defininig of a local secondary index
    request.getAttributeDefinitions().add(
            new AttributeDefinition().withAttributeName("PostedBy").withAttributeType(ScalarAttributeType.S));

    final List<KeySchemaElement> iks = new ArrayList<>();
    iks.add(new KeySchemaElement().withAttributeName(hashKeyName).withKeyType(KeyType.HASH));
    iks.add(new KeySchemaElement().withAttributeName("PostedBy").withKeyType(KeyType.RANGE));

    final LocalSecondaryIndex localSecondaryIndex = new LocalSecondaryIndex().withIndexName("PostedByIndex")
            .withKeySchema(iks).withProjection(new Projection().withProjectionType(ProjectionType.KEYS_ONLY));

    final List<LocalSecondaryIndex> localSecondaryIndexes = new ArrayList<>();
    localSecondaryIndexes.add(localSecondaryIndex);

    request.setLocalSecondaryIndexes(localSecondaryIndexes);

    return request;
}

From source file:DynamoDB.sample.CreateTablesLoadData.java

License:Open Source License

private static void createTable(String tableName, long readCapacityUnits, long writeCapacityUnits,
        String partitionKeyName, String partitionKeyType, String sortKeyName, String sortKeyType) {

    try {//  w  w w. ja v  a2 s  .  c o  m

        ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
        keySchema.add(new KeySchemaElement().withAttributeName(partitionKeyName).withKeyType(KeyType.HASH)); //Partition key

        ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
        attributeDefinitions.add(new AttributeDefinition().withAttributeName(partitionKeyName)
                .withAttributeType(partitionKeyType));

        if (sortKeyName != null) {
            keySchema.add(new KeySchemaElement().withAttributeName(sortKeyName).withKeyType(KeyType.RANGE)); //Sort key
            attributeDefinitions.add(
                    new AttributeDefinition().withAttributeName(sortKeyName).withAttributeType(sortKeyType));
        }

        CreateTableRequest request = new CreateTableRequest().withTableName(tableName).withKeySchema(keySchema)
                .withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(readCapacityUnits)
                        .withWriteCapacityUnits(writeCapacityUnits));

        // If this is the Reply table, define a local secondary index
        if (replyTableName.equals(tableName)) {

            attributeDefinitions
                    .add(new AttributeDefinition().withAttributeName("PostedBy").withAttributeType("S"));

            ArrayList<LocalSecondaryIndex> localSecondaryIndexes = new ArrayList<LocalSecondaryIndex>();
            localSecondaryIndexes.add(new LocalSecondaryIndex().withIndexName("PostedBy-Index")
                    .withKeySchema(
                            new KeySchemaElement().withAttributeName(partitionKeyName)
                                    .withKeyType(KeyType.HASH), //Partition key
                            new KeySchemaElement().withAttributeName("PostedBy").withKeyType(KeyType.RANGE)) //Sort key
                    .withProjection(new Projection().withProjectionType(ProjectionType.KEYS_ONLY)));

            request.setLocalSecondaryIndexes(localSecondaryIndexes);
        }

        request.setAttributeDefinitions(attributeDefinitions);

        System.out.println("Issuing CreateTable request for " + tableName);
        Table table = dynamoDB.createTable(request);
        System.out.println("Waiting for " + tableName + " to be created...this may take a while...");
        table.waitForActive();

    } catch (Exception e) {
        System.err.println("CreateTable request failed for " + tableName);
        System.err.println(e.getMessage());
    }
}

From source file:io.venable.amazonaws.dynamo.table.builder.TableBuilder.java

License:Apache License

private CreateTableRequest buildCreateTableRequest() {
    Collection<KeySchemaElement> keySchemaElementCollection = new ArrayList<>();
    Collection<AttributeDefinition> attributeDefinitionCollection = new ArrayList<>();

    primaryKeyBuilder.buildPrimaryKey(keySchemaElementCollection, attributeDefinitionCollection);

    KeySchemaElement primaryHashKeySchemaElement = getHashKeySchemaElement(keySchemaElementCollection);

    Collection<GlobalSecondaryIndex> globalSecondaryIndexCollection = new ArrayList<>();
    for (GlobalSecondaryIndexBuilderImpl globalSecondaryIndexBuilder : globalSecondaryIndexBuilderCollection) {
        globalSecondaryIndexBuilder.buildSecondaryIndexes(globalSecondaryIndexCollection,
                attributeDefinitionCollection);
    }/*from   w ww  .ja v a  2  s  .c o m*/

    Collection<LocalSecondaryIndex> localSecondaryIndexCollection = new ArrayList<>();
    for (LocalSecondaryIndexBuilderImpl localSecondaryIndexBuilder : localSecondaryIndexBuilderCollection) {
        localSecondaryIndexBuilder.buildSecondaryIndexes(primaryHashKeySchemaElement,
                localSecondaryIndexCollection, attributeDefinitionCollection);
    }

    CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
            .withKeySchema(keySchemaElementCollection).withAttributeDefinitions(attributeDefinitionCollection);

    if (globalSecondaryIndexCollection.size() > 0)
        createTableRequest.setGlobalSecondaryIndexes(globalSecondaryIndexCollection);

    if (localSecondaryIndexCollection.size() > 0)
        createTableRequest.setLocalSecondaryIndexes(localSecondaryIndexCollection);

    primaryKeyBuilder.setProvisionedThroughput(new PrimaryKeyProvisionedThroughputSetter(createTableRequest));

    return createTableRequest;
}

From source file:org.diksha.common.dyutils.SchedulerDynamoTable.java

License:Apache License

public void createDynamoTable(DynamoDB dynamoDB) {

    try {//from w  ww . ja va  2 s.  c  o m
        System.out.println(toString());

        ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
        keySchema.add(new KeySchemaElement().withAttributeName(hashKeyName).withKeyType(KeyType.HASH));

        ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
        attributeDefinitions
                .add(new AttributeDefinition().withAttributeName(hashKeyName).withAttributeType(hashKeyType));
        if (rangeKeyType != null) {
            if (!rangeKeyType.isEmpty()) {
                attributeDefinitions.add(new AttributeDefinition().withAttributeName(rangeKeyName)
                        .withAttributeType(rangeKeyType));
            }
        }

        if (additionalAttributes.size() > 0) {
            for (int cnt = 0; cnt < additionalAttributes.size(); cnt++) {
                String[] args = (additionalAttributes.get(cnt).toString()).split(":");
                if (args.length == 2) {
                    attributeDefinitions.add(
                            new AttributeDefinition().withAttributeName(args[0]).withAttributeType(args[1]));

                }

            }
        }

        CreateTableRequest request = new CreateTableRequest().withTableName(tableName).withKeySchema(keySchema)
                .withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(readCapacityUnits)
                        .withWriteCapacityUnits(writeCapacityUnits));

        ArrayList<LocalSecondaryIndex> localSecondaryIndexes = new ArrayList<LocalSecondaryIndex>();
        ArrayList<GlobalSecondaryIndex> globalSecondaryIndexes = new ArrayList<GlobalSecondaryIndex>();

        if (rangeKeyType != null) {
            if (!rangeKeyType.isEmpty()) {
                if (rangeKeyTypeGlobalOrLocal.equals("L")) {
                    localSecondaryIndexes
                            .add(new LocalSecondaryIndex().withIndexName(rangeKeyName + "-Index")
                                    .withKeySchema(
                                            new KeySchemaElement().withAttributeName(hashKeyName)
                                                    .withKeyType(KeyType.HASH),
                                            new KeySchemaElement().withAttributeName(rangeKeyName)
                                                    .withKeyType(KeyType.RANGE))
                                    .withProjection(
                                            new Projection().withProjectionType(ProjectionType.KEYS_ONLY)));
                } else {
                    globalSecondaryIndexes
                            .add(new GlobalSecondaryIndex().withIndexName(rangeKeyName + "-Index")
                                    .withKeySchema(
                                            new KeySchemaElement().withAttributeName(hashKeyName)
                                                    .withKeyType(KeyType.HASH),
                                            new KeySchemaElement().withAttributeName(rangeKeyName)
                                                    .withKeyType(KeyType.RANGE))
                                    .withProvisionedThroughput(new ProvisionedThroughput()
                                            .withReadCapacityUnits(indexReadCapacityUnits)
                                            .withWriteCapacityUnits(indexWriteCapacityUnits))
                                    .withProjection(
                                            new Projection().withProjectionType(ProjectionType.KEYS_ONLY)));
                }
            }
        }

        if (indices.size() > 0) {

            for (int cnt = 0; cnt < indices.size(); cnt++) {
                SchedulerDynamoTableIndex schedulerDynamoTableIndex = indices.get(cnt);

                if (schedulerDynamoTableIndex.isGlobal()) {
                    //         System.out.println("IS GLOBAL");
                    globalSecondaryIndexes.add(schedulerDynamoTableIndex.getGlobalSecondaryIndex());
                    //         System.out.println("size = " + globalSecondaryIndexes.size());
                }

            }

        }

        if (localSecondaryIndexes.size() > 0)
            request.setLocalSecondaryIndexes(localSecondaryIndexes);
        if (globalSecondaryIndexes.size() > 0)
            request.setGlobalSecondaryIndexes(globalSecondaryIndexes);

        request.setAttributeDefinitions(attributeDefinitions);
        System.out.println("Issuing CreateTable request for " + tableName);
        Table table = dynamoDB.createTable(request);
        System.out.println("Waiting for " + tableName + " to be created...this may take a while...");
        table.waitForActive();

    } catch (Exception e) {
        System.err.println("CreateTable request failed for " + tableName);
        System.err.println(e.getMessage());
    }

}