Search in sources :

Example 21 with ProvisionedThroughput

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

the class UpdateTable method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    UpdateTable <table> <read> <write>\n\n" + "Where:\n" + "    table - the table to put the item in.\n" + "    read  - the new read capacity of the table.\n" + "    write - the new write capacity of the table.\n\n" + "Example:\n" + "    UpdateTable HelloTable 16 10\n";
    if (args.length < 3) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String table_name = args[0];
    Long read_capacity = Long.parseLong(args[1]);
    Long write_capacity = Long.parseLong(args[2]);
    System.out.format("Updating %s with new provisioned throughput values\n", table_name);
    System.out.format("Read capacity : %d\n", read_capacity);
    System.out.format("Write capacity : %d\n", write_capacity);
    ProvisionedThroughput table_throughput = new ProvisionedThroughput(read_capacity, write_capacity);
    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
    try {
        ddb.updateTable(table_name, table_throughput);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
Also used : AmazonServiceException(com.amazonaws.AmazonServiceException) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Example 22 with ProvisionedThroughput

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

the class CreateTablesLoadData method createTable.

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>();
        // Partition
        keySchema.add(new KeySchemaElement().withAttributeName(partitionKeyName).withKeyType(KeyType.HASH));
        // key
        ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
        attributeDefinitions.add(new AttributeDefinition().withAttributeName(partitionKeyName).withAttributeType(partitionKeyType));
        if (sortKeyName != null) {
            // Sort
            keySchema.add(new KeySchemaElement().withAttributeName(sortKeyName).withKeyType(KeyType.RANGE));
            // 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(// Partition
            new KeySchemaElement().withAttributeName(partitionKeyName).withKeyType(KeyType.HASH), // key
            new KeySchemaElement().withAttributeName("PostedBy").withKeyType(// Sort
            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());
    }
}
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 23 with ProvisionedThroughput

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

the class CreateTableCompositeKey method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    CreateTable <table>\n\n" + "Where:\n" + "    table - the table to create.\n\n" + "Example:\n" + "    CreateTable GreetingsTable\n";
    if (args.length < 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    /* Read the name from command args */
    String table_name = args[0];
    System.out.format("Creating table %s\n with a composite primary key:\n");
    System.out.format("* Language - partition key\n");
    System.out.format("* Greeting - sort key\n");
    CreateTableRequest request = new CreateTableRequest().withAttributeDefinitions(new AttributeDefinition("Language", ScalarAttributeType.S), new AttributeDefinition("Greeting", ScalarAttributeType.S)).withKeySchema(new KeySchemaElement("Language", KeyType.HASH), new KeySchemaElement("Greeting", KeyType.RANGE)).withProvisionedThroughput(new ProvisionedThroughput(new Long(10), new Long(10))).withTableName(table_name);
    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
    try {
        CreateTableResult result = ddb.createTable(request);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
Also used : AmazonServiceException(com.amazonaws.AmazonServiceException) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) CreateTableResult(com.amazonaws.services.dynamodbv2.model.CreateTableResult)

Example 24 with ProvisionedThroughput

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

the class StreamsLowLevelDemo method main.

public static void main(String[] args) throws InterruptedException {
    AmazonDynamoDB dynamoDBClient = AmazonDynamoDBClientBuilder.standard().withRegion(Regions.US_EAST_2).withCredentials(new DefaultAWSCredentialsProviderChain()).build();
    AmazonDynamoDBStreams streamsClient = AmazonDynamoDBStreamsClientBuilder.standard().withRegion(Regions.US_EAST_2).withCredentials(new DefaultAWSCredentialsProviderChain()).build();
    // Create a table, with a stream enabled
    String tableName = "TestTableForStreams";
    ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<>(Arrays.asList(new AttributeDefinition().withAttributeName("Id").withAttributeType("N")));
    ArrayList<KeySchemaElement> keySchema = new ArrayList<>(Arrays.asList(new KeySchemaElement().withAttributeName("Id").withKeyType(// Partition key
    KeyType.HASH)));
    StreamSpecification streamSpecification = new StreamSpecification().withStreamEnabled(true).withStreamViewType(StreamViewType.NEW_AND_OLD_IMAGES);
    CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName).withKeySchema(keySchema).withAttributeDefinitions(attributeDefinitions).withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(10L).withWriteCapacityUnits(10L)).withStreamSpecification(streamSpecification);
    System.out.println("Issuing CreateTable request for " + tableName);
    dynamoDBClient.createTable(createTableRequest);
    System.out.println("Waiting for " + tableName + " to be created...");
    try {
        TableUtils.waitUntilActive(dynamoDBClient, tableName);
    } catch (AmazonClientException e) {
        e.printStackTrace();
    }
    // Print the stream settings for the table
    DescribeTableResult describeTableResult = dynamoDBClient.describeTable(tableName);
    String streamArn = describeTableResult.getTable().getLatestStreamArn();
    System.out.println("Current stream ARN for " + tableName + ": " + describeTableResult.getTable().getLatestStreamArn());
    StreamSpecification streamSpec = describeTableResult.getTable().getStreamSpecification();
    System.out.println("Stream enabled: " + streamSpec.getStreamEnabled());
    System.out.println("Update view type: " + streamSpec.getStreamViewType());
    System.out.println();
    // Generate write activity in the table
    System.out.println("Performing write activities on " + tableName);
    int maxItemCount = 100;
    for (Integer i = 1; i <= maxItemCount; i++) {
        System.out.println("Processing item " + i + " of " + maxItemCount);
        // Write a new item
        Map<String, AttributeValue> item = new HashMap<>();
        item.put("Id", new AttributeValue().withN(i.toString()));
        item.put("Message", new AttributeValue().withS("New item!"));
        dynamoDBClient.putItem(tableName, item);
        // Update the item
        Map<String, AttributeValue> key = new HashMap<>();
        key.put("Id", new AttributeValue().withN(i.toString()));
        Map<String, AttributeValueUpdate> attributeUpdates = new HashMap<>();
        attributeUpdates.put("Message", new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(new AttributeValue().withS("This item has changed")));
        dynamoDBClient.updateItem(tableName, key, attributeUpdates);
        // Delete the item
        dynamoDBClient.deleteItem(tableName, key);
    }
    // Get all the shard IDs from the stream.  Note that DescribeStream returns
    // the shard IDs one page at a time.
    String lastEvaluatedShardId = null;
    do {
        DescribeStreamResult describeStreamResult = streamsClient.describeStream(new DescribeStreamRequest().withStreamArn(streamArn).withExclusiveStartShardId(lastEvaluatedShardId));
        List<Shard> shards = describeStreamResult.getStreamDescription().getShards();
        for (Shard shard : shards) {
            String shardId = shard.getShardId();
            System.out.println("Shard: " + shard);
            // Get an iterator for the current shard
            GetShardIteratorRequest getShardIteratorRequest = new GetShardIteratorRequest().withStreamArn(streamArn).withShardId(shardId).withShardIteratorType(ShardIteratorType.TRIM_HORIZON);
            GetShardIteratorResult getShardIteratorResult = streamsClient.getShardIterator(getShardIteratorRequest);
            String currentShardIter = getShardIteratorResult.getShardIterator();
            // Shard iterator is not null until the Shard is sealed (marked as READ_ONLY).
            // To prevent running the loop until the Shard is sealed, which will be on average
            // 4 hours, we process only the items that were written into DynamoDB and then exit.
            int processedRecordCount = 0;
            while (currentShardIter != null && processedRecordCount < maxItemCount) {
                System.out.println("    Shard iterator: " + currentShardIter.substring(380));
                // Use the shard iterator to read the stream records
                GetRecordsResult getRecordsResult = streamsClient.getRecords(new GetRecordsRequest().withShardIterator(currentShardIter));
                List<Record> records = getRecordsResult.getRecords();
                for (Record record : records) {
                    System.out.println("        " + record.getDynamodb());
                }
                processedRecordCount += records.size();
                currentShardIter = getRecordsResult.getNextShardIterator();
            }
        }
        // If LastEvaluatedShardId is set, then there is
        // at least one more page of shard IDs to retrieve
        lastEvaluatedShardId = describeStreamResult.getStreamDescription().getLastEvaluatedShardId();
    } while (lastEvaluatedShardId != null);
    // Delete the table
    System.out.println("Deleting the table...");
    dynamoDBClient.deleteTable(tableName);
    System.out.println("Demo complete");
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) GetRecordsResult(com.amazonaws.services.dynamodbv2.model.GetRecordsResult) HashMap(java.util.HashMap) AmazonDynamoDBStreams(com.amazonaws.services.dynamodbv2.AmazonDynamoDBStreams) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) DescribeStreamRequest(com.amazonaws.services.dynamodbv2.model.DescribeStreamRequest) GetRecordsRequest(com.amazonaws.services.dynamodbv2.model.GetRecordsRequest) GetShardIteratorResult(com.amazonaws.services.dynamodbv2.model.GetShardIteratorResult) Record(com.amazonaws.services.dynamodbv2.model.Record) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) DefaultAWSCredentialsProviderChain(com.amazonaws.auth.DefaultAWSCredentialsProviderChain) AttributeValueUpdate(com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate) StreamSpecification(com.amazonaws.services.dynamodbv2.model.StreamSpecification) GetShardIteratorRequest(com.amazonaws.services.dynamodbv2.model.GetShardIteratorRequest) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DescribeTableResult(com.amazonaws.services.dynamodbv2.model.DescribeTableResult) DescribeStreamResult(com.amazonaws.services.dynamodbv2.model.DescribeStreamResult) Shard(com.amazonaws.services.dynamodbv2.model.Shard)

Example 25 with ProvisionedThroughput

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

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