Search in sources :

Example 6 with ProvisionedThroughput

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

the class StreamsAdapterDemoHelper method createTable.

/**
 * @return StreamArn
 */
public static String createTable(AmazonDynamoDB client, String tableName) {
    java.util.List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N"));
    java.util.List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
    // Partition
    keySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH));
    // key
    ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits(2L).withWriteCapacityUnits(2L);
    StreamSpecification streamSpecification = new StreamSpecification();
    streamSpecification.setStreamEnabled(true);
    streamSpecification.setStreamViewType(StreamViewType.NEW_IMAGE);
    CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName).withAttributeDefinitions(attributeDefinitions).withKeySchema(keySchema).withProvisionedThroughput(provisionedThroughput).withStreamSpecification(streamSpecification);
    try {
        System.out.println("Creating table " + tableName);
        CreateTableResult result = client.createTable(createTableRequest);
        return result.getTableDescription().getLatestStreamArn();
    } catch (ResourceInUseException e) {
        System.out.println("Table already exists.");
        return describeTable(client, tableName).getTable().getLatestStreamArn();
    }
}
Also used : StreamSpecification(com.amazonaws.services.dynamodbv2.model.StreamSpecification) 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 7 with ProvisionedThroughput

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

the class DocumentAPILocalSecondaryIndexExample method createTable.

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>();
    // Partition
    tableKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH));
    // key
    // Sort
    tableKeySchema.add(new KeySchemaElement().withAttributeName("OrderId").withKeyType(KeyType.RANGE));
    // 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>();
    // Partition
    indexKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH));
    // key
    // Sort
    indexKeySchema.add(new KeySchemaElement().withAttributeName("OrderCreationDate").withKeyType(KeyType.RANGE));
    // 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>();
    // Partition
    indexKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH));
    // key
    // Sort
    indexKeySchema.add(new KeySchemaElement().withAttributeName("IsOpen").withKeyType(KeyType.RANGE));
    // 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();
    }
}
Also used : LocalSecondaryIndex(com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndex) 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) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement)

Example 8 with ProvisionedThroughput

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

the class MoviesCreateTable method main.

public static void main(String[] args) throws Exception {
    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")).build();
    DynamoDB dynamoDB = new DynamoDB(client);
    String tableName = "Movies";
    try {
        System.out.println("Attempting to create table; please wait...");
        Table table = dynamoDB.createTable(tableName, // Partition
        Arrays.asList(// Partition
        new KeySchemaElement("year", KeyType.HASH), // key
        new KeySchemaElement("title", // Sort key
        KeyType.RANGE)), Arrays.asList(new AttributeDefinition("year", ScalarAttributeType.N), new AttributeDefinition("title", ScalarAttributeType.S)), new ProvisionedThroughput(10L, 10L));
        table.waitForActive();
        System.out.println("Success.  Table status: " + table.getDescription().getTableStatus());
    } catch (Exception e) {
        System.err.println("Unable to create table: ");
        System.err.println(e.getMessage());
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement)

Example 9 with ProvisionedThroughput

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

the class LowLevelTableExample method updateExampleTable.

static void updateExampleTable() {
    ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits(6L).withWriteCapacityUnits(7L);
    UpdateTableRequest updateTableRequest = new UpdateTableRequest().withTableName(tableName).withProvisionedThroughput(provisionedThroughput);
    client.updateTable(updateTableRequest);
    waitForTableToBecomeAvailable(tableName);
}
Also used : UpdateTableRequest(com.amazonaws.services.dynamodbv2.model.UpdateTableRequest) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)

Example 10 with ProvisionedThroughput

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

the class TryDaxHelper method createTable.

void createTable(String tableName, DynamoDB client) {
    Table table = client.getTable(tableName);
    try {
        System.out.println("Attempting to create table; please wait...");
        table = client.createTable(tableName, Arrays.asList(// Partition key
        new KeySchemaElement("pk", KeyType.HASH), // Sort key
        new KeySchemaElement("sk", KeyType.RANGE)), Arrays.asList(new AttributeDefinition("pk", ScalarAttributeType.N), new AttributeDefinition("sk", ScalarAttributeType.N)), new ProvisionedThroughput(10L, 10L));
        table.waitForActive();
        System.out.println("Successfully created table.  Table status: " + table.getDescription().getTableStatus());
    } catch (Exception e) {
        System.err.println("Unable to create table: ");
        e.printStackTrace();
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement)

Aggregations

ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)30 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)25 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)23 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)23 ArrayList (java.util.ArrayList)14 Table (com.amazonaws.services.dynamodbv2.document.Table)11 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)7 AmazonServiceException (com.amazonaws.AmazonServiceException)5 CreateTableResult (com.amazonaws.services.dynamodbv2.model.CreateTableResult)5 Projection (com.amazonaws.services.dynamodbv2.model.Projection)5 ResourceInUseException (com.amazonaws.services.dynamodbv2.model.ResourceInUseException)5 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)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 StreamSpecification (com.amazonaws.services.dynamodbv2.model.StreamSpecification)3 AmazonClientException (com.amazonaws.AmazonClientException)2 DeleteTableRequest (com.amazonaws.services.dynamodbv2.model.DeleteTableRequest)2 ResourceNotFoundException (com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)2 TableDescription (com.amazonaws.services.dynamodbv2.model.TableDescription)2