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

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

Introduction

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

Prototype


public void setProvisionedThroughput(ProvisionedThroughput provisionedThroughput) 

Source Link

Document

Represents the provisioned throughput settings for a specified table or index.

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//  w ww .  ja  v  a2s .  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()//from ww  w.jav a  2s.c o  m
            .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   w  w  w .  j  a  v a 2  s  . com
            .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 v  a 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:org.apache.gora.dynamodb.store.DynamoDBUtils.java

License:Apache License

/**
 * Builds the necessary requests to create tables
 * /*  w  w w. jav  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(/* www .  j a  va  2 s.  c  o m*/
                    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.openhab.persistence.dynamodb.internal.DynamoDBPersistenceService.java

License:Open Source License

/**
 * Create table (if not present) and wait for table to become active.
 *
 * Synchronized in order to ensure that at most single thread is creating the table at a time
 *
 * @param mapper/*from  w  ww. j a  v  a 2  s  .  co m*/
 * @param dtoClass
 * @return whether table creation succeeded.
 */
private synchronized boolean createTable(DynamoDBMapper mapper, Class<?> dtoClass) {
    if (db == null) {
        return false;
    }
    String tableName;
    try {
        ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput(dbConfig.getReadCapacityUnits(),
                dbConfig.getWriteCapacityUnits());
        CreateTableRequest request = mapper.generateCreateTableRequest(dtoClass);
        request.setProvisionedThroughput(provisionedThroughput);
        if (request.getGlobalSecondaryIndexes() != null) {
            for (GlobalSecondaryIndex index : request.getGlobalSecondaryIndexes()) {
                index.setProvisionedThroughput(provisionedThroughput);
            }
        }
        tableName = request.getTableName();
        try {
            db.getDynamoClient().describeTable(tableName);
        } catch (ResourceNotFoundException e) {
            // No table present, continue with creation
            db.getDynamoClient().createTable(request);
        } catch (AmazonClientException e) {
            logger.error("Table creation failed due to error in describeTable operation", e);
            return false;
        }

        // table found or just created, wait
        return waitForTableToBecomeActive(tableName);

    } catch (AmazonClientException e) {
        logger.error("Exception when creating table", e);
        return false;
    }

}

From source file:org.socialsignin.spring.data.dynamodb.repository.util.Entity2DynamoDBTableSynchronizer.java

License:Apache License

private boolean performCreate(DynamoDBEntityInformation<T, ID> entityInformation)
        throws TableNeverTransitionedToStateException, InterruptedException {
    Class<T> domainType = entityInformation.getJavaType();

    CreateTableRequest ctr = mapper.generateCreateTableRequest(domainType);
    LOGGER.trace("Creating table {} for entity {}", ctr.getTableName(), domainType);
    ctr.setProvisionedThroughput(pt);

    if (ctr.getGlobalSecondaryIndexes() != null) {
        ctr.getGlobalSecondaryIndexes().forEach(gsi -> {
            gsi.setProjection(new Projection().withProjectionType(gsiProjectionType));
            gsi.setProvisionedThroughput(pt);
        });//w w  w  .  ja  v  a 2  s . com
    }

    boolean result = TableUtils.createTableIfNotExists(amazonDynamoDB, ctr);
    if (result) {
        TableUtils.waitUntilActive(amazonDynamoDB, ctr.getTableName());
        LOGGER.debug("Created table {} for entity {}", ctr.getTableName(), domainType);
    }
    return result;
}