com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndex Java Examples

The following examples show how to use com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndex. 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: TableHelper.java    From dynamodb-transactions with Apache License 2.0 6 votes vote down vote up
public void waitForTableActive(String tableName, 
    List<AttributeDefinition> definitions, 
    List<KeySchemaElement> keySchema,
    List<LocalSecondaryIndex> localIndexes,
    long waitTimeSeconds) throws InterruptedException {
    
    if(waitTimeSeconds < 0) {
        throw new IllegalArgumentException("Invalid waitTimeSeconds " + waitTimeSeconds);
    }
    
    long startTimeMs = System.currentTimeMillis();
    long elapsedMs = 0;
    do {
        String status = verifyTableExists(tableName, definitions, keySchema, localIndexes);
        if(TableStatus.ACTIVE.toString().equals(status)) {
            return;
        }
        if(TableStatus.DELETING.toString().equals(status)) {
            throw new ResourceInUseException("Table " + tableName + " is " + status + ", and waiting for it to become ACTIVE is not useful.");
        }
        Thread.sleep(10 * 1000);
        elapsedMs = System.currentTimeMillis() - startTimeMs; 
    } while(elapsedMs / 1000.0 < waitTimeSeconds);
    
    throw new ResourceInUseException("Table " + tableName + " did not become ACTIVE after " + waitTimeSeconds + " seconds.");
}
 
Example #2
Source File: GeoTableUtil.java    From dynamodb-geo with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * Construct a create table request object based on GeoDataManagerConfiguration. The users can update any aspect of
 * the request and call it.
 * </p>
 * Example:
 * 
 * <pre>
 * AmazonDynamoDBClient ddb = new AmazonDynamoDBClient(new ClasspathPropertiesFileCredentialsProvider());
 * Region usWest2 = Region.getRegion(Regions.US_WEST_2);
 * ddb.setRegion(usWest2);
 * 
 * CreateTableRequest createTableRequest = GeoTableUtil.getCreateTableRequest(config);
 * CreateTableResult createTableResult = ddb.createTable(createTableRequest);
 * </pre>
 * 
 * @return Generated create table request.
 */
public static CreateTableRequest getCreateTableRequest(GeoDataManagerConfiguration config) {
	CreateTableRequest createTableRequest = new CreateTableRequest()
			.withTableName(config.getTableName())
			.withProvisionedThroughput(
					new ProvisionedThroughput().withReadCapacityUnits(10L).withWriteCapacityUnits(5L))
			.withKeySchema(
					new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName(
							config.getHashKeyAttributeName()),
					new KeySchemaElement().withKeyType(KeyType.RANGE).withAttributeName(
							config.getRangeKeyAttributeName()))
			.withAttributeDefinitions(
					new AttributeDefinition().withAttributeType(ScalarAttributeType.N).withAttributeName(
							config.getHashKeyAttributeName()),
					new AttributeDefinition().withAttributeType(ScalarAttributeType.S).withAttributeName(
							config.getRangeKeyAttributeName()),
					new AttributeDefinition().withAttributeType(ScalarAttributeType.N).withAttributeName(
							config.getGeohashAttributeName()))
			.withLocalSecondaryIndexes(
					new LocalSecondaryIndex()
							.withIndexName(config.getGeohashIndexName())
							.withKeySchema(
									new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName(
											config.getHashKeyAttributeName()),
									new KeySchemaElement().withKeyType(KeyType.RANGE).withAttributeName(
											config.getGeohashAttributeName()))
							.withProjection(new Projection().withProjectionType(ProjectionType.ALL)));

	return createTableRequest;
}
 
Example #3
Source File: TableHelper.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the table exists with the specified schema, and creates it if it does not exist.
 * 
 * @param tableName
 * @param definitions
 * @param keySchema
 * @param localIndexes
 * @param provisionedThroughput
 * @param waitTimeSeconds
 * @throws InterruptedException 
 */
public void verifyOrCreateTable(
    String tableName, 
    List<AttributeDefinition> definitions, 
    List<KeySchemaElement> keySchema,
    List<LocalSecondaryIndex> localIndexes,
    ProvisionedThroughput provisionedThroughput,
    Long waitTimeSeconds) throws InterruptedException {
    
    if(waitTimeSeconds != null && waitTimeSeconds < 0) {
        throw new IllegalArgumentException("Invalid waitTimeSeconds " + waitTimeSeconds);
    }
    
    String status = null;
    try {
        status = verifyTableExists(tableName, definitions, keySchema, localIndexes);
    } catch(ResourceNotFoundException e) {
        status = client.createTable(new CreateTableRequest()
            .withTableName(tableName)
            .withAttributeDefinitions(definitions)
            .withKeySchema(keySchema)
            .withLocalSecondaryIndexes(localIndexes)
            .withProvisionedThroughput(provisionedThroughput)).getTableDescription().getTableStatus();
    }
    
    if(waitTimeSeconds != null && ! TableStatus.ACTIVE.toString().equals(status)) {
        waitForTableActive(tableName, definitions, keySchema, localIndexes, waitTimeSeconds);
    }
}
 
Example #4
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 #5
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 #6
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 #7
Source File: DynamoDBCryptoIntegrationTestBase.java    From aws-dynamodb-encryption-java with Apache License 2.0 4 votes vote down vote up
protected static void setUpTableWithIndexRangeAttribute(boolean recreateTable) throws Exception {
    setUp();
    if (recreateTable) {
        dynamo.deleteTable(new DeleteTableRequest().withTableName(TABLE_WITH_INDEX_RANGE_ATTRIBUTE));
        waitForTableToBecomeDeleted(TABLE_WITH_INDEX_RANGE_ATTRIBUTE);
    }

    String keyName = DynamoDBCryptoIntegrationTestBase.KEY_NAME;
    String rangeKeyAttributeName = "rangeKey";
    String indexFooRangeKeyAttributeName = "indexFooRangeKey";
    String indexBarRangeKeyAttributeName = "indexBarRangeKey";
    String multipleIndexRangeKeyAttributeName = "multipleIndexRangeKey";
    String indexFooName = "index_foo";
    String indexBarName = "index_bar";
    String indexFooCopyName = "index_foo_copy";
    String indexBarCopyName = "index_bar_copy";

    CreateTableRequest createTableRequest = new CreateTableRequest()
            .withTableName(TABLE_WITH_INDEX_RANGE_ATTRIBUTE)
            .withKeySchema(
                    new KeySchemaElement().withAttributeName(keyName).withKeyType(KeyType.HASH),
                    new KeySchemaElement().withAttributeName(rangeKeyAttributeName).withKeyType(KeyType.RANGE))
            .withLocalSecondaryIndexes(
                    new LocalSecondaryIndex()
                            .withIndexName(indexFooName)
                            .withKeySchema(
                                    new KeySchemaElement().withAttributeName(keyName).withKeyType(KeyType.HASH),
                                    new KeySchemaElement().withAttributeName(indexFooRangeKeyAttributeName).withKeyType(KeyType.RANGE))
                            .withProjection(new Projection().withProjectionType(ProjectionType.ALL)),
                    new LocalSecondaryIndex()
                            .withIndexName(indexBarName)
                            .withKeySchema(
                                    new KeySchemaElement().withAttributeName(keyName).withKeyType(KeyType.HASH),
                                    new KeySchemaElement().withAttributeName(indexBarRangeKeyAttributeName).withKeyType(KeyType.RANGE))
                            .withProjection(new Projection()
                                                .withProjectionType(ProjectionType.ALL)),
                    new LocalSecondaryIndex()
                            .withIndexName(indexFooCopyName)
                            .withKeySchema(
                                    new KeySchemaElement().withAttributeName(keyName).withKeyType(KeyType.HASH),
                                    new KeySchemaElement().withAttributeName(multipleIndexRangeKeyAttributeName).withKeyType(KeyType.RANGE))
                            .withProjection(new Projection()
                                                .withProjectionType(ProjectionType.ALL)),
                    new LocalSecondaryIndex()
                            .withIndexName(indexBarCopyName)
                            .withKeySchema(
                                    new KeySchemaElement().withAttributeName(keyName).withKeyType(KeyType.HASH),
                                    new KeySchemaElement().withAttributeName(multipleIndexRangeKeyAttributeName).withKeyType(KeyType.RANGE))
                            .withProjection(new Projection()
                                                .withProjectionType(ProjectionType.ALL)))
            .withAttributeDefinitions(
                    new AttributeDefinition().withAttributeName(keyName).withAttributeType(ScalarAttributeType.N),
                    new AttributeDefinition().withAttributeName(rangeKeyAttributeName).withAttributeType(ScalarAttributeType.N),
                    new AttributeDefinition().withAttributeName(indexFooRangeKeyAttributeName).withAttributeType(ScalarAttributeType.N),
                    new AttributeDefinition().withAttributeName(indexBarRangeKeyAttributeName).withAttributeType(ScalarAttributeType.N),
                    new AttributeDefinition().withAttributeName(multipleIndexRangeKeyAttributeName).withAttributeType(ScalarAttributeType.N));
    createTableRequest.setProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(10L)
            .withWriteCapacityUnits(5L));

    if (TableUtils.createTableIfNotExists(dynamo, createTableRequest)) {
        TableUtils.waitUntilActive(dynamo, TABLE_WITH_INDEX_RANGE_ATTRIBUTE);
    }
}
 
Example #8
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 #9
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 #10
Source File: TableHelper.java    From dynamodb-transactions with Apache License 2.0 4 votes vote down vote up
public String verifyTableExists( 
    String tableName, 
    List<AttributeDefinition> definitions, 
    List<KeySchemaElement> keySchema,
    List<LocalSecondaryIndex> localIndexes) {
    
    DescribeTableResult describe = client.describeTable(new DescribeTableRequest().withTableName(tableName));
    if(! new HashSet<AttributeDefinition>(definitions).equals(new HashSet<AttributeDefinition>(describe.getTable().getAttributeDefinitions()))) {
        throw new ResourceInUseException("Table " + tableName + " had the wrong AttributesToGet." 
            + " Expected: " + definitions + " "
            + " Was: " + describe.getTable().getAttributeDefinitions());
    }
    
    if(! keySchema.equals(describe.getTable().getKeySchema())) {
        throw new ResourceInUseException("Table " + tableName + " had the wrong KeySchema." 
            + " Expected: " + keySchema + " "
            + " Was: " + describe.getTable().getKeySchema());
    }
    
    List<LocalSecondaryIndex> theirLSIs = null;
    if(describe.getTable().getLocalSecondaryIndexes() != null) {
        theirLSIs = new ArrayList<LocalSecondaryIndex>();
        for(LocalSecondaryIndexDescription description : describe.getTable().getLocalSecondaryIndexes()) {
            LocalSecondaryIndex lsi = new LocalSecondaryIndex()
                .withIndexName(description.getIndexName())
                .withKeySchema(description.getKeySchema())
                .withProjection(description.getProjection());
            theirLSIs.add(lsi);
        }
    }
    
    if(localIndexes != null) {
        if(! new HashSet<LocalSecondaryIndex>(localIndexes).equals(new HashSet<LocalSecondaryIndex>(theirLSIs))) {
            throw new ResourceInUseException("Table " + tableName + " did not have the expected LocalSecondaryIndexes."
                + " Expected: " + localIndexes
                + " Was: " + theirLSIs);
        }
    } else {
        if(theirLSIs != null) {
            throw new ResourceInUseException("Table " + tableName + " had local secondary indexes, but expected none."
                + " Indexes: " + theirLSIs);
        }
    }

    return describe.getTable().getTableStatus();
}