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

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

Introduction

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

Prototype


public void setAttributeDefinitions(java.util.Collection<AttributeDefinition> attributeDefinitions) 

Source Link

Document

An array of attributes that describe the key schema for the table and indexes.

Usage

From source file:DynamoDBUtils.java

License:Open Source License

/**
 * Creates the Amazon DynamoDB table if it does not already exist and have the correct schema. Then it
 * waits for the table to become active.
 *
 * @param client/*from w  w  w  . j ava2 s.com*/
 *        The {@link AmazonDynamoDBClient} with Amazon DynamoDB read and write privileges
 * @param tableName
 *        The Amazon DynamoDB table to create
 * @param key
 *        The Amazon DynamoDB table hashkey
 * @param readCapacityUnits
 *        Number of read capacity units for the Amazon DynamoDB table
 * @param writeCapacityUnits
 *        Number of write capacity units for the Amazon DynamoDB table
 * @throws IllegalStateException
 *         Table already exists and schema does not match
 * @throws IllegalStateException
 *         Table is already getting created
 */
public static void createTable(AmazonDynamoDBClient client, String tableName, String key,
        long readCapacityUnits, long writeCapacityUnits) {
    if (tableExists(client, tableName)) {
        if (tableHasCorrectSchema(client, tableName, key)) {
            waitForActive(client, tableName);
            return;
        } else {
            throw new IllegalStateException("Table already exists and schema does not match");
        }

    }
    CreateTableRequest createTableRequest = new CreateTableRequest();
    createTableRequest.setTableName(tableName);
    createTableRequest.setKeySchema(Arrays.asList(new KeySchemaElement(key, KeyType.HASH)));
    createTableRequest
            .setProvisionedThroughput(new ProvisionedThroughput(readCapacityUnits, writeCapacityUnits));
    createTableRequest
            .setAttributeDefinitions(Arrays.asList(new AttributeDefinition(key, ScalarAttributeType.S)));
    try {
        client.createTable(createTableRequest);
    } catch (ResourceInUseException e) {
        throw new IllegalStateException("The table may already be getting created.", e);
    }
    LOG.info("Table " + tableName + " created");
    waitForActive(client, tableName);
}

From source file:awslabs.lab22.SolutionCode.java

License:Open Source License

@Override
public void buildTable(AmazonDynamoDBClient ddbClient, String tableName) {
    System.out.println("Creating table.");
    CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName);
    createTableRequest.setAttributeDefinitions(new ArrayList<AttributeDefinition>());
    // Define attributes
    createTableRequest.getAttributeDefinitions()
            .add(new AttributeDefinition().withAttributeName("Company").withAttributeType("S"));
    createTableRequest.getAttributeDefinitions()
            .add(new AttributeDefinition().withAttributeName("Email").withAttributeType("S"));
    // Define key schema
    createTableRequest.setKeySchema(new ArrayList<KeySchemaElement>());
    createTableRequest.getKeySchema()/*  w ww .j  a v  a 2s.  c  om*/
            .add(new KeySchemaElement().withAttributeName("Company").withKeyType("HASH"));
    createTableRequest.getKeySchema()
            .add(new KeySchemaElement().withAttributeName("Email").withKeyType("RANGE"));
    // Define provisioned throughput
    createTableRequest.setProvisionedThroughput(
            new ProvisionedThroughput().withReadCapacityUnits(10L).withWriteCapacityUnits(5L));

    // Submit create request
    ddbClient.createTable(createTableRequest);
    // Pause until the table is active
    waitForStatus(ddbClient, tableName, "ACTIVE");
    System.out.println("Table created and active.");
}

From source file:awslabs.lab51.SolutionCode.java

License:Open Source License

@Override
public void buildTable(AmazonDynamoDBClient ddbClient, String tableName) {
    CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName);
    createTableRequest.setAttributeDefinitions(new ArrayList<AttributeDefinition>());
    // Define attributes
    createTableRequest.getAttributeDefinitions()
            .add(new AttributeDefinition().withAttributeName("Key").withAttributeType("S"));
    createTableRequest.getAttributeDefinitions()
            .add(new AttributeDefinition().withAttributeName("Bucket").withAttributeType("S"));
    // Define key schema
    createTableRequest.setKeySchema(new ArrayList<KeySchemaElement>());
    createTableRequest.getKeySchema().add(new KeySchemaElement().withAttributeName("Key").withKeyType("HASH"));
    createTableRequest.getKeySchema()//from www .jav  a2  s . c o  m
            .add(new KeySchemaElement().withAttributeName("Bucket").withKeyType("RANGE"));
    // Define provisioned throughput
    createTableRequest.setProvisionedThroughput(
            new ProvisionedThroughput().withReadCapacityUnits(5L).withWriteCapacityUnits(5L));

    // Submit create request
    ddbClient.createTable(createTableRequest);
    // Pause until the table is active
    waitForStatus(ddbClient, tableName, "ACTIVE");
    labController.logMessageToPage("Table created and active.");
}

From source file:com.amazon.services.awsrum.utils.DynamoDBUtils.java

License:Open Source License

public static void createTable(AmazonDynamoDBClient client, String tableName, String key, String rangeKey,
        long readCapacityUnits, long writeCapacityUnits) {
    if (tableExists(client, tableName)) {
        if (tableHasCorrectSchema(client, tableName, key, rangeKey)) {
            waitForActive(client, tableName);
            return;
        } else {/*from   w w  w.j a  va  2  s .c  o  m*/
            LOG.error("Table already exists and schema does not match");
        }

    }
    CreateTableRequest createTableRequest = new CreateTableRequest();
    createTableRequest.setTableName(tableName);
    createTableRequest.setKeySchema(Arrays.asList(new KeySchemaElement(key, KeyType.HASH),
            new KeySchemaElement(rangeKey, KeyType.RANGE)));
    createTableRequest
            .setProvisionedThroughput(new ProvisionedThroughput(readCapacityUnits, writeCapacityUnits));
    createTableRequest
            .setAttributeDefinitions(Arrays.asList(new AttributeDefinition(key, ScalarAttributeType.S),
                    new AttributeDefinition(rangeKey, ScalarAttributeType.N)));
    try {
        client.createTable(createTableRequest);
    } catch (ResourceInUseException e) {
        throw new IllegalStateException("The table may already be getting created.", e);
    }
    LOG.info("Table " + tableName + " created");
    waitForActive(client, tableName);
}

From source file:Database.TableFunctions.java

public static void createTable() {
        ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits(5L)
                .withWriteCapacityUnits(5L);
        CreateTableRequest request = new CreateTableRequest().withTableName(tablename)
                .withProvisionedThroughput(provisionedThroughput);

        ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();// --------creates empty list of attributes ------
        attributeDefinitions.add(new AttributeDefinition().withAttributeName(attributeKey).withAttributeType("S"));
        request.setAttributeDefinitions(attributeDefinitions);

        ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();// --------creates empty list of keys ------
        tableKeySchema.add(new KeySchemaElement().withAttributeName(attributeKey).withKeyType(KeyType.HASH));
        request.setKeySchema(tableKeySchema);

        dynamoDB.createTable(request);//  w  w  w. j  av a 2 s  .com
        waitForTableToBecomeAvailable(tablename);
        getTableInformation();
    }

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 {//from www . j  a  va2  s.  co 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:org.apache.gora.dynamodb.store.DynamoDBUtils.java

License:Apache License

/**
 * Builds the necessary requests to create tables
 * /*from w  ww.  j  a  v a2s  .  c om*/
 * @param tableName
 * @param keySchema
 * @param proThrou
 * @param attrs 
 * @return
 */
public static CreateTableRequest buildCreateTableRequest(String tableName,
        ArrayList<KeySchemaElement> keySchema, ProvisionedThroughput proThrou, Map<String, String> attrs) {
    CreateTableRequest createTableRequest = new CreateTableRequest();
    createTableRequest.setTableName(tableName);
    createTableRequest.setKeySchema(keySchema);
    ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
    for (KeySchemaElement kEle : keySchema) {
        AttributeDefinition attrDef = new AttributeDefinition();
        attrDef.setAttributeName(kEle.getAttributeName());
        attrDef.setAttributeType(attrs.get(kEle.getAttributeName()));
        attributeDefinitions.add(attrDef);
    }
    createTableRequest.setAttributeDefinitions(attributeDefinitions);
    createTableRequest.setProvisionedThroughput(proThrou);
    return createTableRequest;
}

From source file:org.apache.metamodel.dynamodb.DynamoDbTableCreationBuilder.java

License:Apache License

@Override
public Table execute() throws MetaModelException {
    final MutableTable table = getTable();
    final String tableName = table.getName();

    final Collection<AttributeDefinition> attributes = new ArrayList<>();
    final Collection<KeySchemaElement> keySchema = new ArrayList<>();
    final Collection<GlobalSecondaryIndex> globalSecondaryIndices = new ArrayList<>();

    final long readCapacity = Long
            .parseLong(System.getProperty(DynamoDbDataContext.SYSTEM_PROPERTY_THROUGHPUT_READ_CAPACITY, "5"));
    final long writeCapacity = Long
            .parseLong(System.getProperty(DynamoDbDataContext.SYSTEM_PROPERTY_THROUGHPUT_WRITE_CAPACITY, "5"));
    final ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput(readCapacity, writeCapacity);

    for (Column column : table.getColumns()) {
        if (column.isPrimaryKey()) {
            final KeyType keyType = getKeyType(column.getRemarks());
            keySchema.add(new KeySchemaElement(column.getName(), keyType));
            attributes.add(//ww w  .  jav  a 2 s .com
                    new AttributeDefinition(column.getName(), DynamoDbUtils.toAttributeType(column.getType())));
        }
    }

    final CreateTableRequest createTableRequest = new CreateTableRequest();
    createTableRequest.setTableName(tableName);
    createTableRequest.setAttributeDefinitions(attributes);
    createTableRequest.setGlobalSecondaryIndexes(globalSecondaryIndices);
    createTableRequest.setKeySchema(keySchema);
    createTableRequest.setProvisionedThroughput(provisionedThroughput);

    final AmazonDynamoDB client = getUpdateCallback().getDataContext().getDynamoDb();

    final CreateTableResult createTableResult = client.createTable(createTableRequest);

    // await the table creation to be "done".
    {
        String tableStatus = createTableResult.getTableDescription().getTableStatus();
        while (TableStatus.CREATING.name().equals(tableStatus)) {
            logger.debug("Waiting for table status to be ACTIVE. Currently: {}", tableStatus);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                getUpdateCallback().setInterrupted(true);
            }
            tableStatus = client.describeTable(tableName).getTable().getTableStatus();
        }
    }

    return table;
}

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

License:Apache License

public void createDynamoTable(DynamoDB dynamoDB) {

    try {// ww w  .j a  v  a  2 s. c om
        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());
    }

}