Java Code Examples for com.amazonaws.services.dynamodbv2.model.CreateTableRequest#setAttributeDefinitions()

The following examples show how to use com.amazonaws.services.dynamodbv2.model.CreateTableRequest#setAttributeDefinitions() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LowLevelTableExample.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
static void createExampleTable() {

        // Provide the initial provisioned throughput values as Java long data
        // types
        ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits(5L)
            .withWriteCapacityUnits(6L);
        CreateTableRequest request = new CreateTableRequest().withTableName(tableName)
            .withProvisionedThroughput(provisionedThroughput);

        ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
        attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N"));
        request.setAttributeDefinitions(attributeDefinitions);

        ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
        tableKeySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH)); // Partition
                                                                                                      // key
        request.setKeySchema(tableKeySchema);

        client.createTable(request);

        waitForTableToBecomeAvailable(tableName);

        getTableInformation();

    }
 
Example 2
Source File: MarsDynamoDBManagerTest.java    From aws-dynamodb-mars-json-demo with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateResourceTable() {
    final AmazonDynamoDB dynamoDB = PowerMock.createMock(AmazonDynamoDB.class);
    PowerMock.mockStatic(DynamoDBManager.class);
    final CreateTableRequest request = new CreateTableRequest();
    request.setAttributeDefinitions(MarsDynamoDBManager.RESOURCE_TABLE_ATTRIBUTE_DEFINITIONS);
    request.setKeySchema(MarsDynamoDBManager.RESOURCE_TABLE_KEY_SCHEMA);
    request.setProvisionedThroughput(PROVISIONED_THROUGHPUT);
    request.setTableName(TABLE_NAME);

    DynamoDBManager.createTable(dynamoDB, request);
    PowerMock.expectLastCall().andReturn(null);
    PowerMock.replayAll();
    MarsDynamoDBManager.createResourceTable(dynamoDB, TABLE_NAME, PROVISIONED_THROUGHPUT);
    PowerMock.verifyAll();
}
 
Example 3
Source File: LowLevelTableExample.java    From aws-dynamodb-examples with Apache License 2.0 6 votes vote down vote up
static void createExampleTable() {
            
    
    // Provide the initial provisioned throughput values as Java long data types
    ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput()
        .withReadCapacityUnits(5L)
        .withWriteCapacityUnits(6L);
    CreateTableRequest request = new CreateTableRequest()
        .withTableName(tableName)
        .withProvisionedThroughput(provisionedThroughput);
    
    ArrayList<AttributeDefinition> attributeDefinitions= new ArrayList<AttributeDefinition>();
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N"));
    request.setAttributeDefinitions(attributeDefinitions);
    
    ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
    tableKeySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH));
    request.setKeySchema(tableKeySchema);
  
    client.createTable(request);
    
    waitForTableToBecomeAvailable(tableName); 

    getTableInformation();
    
}
 
Example 4
Source File: MarsDynamoDBManager.java    From aws-dynamodb-mars-json-demo with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the table that stores images.
 *
 * @param dynamoDB
 *            {@link AmazonDynamoDB} used to create the image table
 * @param tableName
 *            name of the table to create
 * @param tableProvisionedThroughput
 *            initial provisioned throughput for the table
 * @param timeGSIProvisionedThroughput
 *            initial provisioned throughput for the time-based global secondary index
 * @param voteGSIProvisionedThroughput
 *            initial provisioned throughput for the vote-based global secondary index
 * @see <a href="http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html">Global Secondary
 *      Indexes</a> ======= initial provisioned throughput for the time-based global secondary index
 * @see <a
 *      href="http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ProvisionedThroughputIntro.html">Provisioned
 *      Throughput in Amazon DynamoDB</a>
 */
public static void createImageTable(final AmazonDynamoDB dynamoDB, final String tableName,
    final ProvisionedThroughput tableProvisionedThroughput,
    final ProvisionedThroughput timeGSIProvisionedThroughput,
    final ProvisionedThroughput voteGSIProvisionedThroughput) {
    // Set up time GSI
    final GlobalSecondaryIndex timeGSI = new GlobalSecondaryIndex();
    timeGSI.setIndexName(IMAGE_TABLE_TIME_GSI_NAME);
    timeGSI.setKeySchema(Arrays.asList(IMAGE_TABLE_TIME_GSI_HASH_KSE, IMAGE_TABLE_TIME_GSI_RANGE_KSE));
    timeGSI.setProjection(IMAGE_TABLE_TIME_GSI_PROJECTION);
    timeGSI.setProvisionedThroughput(timeGSIProvisionedThroughput);
    // Set up vote GSI
    final GlobalSecondaryIndex voteGSI = new GlobalSecondaryIndex();
    voteGSI.setIndexName(IMAGE_TABLE_VOTE_GSI_NAME);
    voteGSI.setKeySchema(Arrays.asList(IMAGE_TABLE_VOTE_GSI_HASH_KSE, IMAGE_TABLE_VOTE_GSI_RANGE_KSE));
    voteGSI.setProjection(IMAGE_TABLE_VOTE_GSI_PROJECTION);
    voteGSI.setProvisionedThroughput(voteGSIProvisionedThroughput);
    // Create table
    final CreateTableRequest request = new CreateTableRequest();
    request.setAttributeDefinitions(IMAGE_TABLE_ATTRIBUTE_DEFINITIONS);
    request.setKeySchema(IMAGE_TABLE_KEY_SCHEMA);
    request.setGlobalSecondaryIndexes(Arrays.asList(timeGSI, voteGSI));
    request.setProvisionedThroughput(tableProvisionedThroughput);
    request.setTableName(tableName);
    LOGGER.info("Creating image table: " + request);
    final TableDescription result = DynamoDBManager.createTable(dynamoDB, request);
    LOGGER.info("Image table successfully created: " + result);
}
 
Example 5
Source File: MarsDynamoDBManagerTest.java    From aws-dynamodb-mars-json-demo with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateImageTable() {
    final AmazonDynamoDB dynamoDB = PowerMock.createMock(AmazonDynamoDB.class);
    PowerMock.mockStatic(DynamoDBManager.class);
    final CreateTableRequest request = new CreateTableRequest();
    request.setAttributeDefinitions(MarsDynamoDBManager.IMAGE_TABLE_ATTRIBUTE_DEFINITIONS);
    request.setKeySchema(MarsDynamoDBManager.IMAGE_TABLE_KEY_SCHEMA);
    final GlobalSecondaryIndex timeGSI = new GlobalSecondaryIndex();
    timeGSI.setIndexName(MarsDynamoDBManager.IMAGE_TABLE_TIME_GSI_NAME);
    timeGSI.setKeySchema(Arrays.asList(MarsDynamoDBManager.IMAGE_TABLE_TIME_GSI_HASH_KSE,
        MarsDynamoDBManager.IMAGE_TABLE_TIME_GSI_RANGE_KSE));
    timeGSI.setProjection(MarsDynamoDBManager.IMAGE_TABLE_TIME_GSI_PROJECTION);
    timeGSI.setProvisionedThroughput(PROVISIONED_THROUGHPUT);
    final GlobalSecondaryIndex voteGSI = new GlobalSecondaryIndex();
    voteGSI.setIndexName(MarsDynamoDBManager.IMAGE_TABLE_VOTE_GSI_NAME);
    voteGSI.setKeySchema(Arrays.asList(MarsDynamoDBManager.IMAGE_TABLE_VOTE_GSI_HASH_KSE,
        MarsDynamoDBManager.IMAGE_TABLE_VOTE_GSI_RANGE_KSE));
    voteGSI.setProjection(MarsDynamoDBManager.IMAGE_TABLE_VOTE_GSI_PROJECTION);
    voteGSI.setProvisionedThroughput(PROVISIONED_THROUGHPUT);
    request.setGlobalSecondaryIndexes(Arrays.asList(timeGSI, voteGSI));
    request.setProvisionedThroughput(PROVISIONED_THROUGHPUT);
    request.setTableName(TABLE_NAME);

    DynamoDBManager.createTable(dynamoDB, request);
    PowerMock.expectLastCall().andReturn(null);
    PowerMock.replayAll();
    MarsDynamoDBManager.createImageTable(dynamoDB, TABLE_NAME, PROVISIONED_THROUGHPUT, PROVISIONED_THROUGHPUT,
        PROVISIONED_THROUGHPUT);
    PowerMock.verifyAll();

}
 
Example 6
Source File: DynamoDBUtils.java    From amazon-kinesis-connectors with Apache License 2.0 5 votes vote down vote up
/**
 * 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
 *        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);
}
 
Example 7
Source File: LowLevelLocalSecondaryIndexExample.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void createTable() {

        CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
            .withProvisionedThroughput(
                new ProvisionedThroughput().withReadCapacityUnits((long) 1).withWriteCapacityUnits((long) 1));

        // Attribute definitions for table partition key and sort key
        ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
        attributeDefinitions.add(new AttributeDefinition().withAttributeName("CustomerId").withAttributeType("S"));
        attributeDefinitions.add(new AttributeDefinition().withAttributeName("OrderId").withAttributeType("N"));

        // Attribute definition for index sort key attributes
        attributeDefinitions
            .add(new AttributeDefinition().withAttributeName("OrderCreationDate").withAttributeType("N"));
        attributeDefinitions.add(new AttributeDefinition().withAttributeName("IsOpen").withAttributeType("N"));

        createTableRequest.setAttributeDefinitions(attributeDefinitions);

        // Key schema for table
        ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
        tableKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH)); // Partition
                                                                                                              // key
        tableKeySchema.add(new KeySchemaElement().withAttributeName("OrderId").withKeyType(KeyType.RANGE)); // Sort
                                                                                                            // key

        createTableRequest.setKeySchema(tableKeySchema);

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

        // OrderCreationDateIndex
        LocalSecondaryIndex orderCreationDateIndex = new LocalSecondaryIndex().withIndexName("OrderCreationDateIndex");

        // Key schema for OrderCreationDateIndex
        ArrayList<KeySchemaElement> indexKeySchema = new ArrayList<KeySchemaElement>();
        indexKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH)); // Partition
                                                                                                              // key
        indexKeySchema.add(new KeySchemaElement().withAttributeName("OrderCreationDate").withKeyType(KeyType.RANGE)); // Sort
                                                                                                                      // key

        orderCreationDateIndex.setKeySchema(indexKeySchema);

        // Projection (with list of projected attributes) for
        // OrderCreationDateIndex
        Projection projection = new Projection().withProjectionType(ProjectionType.INCLUDE);
        ArrayList<String> nonKeyAttributes = new ArrayList<String>();
        nonKeyAttributes.add("ProductCategory");
        nonKeyAttributes.add("ProductName");
        projection.setNonKeyAttributes(nonKeyAttributes);

        orderCreationDateIndex.setProjection(projection);

        localSecondaryIndexes.add(orderCreationDateIndex);

        // IsOpenIndex
        LocalSecondaryIndex isOpenIndex = new LocalSecondaryIndex().withIndexName("IsOpenIndex");

        // Key schema for IsOpenIndex
        indexKeySchema = new ArrayList<KeySchemaElement>();
        indexKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH)); // Partition
                                                                                                              // key
        indexKeySchema.add(new KeySchemaElement().withAttributeName("IsOpen").withKeyType(KeyType.RANGE)); // Sort
                                                                                                           // key

        // Projection (all attributes) for IsOpenIndex
        projection = new Projection().withProjectionType(ProjectionType.ALL);

        isOpenIndex.setKeySchema(indexKeySchema);
        isOpenIndex.setProjection(projection);

        localSecondaryIndexes.add(isOpenIndex);

        // Add index definitions to CreateTable request
        createTableRequest.setLocalSecondaryIndexes(localSecondaryIndexes);

        System.out.println("Creating table " + tableName + "...");
        System.out.println(client.createTable(createTableRequest));
        waitForTableToBecomeAvailable(tableName);
    }
 
Example 8
Source File: CreateTablesLoadData.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
private static void createTable(String tableName, long readCapacityUnits, long writeCapacityUnits,
    String partitionKeyName, String partitionKeyType, String sortKeyName, String sortKeyType) {

    try {

        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());
    }
}
 
Example 9
Source File: DocumentAPILocalSecondaryIndexExample.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void createTable() {

        CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
            .withProvisionedThroughput(
                new ProvisionedThroughput().withReadCapacityUnits((long) 1).withWriteCapacityUnits((long) 1));

        // Attribute definitions for table partition and sort keys
        ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
        attributeDefinitions.add(new AttributeDefinition().withAttributeName("CustomerId").withAttributeType("S"));
        attributeDefinitions.add(new AttributeDefinition().withAttributeName("OrderId").withAttributeType("N"));

        // Attribute definition for index primary key attributes
        attributeDefinitions
            .add(new AttributeDefinition().withAttributeName("OrderCreationDate").withAttributeType("N"));
        attributeDefinitions.add(new AttributeDefinition().withAttributeName("IsOpen").withAttributeType("N"));

        createTableRequest.setAttributeDefinitions(attributeDefinitions);

        // Key schema for table
        ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
        tableKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH)); // Partition
                                                                                                              // key
        tableKeySchema.add(new KeySchemaElement().withAttributeName("OrderId").withKeyType(KeyType.RANGE)); // Sort
                                                                                                            // key

        createTableRequest.setKeySchema(tableKeySchema);

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

        // OrderCreationDateIndex
        LocalSecondaryIndex orderCreationDateIndex = new LocalSecondaryIndex().withIndexName("OrderCreationDateIndex");

        // Key schema for OrderCreationDateIndex
        ArrayList<KeySchemaElement> indexKeySchema = new ArrayList<KeySchemaElement>();
        indexKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH)); // Partition
                                                                                                              // key
        indexKeySchema.add(new KeySchemaElement().withAttributeName("OrderCreationDate").withKeyType(KeyType.RANGE)); // Sort
                                                                                                                      // key

        orderCreationDateIndex.setKeySchema(indexKeySchema);

        // Projection (with list of projected attributes) for
        // OrderCreationDateIndex
        Projection projection = new Projection().withProjectionType(ProjectionType.INCLUDE);
        ArrayList<String> nonKeyAttributes = new ArrayList<String>();
        nonKeyAttributes.add("ProductCategory");
        nonKeyAttributes.add("ProductName");
        projection.setNonKeyAttributes(nonKeyAttributes);

        orderCreationDateIndex.setProjection(projection);

        localSecondaryIndexes.add(orderCreationDateIndex);

        // IsOpenIndex
        LocalSecondaryIndex isOpenIndex = new LocalSecondaryIndex().withIndexName("IsOpenIndex");

        // Key schema for IsOpenIndex
        indexKeySchema = new ArrayList<KeySchemaElement>();
        indexKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH)); // Partition
                                                                                                              // key
        indexKeySchema.add(new KeySchemaElement().withAttributeName("IsOpen").withKeyType(KeyType.RANGE)); // Sort
                                                                                                           // key

        // Projection (all attributes) for IsOpenIndex
        projection = new Projection().withProjectionType(ProjectionType.ALL);

        isOpenIndex.setKeySchema(indexKeySchema);
        isOpenIndex.setProjection(projection);

        localSecondaryIndexes.add(isOpenIndex);

        // Add index definitions to CreateTable request
        createTableRequest.setLocalSecondaryIndexes(localSecondaryIndexes);

        System.out.println("Creating table " + tableName + "...");
        System.out.println(dynamoDB.createTable(createTableRequest));

        // Wait for table to become active
        System.out.println("Waiting for " + tableName + " to become ACTIVE...");
        try {
            Table table = dynamoDB.getTable(tableName);
            table.waitForActive();
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
 
Example 10
Source File: LowLevelLocalSecondaryIndexExample.java    From aws-dynamodb-examples with Apache License 2.0 4 votes vote down vote up
public static void createTable() {

        CreateTableRequest createTableRequest = new CreateTableRequest()
            .withTableName(tableName)
            .withProvisionedThroughput(
                new ProvisionedThroughput()
                    .withReadCapacityUnits((long)1)
                    .withWriteCapacityUnits((long) 1));

        // Attribute definitions for table hash and range key
        ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
        attributeDefinitions.add(new AttributeDefinition()
            .withAttributeName("CustomerId")
            .withAttributeType("S"));
        attributeDefinitions.add(new AttributeDefinition().withAttributeName(
                "OrderId").withAttributeType("N"));

        // Attribute definition for index range keys
        attributeDefinitions.add(new AttributeDefinition()
            .withAttributeName("OrderCreationDate")
            .withAttributeType("N"));
        attributeDefinitions.add(new AttributeDefinition()
            .withAttributeName("IsOpen")
            .withAttributeType("N"));

        createTableRequest.setAttributeDefinitions(attributeDefinitions);

        // Key schema for table
        ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
        tableKeySchema.add(new KeySchemaElement()
            .withAttributeName("CustomerId")
            .withKeyType(KeyType.HASH));
        tableKeySchema.add(new KeySchemaElement()
            .withAttributeName("OrderId")
            .withKeyType(KeyType.RANGE));

        createTableRequest.setKeySchema(tableKeySchema);

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

        // OrderCreationDateIndex
        LocalSecondaryIndex orderCreationDateIndex = new LocalSecondaryIndex()
            .withIndexName("OrderCreationDateIndex");

        // Key schema for OrderCreationDateIndex
        ArrayList<KeySchemaElement> indexKeySchema = new ArrayList<KeySchemaElement>();
        indexKeySchema.add(new KeySchemaElement()
            .withAttributeName("CustomerId")
            .withKeyType(KeyType.HASH));
        indexKeySchema.add(new KeySchemaElement()
            .withAttributeName("OrderCreationDate")
            .withKeyType(KeyType.RANGE));

        orderCreationDateIndex.setKeySchema(indexKeySchema);

        // Projection (with list of projected attributes) for
        // OrderCreationDateIndex
        Projection projection = new Projection()
            .withProjectionType(ProjectionType.INCLUDE);
        ArrayList<String> nonKeyAttributes = new ArrayList<String>();
        nonKeyAttributes.add("ProductCategory");
        nonKeyAttributes.add("ProductName");
        projection.setNonKeyAttributes(nonKeyAttributes);

        orderCreationDateIndex.setProjection(projection);

        localSecondaryIndexes.add(orderCreationDateIndex);

        // IsOpenIndex
        LocalSecondaryIndex isOpenIndex = new LocalSecondaryIndex()
            .withIndexName("IsOpenIndex");

        // Key schema for IsOpenIndex
        indexKeySchema = new ArrayList<KeySchemaElement>();
        indexKeySchema.add(new KeySchemaElement()
            .withAttributeName("CustomerId")
            .withKeyType(KeyType.HASH));
        indexKeySchema.add(new KeySchemaElement()
            .withAttributeName("IsOpen")
            .withKeyType(KeyType.RANGE));

        // Projection (all attributes) for IsOpenIndex
        projection = new Projection().withProjectionType(ProjectionType.ALL);

        isOpenIndex.setKeySchema(indexKeySchema);
        isOpenIndex.setProjection(projection);

        localSecondaryIndexes.add(isOpenIndex);

        // Add index definitions to CreateTable request
        createTableRequest.setLocalSecondaryIndexes(localSecondaryIndexes);

        System.out.println("Creating table " + tableName + "...");
        System.out.println(client.createTable(createTableRequest));
        waitForTableToBecomeAvailable(tableName);
    }
 
Example 11
Source File: CreateTablesLoadData.java    From aws-dynamodb-examples with Apache License 2.0 4 votes vote down vote up
private static void createTable(
    String tableName, long readCapacityUnits, long writeCapacityUnits, 
    String hashKeyName, String hashKeyType, 
    String rangeKeyName, String rangeKeyType) {

    try {

        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 (rangeKeyName != null) {
            keySchema.add(new KeySchemaElement()
                .withAttributeName(rangeKeyName)
                .withKeyType(KeyType.RANGE));
            attributeDefinitions.add(new AttributeDefinition()
                .withAttributeName(rangeKeyName)
                .withAttributeType(rangeKeyType));
        }

        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(hashKeyName).withKeyType(KeyType.HASH), 
                    new KeySchemaElement() .withAttributeName("PostedBy") .withKeyType(KeyType.RANGE))
                .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());
    }
}
 
Example 12
Source File: MarsDynamoDBManager.java    From aws-dynamodb-mars-json-demo with Apache License 2.0 3 votes vote down vote up
/**
 * Creates the table that stores resources with an ETag.
 *
 * @param dynamoDB
 *            {@link AmazonDynamoDB} used to create DynamoDB table
 * @param tableName
 *            name of the table to create
 * @param provisionedThroughput
 *            initial provisioned throughput for the table
 * @see <a
 *      href="http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ProvisionedThroughputIntro.html">Provisioned
 *      Througput in Amazon DynamoDB</a>
 */
public static void createResourceTable(final AmazonDynamoDB dynamoDB, final String tableName,
    final ProvisionedThroughput provisionedThroughput) {
    final CreateTableRequest request = new CreateTableRequest();
    request.setAttributeDefinitions(RESOURCE_TABLE_ATTRIBUTE_DEFINITIONS);
    request.setKeySchema(RESOURCE_TABLE_KEY_SCHEMA);
    request.setProvisionedThroughput(provisionedThroughput);
    request.setTableName(tableName);
    LOGGER.info("Creating resource table: " + request);
    DynamoDBManager.createTable(dynamoDB, request);
    LOGGER.info("Resource table successfully created");
}