Search in sources :

Example 1 with StreamSpecification

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

use of com.amazonaws.services.dynamodbv2.model.StreamSpecification in project wildfly-camel by wildfly-extras.

the class DynamoDBUtils method createTable.

public static TableDescription createTable(AmazonDynamoDB client, String tableName) throws InterruptedException {
    CreateTableRequest tableReq = new CreateTableRequest().withTableName(tableName).withKeySchema(new KeySchemaElement("Id", KeyType.HASH)).withAttributeDefinitions(new AttributeDefinition("Id", ScalarAttributeType.N)).withProvisionedThroughput(new ProvisionedThroughput(10L, 10L)).withStreamSpecification(new StreamSpecification().withStreamEnabled(true).withStreamViewType(StreamViewType.NEW_AND_OLD_IMAGES));
    DynamoDB dynamoDB = new DynamoDB(client);
    Table table = dynamoDB.createTable(tableReq);
    return table.waitForActive();
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) StreamSpecification(com.amazonaws.services.dynamodbv2.model.StreamSpecification) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement)

Example 3 with StreamSpecification

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

Aggregations

AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)3 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)3 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)3 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)3 StreamSpecification (com.amazonaws.services.dynamodbv2.model.StreamSpecification)3 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)2 ArrayList (java.util.ArrayList)2 AmazonClientException (com.amazonaws.AmazonClientException)1 DefaultAWSCredentialsProviderChain (com.amazonaws.auth.DefaultAWSCredentialsProviderChain)1 AmazonDynamoDBStreams (com.amazonaws.services.dynamodbv2.AmazonDynamoDBStreams)1 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)1 Table (com.amazonaws.services.dynamodbv2.document.Table)1 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)1 AttributeValueUpdate (com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate)1 CreateTableResult (com.amazonaws.services.dynamodbv2.model.CreateTableResult)1 DescribeStreamRequest (com.amazonaws.services.dynamodbv2.model.DescribeStreamRequest)1 DescribeStreamResult (com.amazonaws.services.dynamodbv2.model.DescribeStreamResult)1 DescribeTableResult (com.amazonaws.services.dynamodbv2.model.DescribeTableResult)1 GetRecordsRequest (com.amazonaws.services.dynamodbv2.model.GetRecordsRequest)1 GetRecordsResult (com.amazonaws.services.dynamodbv2.model.GetRecordsResult)1