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

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

Introduction

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

Prototype


public void setKeySchema(java.util.Collection<KeySchemaElement> keySchema) 

Source Link

Document

Specifies the attributes that make up the primary key for a table or an 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//from  www .  j  a v  a 2 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()//from  w  w  w. ja v  a2 s  .  com
            .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()/* w  w w.  j a  va  2 s  .co 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 {//  w w w  .j  av 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: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);/*from   w  w  w. ja  v a2 s.  c o m*/
        waitForTableToBecomeAvailable(tablename);
        getTableInformation();
    }

From source file:org.apache.gora.dynamodb.store.DynamoDBUtils.java

License:Apache License

/**
 * Builds the necessary requests to create tables
 * //from ww w. j  a v a2  s . c o m
 * @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  . j  a v a  2 s. c  om
                    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;
}