Search in sources :

Example 51 with Table

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

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

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

Example 54 with Table

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

the class MoviesItemOps06 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";
    DeleteItemSpec deleteItemSpec = new DeleteItemSpec().withPrimaryKey(new PrimaryKey("year", year, "title", title)).withConditionExpression("info.rating <= :val").withValueMap(new ValueMap().withNumber(":val", 5.0));
    try {
        System.out.println("Attempting a conditional delete...");
        table.deleteItem(deleteItemSpec);
        System.out.println("DeleteItem succeeded");
    } catch (Exception e) {
        System.err.println("Unable to delete item: " + year + " " + title);
        System.err.println(e.getMessage());
    }
}
Also used : DeleteItemSpec(com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec) Table(com.amazonaws.services.dynamodbv2.document.Table) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) 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 55 with Table

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

the class MoviesQuery 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");
    HashMap<String, String> nameMap = new HashMap<String, String>();
    nameMap.put("#yr", "year");
    HashMap<String, Object> valueMap = new HashMap<String, Object>();
    valueMap.put(":yyyy", 1985);
    QuerySpec querySpec = new QuerySpec().withKeyConditionExpression("#yr = :yyyy").withNameMap(nameMap).withValueMap(valueMap);
    ItemCollection<QueryOutcome> items = null;
    Iterator<Item> iterator = null;
    Item item = null;
    try {
        System.out.println("Movies from 1985");
        items = table.query(querySpec);
        iterator = items.iterator();
        while (iterator.hasNext()) {
            item = iterator.next();
            System.out.println(item.getNumber("year") + ": " + item.getString("title"));
        }
    } catch (Exception e) {
        System.err.println("Unable to query movies from 1985");
        System.err.println(e.getMessage());
    }
    valueMap.put(":yyyy", 1992);
    valueMap.put(":letter1", "A");
    valueMap.put(":letter2", "L");
    querySpec.withProjectionExpression("#yr, title, info.genres, info.actors[0]").withKeyConditionExpression("#yr = :yyyy and title between :letter1 and :letter2").withNameMap(nameMap).withValueMap(valueMap);
    try {
        System.out.println("Movies from 1992 - titles A-L, with genres and lead actor");
        items = table.query(querySpec);
        iterator = items.iterator();
        while (iterator.hasNext()) {
            item = iterator.next();
            System.out.println(item.getNumber("year") + ": " + item.getString("title") + " " + item.getMap("info"));
        }
    } catch (Exception e) {
        System.err.println("Unable to query movies from 1992:");
        System.err.println(e.getMessage());
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) HashMap(java.util.HashMap) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) QueryOutcome(com.amazonaws.services.dynamodbv2.document.QueryOutcome) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) Item(com.amazonaws.services.dynamodbv2.document.Item) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)

Aggregations

Table (com.amazonaws.services.dynamodbv2.document.Table)63 Item (com.amazonaws.services.dynamodbv2.document.Item)40 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)38 AmazonServiceException (com.amazonaws.AmazonServiceException)35 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)31 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)28 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)28 IOException (java.io.IOException)24 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)23 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)23 ResourceNotFoundException (com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)23 HashMap (java.util.HashMap)23 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)22 TableDescription (com.amazonaws.services.dynamodbv2.model.TableDescription)22 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)17 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)17 ArrayList (java.util.ArrayList)17 AmazonClientException (com.amazonaws.AmazonClientException)14 Test (org.junit.Test)14 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)10