Search in sources :

Example 86 with DynamoDB

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

the class SampleDataLoad method loadSampleThreads.

private static void loadSampleThreads(String tableName) {
    try {
        // 7
        long time1 = (new Date()).getTime() - (7 * 24 * 60 * 60 * 1000);
        // days
        // ago
        // 14
        long time2 = (new Date()).getTime() - (14 * 24 * 60 * 60 * 1000);
        // days
        // ago
        // 21
        long time3 = (new Date()).getTime() - (21 * 24 * 60 * 60 * 1000);
        // days
        // ago
        Date date1 = new Date();
        date1.setTime(time1);
        Date date2 = new Date();
        date2.setTime(time2);
        Date date3 = new Date();
        date3.setTime(time3);
        dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        Table table = dynamoDB.getTable(tableName);
        System.out.println("Adding data to " + tableName);
        Item item = new Item().withPrimaryKey("ForumName", "Amazon DynamoDB").withString("Subject", "DynamoDB Thread 1").withString("Message", "DynamoDB thread 1 message").withString("LastPostedBy", "User A").withString("LastPostedDateTime", dateFormatter.format(date2)).withNumber("Views", 0).withNumber("Replies", 0).withNumber("Answered", 0).withStringSet("Tags", new HashSet<String>(Arrays.asList("index", "primarykey", "table")));
        table.putItem(item);
        item = new Item().withPrimaryKey("ForumName", "Amazon DynamoDB").withString("Subject", "DynamoDB Thread 2").withString("Message", "DynamoDB thread 2 message").withString("LastPostedBy", "User A").withString("LastPostedDateTime", dateFormatter.format(date3)).withNumber("Views", 0).withNumber("Replies", 0).withNumber("Answered", 0).withStringSet("Tags", new HashSet<String>(Arrays.asList("index", "partitionkey", "sortkey")));
        table.putItem(item);
        item = new Item().withPrimaryKey("ForumName", "Amazon S3").withString("Subject", "S3 Thread 1").withString("Message", "S3 Thread 3 message").withString("LastPostedBy", "User A").withString("LastPostedDateTime", dateFormatter.format(date1)).withNumber("Views", 0).withNumber("Replies", 0).withNumber("Answered", 0).withStringSet("Tags", new HashSet<String>(Arrays.asList("largeobjects", "multipart upload")));
        table.putItem(item);
    } catch (Exception e) {
        System.err.println("Failed to create item in " + tableName);
        System.err.println(e.getMessage());
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Table(com.amazonaws.services.dynamodbv2.document.Table) Date(java.util.Date) AmazonServiceException(com.amazonaws.AmazonServiceException) HashSet(java.util.HashSet)

Example 87 with DynamoDB

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

the class SampleDataLoad method loadSampleReplies.

private static void loadSampleReplies(String tableName) {
    try {
        // 1 day ago
        long time0 = (new Date()).getTime() - (1 * 24 * 60 * 60 * 1000);
        // 7 days ago
        long time1 = (new Date()).getTime() - (7 * 24 * 60 * 60 * 1000);
        // 14 days ago
        long time2 = (new Date()).getTime() - (14 * 24 * 60 * 60 * 1000);
        // 21 days ago
        long time3 = (new Date()).getTime() - (21 * 24 * 60 * 60 * 1000);
        Date date0 = new Date();
        date0.setTime(time0);
        Date date1 = new Date();
        date1.setTime(time1);
        Date date2 = new Date();
        date2.setTime(time2);
        Date date3 = new Date();
        date3.setTime(time3);
        dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        Table table = dynamoDB.getTable(tableName);
        System.out.println("Adding data to " + tableName);
        // Add threads.
        Item item = new Item().withPrimaryKey("Id", "Amazon DynamoDB#DynamoDB Thread 1").withString("ReplyDateTime", (dateFormatter.format(date3))).withString("Message", "DynamoDB Thread 1 Reply 1 text").withString("PostedBy", "User A");
        table.putItem(item);
        item = new Item().withPrimaryKey("Id", "Amazon DynamoDB#DynamoDB Thread 1").withString("ReplyDateTime", dateFormatter.format(date2)).withString("Message", "DynamoDB Thread 1 Reply 2 text").withString("PostedBy", "User B");
        table.putItem(item);
        item = new Item().withPrimaryKey("Id", "Amazon DynamoDB#DynamoDB Thread 2").withString("ReplyDateTime", dateFormatter.format(date1)).withString("Message", "DynamoDB Thread 2 Reply 1 text").withString("PostedBy", "User A");
        table.putItem(item);
        item = new Item().withPrimaryKey("Id", "Amazon DynamoDB#DynamoDB Thread 2").withString("ReplyDateTime", dateFormatter.format(date0)).withString("Message", "DynamoDB Thread 2 Reply 2 text").withString("PostedBy", "User A");
        table.putItem(item);
    } catch (Exception e) {
        System.err.println("Failed to create item in " + tableName);
        System.err.println(e.getMessage());
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Table(com.amazonaws.services.dynamodbv2.document.Table) Date(java.util.Date) AmazonServiceException(com.amazonaws.AmazonServiceException)

Example 88 with DynamoDB

use of com.amazonaws.services.dynamodbv2.document.DynamoDB 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 89 with DynamoDB

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

the class MoviesItemOps05 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);
    Table table = dynamoDB.getTable("Movies");
    int year = 2015;
    String title = "The Big New Movie";
    UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey(new PrimaryKey("year", year, "title", title)).withUpdateExpression("remove info.actors[0]").withConditionExpression("size(info.actors) > :num").withValueMap(new ValueMap().withNumber(":num", 3)).withReturnValues(ReturnValue.UPDATED_NEW);
    // Conditional update (we expect this to fail)
    try {
        System.out.println("Attempting a conditional update...");
        UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
        System.out.println("UpdateItem succeeded:\n" + outcome.getItem().toJSONPretty());
    } catch (Exception e) {
        System.err.println("Unable to update item: " + year + " " + title);
        System.err.println(e.getMessage());
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) UpdateItemOutcome(com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) PrimaryKey(com.amazonaws.services.dynamodbv2.document.PrimaryKey) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Example 90 with DynamoDB

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

the class MoviesScan 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);
    Table table = dynamoDB.getTable("Movies");
    ScanSpec scanSpec = new ScanSpec().withProjectionExpression("#yr, title, info.rating").withFilterExpression("#yr between :start_yr and :end_yr").withNameMap(new NameMap().with("#yr", "year")).withValueMap(new ValueMap().withNumber(":start_yr", 1950).withNumber(":end_yr", 1959));
    try {
        ItemCollection<ScanOutcome> items = table.scan(scanSpec);
        Iterator<Item> iter = items.iterator();
        while (iter.hasNext()) {
            Item item = iter.next();
            System.out.println(item.toString());
        }
    } catch (Exception e) {
        System.err.println("Unable to scan the table:");
        System.err.println(e.getMessage());
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Table(com.amazonaws.services.dynamodbv2.document.Table) ScanOutcome(com.amazonaws.services.dynamodbv2.document.ScanOutcome) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) NameMap(com.amazonaws.services.dynamodbv2.document.utils.NameMap) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) ScanSpec(com.amazonaws.services.dynamodbv2.document.spec.ScanSpec)

Aggregations

DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)38 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)32 Table (com.amazonaws.services.dynamodbv2.document.Table)25 Item (com.amazonaws.services.dynamodbv2.document.Item)19 Test (org.junit.Test)18 AmazonServiceException (com.amazonaws.AmazonServiceException)16 HashMap (java.util.HashMap)15 TestRunner (org.apache.nifi.util.TestRunner)15 AmazonClientException (com.amazonaws.AmazonClientException)14 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)14 MockFlowFile (org.apache.nifi.util.MockFlowFile)14 ArrayList (java.util.ArrayList)11 Map (java.util.Map)11 TableWriteItems (com.amazonaws.services.dynamodbv2.document.TableWriteItems)10 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)10 IOException (java.io.IOException)10 TableKeysAndAttributes (com.amazonaws.services.dynamodbv2.document.TableKeysAndAttributes)9 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)9 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)9 List (java.util.List)9