Search in sources :

Example 21 with CreateTableRequest

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

the class DynamoDBDynamicFaultInjection method createTable.

/*
     * Create the table if it already does not exist
     */
private static void createTable() {
    List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("name").withAttributeType("S"));
    List<KeySchemaElement> ks = new ArrayList<KeySchemaElement>();
    // Partition
    ks.add(new KeySchemaElement().withAttributeName("name").withKeyType(KeyType.HASH));
    // key
    ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits(10L).withWriteCapacityUnits(10L);
    CreateTableRequest request = new CreateTableRequest().withTableName(TABLENAME).withAttributeDefinitions(attributeDefinitions).withKeySchema(ks).withProvisionedThroughput(provisionedThroughput);
    try {
        CreateTableResult createdTableDescription = dynamoDBClient.createTable(request);
        logger.info("Created Table: " + createdTableDescription);
        // Wait for it to become active
        waitForTableToBecomeAvailable(TABLENAME);
    } catch (ResourceInUseException e) {
        logger.warn("Table already existed", e);
    }
}
Also used : ResourceInUseException(com.amazonaws.services.dynamodbv2.model.ResourceInUseException) ArrayList(java.util.ArrayList) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) CreateTableResult(com.amazonaws.services.dynamodbv2.model.CreateTableResult)

Example 22 with CreateTableRequest

use of com.amazonaws.services.dynamodbv2.model.CreateTableRequest 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 23 with CreateTableRequest

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

the class DocumentAPITableExample method createExampleTable.

static void createExampleTable() {
    try {
        List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
        attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N"));
        List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
        // Partition
        keySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH));
        // key
        CreateTableRequest request = new CreateTableRequest().withTableName(tableName).withKeySchema(keySchema).withAttributeDefinitions(attributeDefinitions).withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(5L).withWriteCapacityUnits(6L));
        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();
        getTableInformation();
    } catch (Exception e) {
        System.err.println("CreateTable request failed for " + tableName);
        System.err.println(e.getMessage());
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) ArrayList(java.util.ArrayList) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement)

Example 24 with CreateTableRequest

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

the class LowLevelTableExample method createExampleTable.

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>();
    // Partition
    tableKeySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH));
    // key
    request.setKeySchema(tableKeySchema);
    client.createTable(request);
    waitForTableToBecomeAvailable(tableName);
    getTableInformation();
}
Also used : ArrayList(java.util.ArrayList) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement)

Example 25 with CreateTableRequest

use of com.amazonaws.services.dynamodbv2.model.CreateTableRequest in project tutorials by eugenp.

the class ProductInfoRepositoryIntegrationTest method setup.

@Before
public void setup() {
    try {
        repository = new ProductInfoRepository();
        repository.setMapper(dynamoDBMapper);
        CreateTableRequest tableRequest = dynamoDBMapper.generateCreateTableRequest(ProductInfo.class);
        tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
        amazonDynamoDB.createTable(tableRequest);
    } catch (ResourceInUseException e) {
    // Do nothing, table already created
    }
    // TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfo on table
    dynamoDBMapper.batchDelete((List<ProductInfo>) repository.findAll());
}
Also used : ProductInfoRepository(com.baeldung.dynamodb.repository.ProductInfoRepository) ProductInfo(com.baeldung.dynamodb.entity.ProductInfo) ResourceInUseException(com.amazonaws.services.dynamodbv2.model.ResourceInUseException) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) Before(org.junit.Before)

Aggregations

CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)25 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)23 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)21 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)20 ArrayList (java.util.ArrayList)13 Table (com.amazonaws.services.dynamodbv2.document.Table)7 CreateTableResult (com.amazonaws.services.dynamodbv2.model.CreateTableResult)5 Projection (com.amazonaws.services.dynamodbv2.model.Projection)5 ResourceInUseException (com.amazonaws.services.dynamodbv2.model.ResourceInUseException)5 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)4 AmazonServiceException (com.amazonaws.AmazonServiceException)3 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)3 GlobalSecondaryIndex (com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex)3 LocalSecondaryIndex (com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndex)3 ResourceNotFoundException (com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)3 StreamSpecification (com.amazonaws.services.dynamodbv2.model.StreamSpecification)3 TableDescription (com.amazonaws.services.dynamodbv2.model.TableDescription)3 AmazonClientException (com.amazonaws.AmazonClientException)2 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)2 DeleteTableRequest (com.amazonaws.services.dynamodbv2.model.DeleteTableRequest)2