Search in sources :

Example 26 with AmazonDynamoDB

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

the class MoviesItemOps03 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("year", year, "title", title).withUpdateExpression("set info.rating = :r, info.plot=:p, info.actors=:a").withValueMap(new ValueMap().withNumber(":r", 5.5).withString(":p", "Everything happens all at once.").withList(":a", Arrays.asList("Larry", "Moe", "Curly"))).withReturnValues(ReturnValue.UPDATED_NEW);
    try {
        System.out.println("Updating the item...");
        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) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Example 27 with AmazonDynamoDB

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

the class MoviesLoadData 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");
    JsonParser parser = new JsonFactory().createParser(new File("moviedata.json"));
    JsonNode rootNode = new ObjectMapper().readTree(parser);
    Iterator<JsonNode> iter = rootNode.iterator();
    ObjectNode currentNode;
    while (iter.hasNext()) {
        currentNode = (ObjectNode) iter.next();
        int year = currentNode.path("year").asInt();
        String title = currentNode.path("title").asText();
        try {
            table.putItem(new Item().withPrimaryKey("year", year, "title", title).withJSON("info", currentNode.path("info").toString()));
            System.out.println("PutItem succeeded: " + year + " " + title);
        } catch (Exception e) {
            System.err.println("Unable to add movie: " + year + " " + title);
            System.err.println(e.getMessage());
            break;
        }
    }
    parser.close();
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonFactory(com.fasterxml.jackson.core.JsonFactory) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) JsonNode(com.fasterxml.jackson.databind.JsonNode) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) Item(com.amazonaws.services.dynamodbv2.document.Item) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 28 with AmazonDynamoDB

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

the class ListTables method main.

public static void main(String[] args) {
    System.out.println("Your DynamoDB tables:\n");
    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
    ListTablesRequest request;
    boolean more_tables = true;
    String last_name = null;
    while (more_tables) {
        try {
            if (last_name == null) {
                request = new ListTablesRequest().withLimit(10);
            } else {
                request = new ListTablesRequest().withLimit(10).withExclusiveStartTableName(last_name);
            }
            ListTablesResult table_list = ddb.listTables(request);
            List<String> table_names = table_list.getTableNames();
            if (table_names.size() > 0) {
                for (String cur_name : table_names) {
                    System.out.format("* %s\n", cur_name);
                }
            } else {
                System.out.println("No tables found!");
                System.exit(0);
            }
            last_name = table_list.getLastEvaluatedTableName();
            if (last_name == null) {
                more_tables = false;
            }
        } catch (AmazonServiceException e) {
            System.err.println(e.getErrorMessage());
            System.exit(1);
        }
    }
    System.out.println("\nDone!");
}
Also used : ListTablesResult(com.amazonaws.services.dynamodbv2.model.ListTablesResult) AmazonServiceException(com.amazonaws.AmazonServiceException) ListTablesRequest(com.amazonaws.services.dynamodbv2.model.ListTablesRequest) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Example 29 with AmazonDynamoDB

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

the class MoviesDeleteTable 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");
    try {
        System.out.println("Attempting to delete table; please wait...");
        table.delete();
        table.waitForDelete();
        System.out.print("Success.");
    } catch (Exception e) {
        System.err.println("Unable to delete 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) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Example 30 with AmazonDynamoDB

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

the class MoviesItemOps02 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";
    GetItemSpec spec = new GetItemSpec().withPrimaryKey("year", year, "title", title);
    try {
        System.out.println("Attempting to read the item...");
        Item outcome = table.getItem(spec);
        System.out.println("GetItem succeeded: " + outcome);
    } catch (Exception e) {
        System.err.println("Unable to read item: " + year + " " + title);
        System.err.println(e.getMessage());
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Table(com.amazonaws.services.dynamodbv2.document.Table) GetItemSpec(com.amazonaws.services.dynamodbv2.document.spec.GetItemSpec) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Aggregations

AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)70 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)16 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)14 Test (org.junit.Test)13 Table (com.amazonaws.services.dynamodbv2.document.Table)12 IOException (java.io.IOException)12 AmazonServiceException (com.amazonaws.AmazonServiceException)11 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)11 HashMap (java.util.HashMap)11 AmazonClientException (com.amazonaws.AmazonClientException)10 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)10 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)9 ScanRequest (com.amazonaws.services.dynamodbv2.model.ScanRequest)9 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)8 ToString (lombok.ToString)7 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)6 ArrayList (java.util.ArrayList)6 DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)5 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)5 DescribeTableResult (com.amazonaws.services.dynamodbv2.model.DescribeTableResult)5