Search in sources :

Example 1 with NameMap

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

Example 2 with NameMap

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

the class DocumentAPIItemCRUDExample method updateMultipleAttributes.

private static void updateMultipleAttributes() {
    Table table = dynamoDB.getTable(tableName);
    try {
        UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("Id", 120).withUpdateExpression("add #a :val1 set #na=:val2").withNameMap(new NameMap().with("#a", "Authors").with("#na", "NewAttribute")).withValueMap(new ValueMap().withStringSet(":val1", "Author YY", "Author ZZ").withString(":val2", "someValue")).withReturnValues(ReturnValue.ALL_NEW);
        UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
        // Check the response.
        System.out.println("Printing item after multiple attribute update...");
        System.out.println(outcome.getItem().toJSONPretty());
    } catch (Exception e) {
        System.err.println("Failed to update multiple attributes in " + tableName);
        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) NameMap(com.amazonaws.services.dynamodbv2.document.utils.NameMap) IOException(java.io.IOException)

Example 3 with NameMap

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

the class DocumentAPIItemCRUDExample method updateExistingAttributeConditionally.

private static void updateExistingAttributeConditionally() {
    Table table = dynamoDB.getTable(tableName);
    try {
        // Specify the desired price (25.00) and also the condition (price =
        // 20.00)
        UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("Id", 120).withReturnValues(ReturnValue.ALL_NEW).withUpdateExpression("set #p = :val1").withConditionExpression("#p = :val2").withNameMap(new NameMap().with("#p", "Price")).withValueMap(new ValueMap().withNumber(":val1", 25).withNumber(":val2", 20));
        UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
        // Check the response.
        System.out.println("Printing item after conditional update to new attribute...");
        System.out.println(outcome.getItem().toJSONPretty());
    } catch (Exception e) {
        System.err.println("Error updating item in " + tableName);
        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) NameMap(com.amazonaws.services.dynamodbv2.document.utils.NameMap) IOException(java.io.IOException)

Example 4 with NameMap

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

Example 5 with NameMap

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

the class DocumentAPIItemCRUDExample method deleteItem.

private static void deleteItem() {
    Table table = dynamoDB.getTable(tableName);
    try {
        DeleteItemSpec deleteItemSpec = new DeleteItemSpec().withPrimaryKey("Id", 120).withConditionExpression("#ip = :val").withNameMap(new NameMap().with("#ip", "InPublication")).withValueMap(new ValueMap().withBoolean(":val", false)).withReturnValues(ReturnValue.ALL_OLD);
        DeleteItemOutcome outcome = table.deleteItem(deleteItemSpec);
        // Check the response.
        System.out.println("Printing item that was deleted...");
        System.out.println(outcome.getItem().toJSONPretty());
    } catch (Exception e) {
        System.err.println("Error deleting item in " + tableName);
        System.err.println(e.getMessage());
    }
}
Also used : DeleteItemOutcome(com.amazonaws.services.dynamodbv2.document.DeleteItemOutcome) DeleteItemSpec(com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec) Table(com.amazonaws.services.dynamodbv2.document.Table) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) NameMap(com.amazonaws.services.dynamodbv2.document.utils.NameMap) IOException(java.io.IOException)

Aggregations

Table (com.amazonaws.services.dynamodbv2.document.Table)6 NameMap (com.amazonaws.services.dynamodbv2.document.utils.NameMap)5 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)5 IOException (java.io.IOException)4 UpdateItemOutcome (com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome)3 UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)3 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)2 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)2 Item (com.amazonaws.services.dynamodbv2.document.Item)2 DeleteItemOutcome (com.amazonaws.services.dynamodbv2.document.DeleteItemOutcome)1 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)1 ScanOutcome (com.amazonaws.services.dynamodbv2.document.ScanOutcome)1 DeleteItemSpec (com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec)1 QuerySpec (com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)1 ScanSpec (com.amazonaws.services.dynamodbv2.document.spec.ScanSpec)1 HashMap (java.util.HashMap)1