Search in sources :

Example 1 with GlobalSecondaryIndex

use of com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex in project openhab1-addons by openhab.

the class DynamoDBPersistenceService method createTable.

/**
     * 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
     * @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;
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) GlobalSecondaryIndex(com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex)

Example 2 with GlobalSecondaryIndex

use of com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex in project aws-doc-sdk-examples by awsdocs.

the class DocumentAPIGlobalSecondaryIndexExample method createTable.

public static void createTable() {
    // Attribute definitions
    ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("IssueId").withAttributeType("S"));
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("Title").withAttributeType("S"));
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("CreateDate").withAttributeType("S"));
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("DueDate").withAttributeType("S"));
    // Key schema for table
    ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
    // Partition
    tableKeySchema.add(new KeySchemaElement().withAttributeName("IssueId").withKeyType(KeyType.HASH));
    // key
    // Sort
    tableKeySchema.add(new KeySchemaElement().withAttributeName("Title").withKeyType(KeyType.RANGE));
    // key
    // Initial provisioned throughput settings for the indexes
    ProvisionedThroughput ptIndex = new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L);
    // CreateDateIndex
    GlobalSecondaryIndex createDateIndex = new GlobalSecondaryIndex().withIndexName("CreateDateIndex").withProvisionedThroughput(ptIndex).withKeySchema(// Partition
    new KeySchemaElement().withAttributeName("CreateDate").withKeyType(KeyType.HASH), // key
    new KeySchemaElement().withAttributeName("IssueId").withKeyType(// Sort
    KeyType.RANGE)).withProjection(new Projection().withProjectionType("INCLUDE").withNonKeyAttributes("Description", "Status"));
    // TitleIndex
    GlobalSecondaryIndex titleIndex = new GlobalSecondaryIndex().withIndexName("TitleIndex").withProvisionedThroughput(ptIndex).withKeySchema(// Partition
    new KeySchemaElement().withAttributeName("Title").withKeyType(KeyType.HASH), // key
    new KeySchemaElement().withAttributeName("IssueId").withKeyType(// Sort
    KeyType.RANGE)).withProjection(new Projection().withProjectionType("KEYS_ONLY"));
    // DueDateIndex
    GlobalSecondaryIndex dueDateIndex = new GlobalSecondaryIndex().withIndexName("DueDateIndex").withProvisionedThroughput(ptIndex).withKeySchema(// Partition
    new KeySchemaElement().withAttributeName("DueDate").withKeyType(KeyType.HASH)).withProjection(new Projection().withProjectionType("ALL"));
    CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName).withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits((long) 1).withWriteCapacityUnits((long) 1)).withAttributeDefinitions(attributeDefinitions).withKeySchema(tableKeySchema).withGlobalSecondaryIndexes(createDateIndex, titleIndex, dueDateIndex);
    System.out.println("Creating table " + tableName + "...");
    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();
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) ArrayList(java.util.ArrayList) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) Projection(com.amazonaws.services.dynamodbv2.model.Projection) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) GlobalSecondaryIndex(com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement)

Example 3 with GlobalSecondaryIndex

use of com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex in project aws-doc-sdk-examples by awsdocs.

the class LowLevelGlobalSecondaryIndexExample method createTable.

public static void createTable() {
    // Attribute definitions
    ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("IssueId").withAttributeType("S"));
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("Title").withAttributeType("S"));
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("CreateDate").withAttributeType("S"));
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("DueDate").withAttributeType("S"));
    // Key schema for table
    ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
    // Partition
    tableKeySchema.add(new KeySchemaElement().withAttributeName("IssueId").withKeyType(KeyType.HASH));
    // key
    // Sort
    tableKeySchema.add(new KeySchemaElement().withAttributeName("Title").withKeyType(KeyType.RANGE));
    // key
    // Initial provisioned throughput settings for the indexes
    ProvisionedThroughput ptIndex = new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L);
    // CreateDateIndex
    GlobalSecondaryIndex createDateIndex = new GlobalSecondaryIndex().withIndexName("CreateDateIndex").withProvisionedThroughput(ptIndex).withKeySchema(// Partition
    new KeySchemaElement().withAttributeName("CreateDate").withKeyType(KeyType.HASH), // key
    new KeySchemaElement().withAttributeName("IssueId").withKeyType(// Sort
    KeyType.RANGE)).withProjection(new Projection().withProjectionType("INCLUDE").withNonKeyAttributes("Description", "Status"));
    // TitleIndex
    GlobalSecondaryIndex titleIndex = new GlobalSecondaryIndex().withIndexName("TitleIndex").withProvisionedThroughput(ptIndex).withKeySchema(// Partition
    new KeySchemaElement().withAttributeName("Title").withKeyType(KeyType.HASH), // key
    new KeySchemaElement().withAttributeName("IssueId").withKeyType(// Sort
    KeyType.RANGE)).withProjection(new Projection().withProjectionType("KEYS_ONLY"));
    // DueDateIndex
    GlobalSecondaryIndex dueDateIndex = new GlobalSecondaryIndex().withIndexName("DueDateIndex").withProvisionedThroughput(ptIndex).withKeySchema(// Partition
    new KeySchemaElement().withAttributeName("DueDate").withKeyType(KeyType.HASH)).withProjection(new Projection().withProjectionType("ALL"));
    CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName).withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits((long) 1).withWriteCapacityUnits((long) 1)).withAttributeDefinitions(attributeDefinitions).withKeySchema(tableKeySchema).withGlobalSecondaryIndexes(createDateIndex, titleIndex, dueDateIndex);
    System.out.println("Creating table " + tableName + "...");
    System.out.println(client.createTable(createTableRequest));
    waitForTableToBecomeAvailable(tableName);
}
Also used : ArrayList(java.util.ArrayList) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) Projection(com.amazonaws.services.dynamodbv2.model.Projection) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) GlobalSecondaryIndex(com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement)

Aggregations

CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)3 GlobalSecondaryIndex (com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex)3 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)3 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)2 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)2 Projection (com.amazonaws.services.dynamodbv2.model.Projection)2 ArrayList (java.util.ArrayList)2 AmazonClientException (com.amazonaws.AmazonClientException)1 Table (com.amazonaws.services.dynamodbv2.document.Table)1 ResourceNotFoundException (com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)1