Search in sources :

Example 31 with AttributeValue

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

the class UpdateItem method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    UpdateItem <table> <name> <greeting>\n\n" + "Where:\n" + "    table    - the table to put the item in.\n" + "    name     - a name to update in the table. The name must exist,\n" + "               or an error will result.\n" + "Additional fields can be specified by appending them to the end of the\n" + "input.\n\n" + "Examples:\n" + "    UpdateItem SiteColors text default=000000 bold=b22222\n" + "    UpdateItem SiteColors background default=eeeeee code=d3d3d3\n\n";
    if (args.length < 3) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String table_name = args[0];
    String name = args[1];
    ArrayList<String[]> extra_fields = new ArrayList<String[]>();
    // any additional args (fields to add or update)?
    for (int x = 2; x < args.length; x++) {
        String[] fields = args[x].split("=", 2);
        if (fields.length == 2) {
            extra_fields.add(fields);
        } else {
            System.out.format("Invalid argument: %s\n", args[x]);
            System.out.println(USAGE);
            System.exit(1);
        }
    }
    System.out.format("Updating \"%s\" in %s\n", name, table_name);
    if (extra_fields.size() > 0) {
        System.out.println("Additional fields:");
        for (String[] field : extra_fields) {
            System.out.format("  %s: %s\n", field[0], field[1]);
        }
    }
    HashMap<String, AttributeValue> item_key = new HashMap<String, AttributeValue>();
    item_key.put("Name", new AttributeValue(name));
    HashMap<String, AttributeValueUpdate> updated_values = new HashMap<String, AttributeValueUpdate>();
    for (String[] field : extra_fields) {
        updated_values.put(field[0], new AttributeValueUpdate(new AttributeValue(field[1]), AttributeAction.PUT));
    }
    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
    try {
        ddb.updateItem(table_name, item_key, updated_values);
    } catch (ResourceNotFoundException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    } catch (AmazonServiceException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) AttributeValueUpdate(com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) AmazonServiceException(com.amazonaws.AmazonServiceException) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)

Example 32 with AttributeValue

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

the class GetItem method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    GetItem <table> <name> [projection_expression]\n\n" + "Where:\n" + "    table - the table to get an item from.\n" + "    name  - the item to get.\n\n" + "You can add an optional projection expression (a quote-delimited,\n" + "comma-separated list of attributes to retrieve) to limit the\n" + "fields returned from the table.\n\n" + "Example:\n" + "    GetItem HelloTable World\n" + "    GetItem SiteColors text \"default, bold\"\n";
    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String table_name = args[0];
    String name = args[1];
    String projection_expression = null;
    // if a projection expression was included, set it.
    if (args.length == 3) {
        projection_expression = args[2];
    }
    System.out.format("Retrieving item \"%s\" from \"%s\"\n", name, table_name);
    HashMap<String, AttributeValue> key_to_get = new HashMap<String, AttributeValue>();
    key_to_get.put("Name", new AttributeValue(name));
    GetItemRequest request = null;
    if (projection_expression != null) {
        request = new GetItemRequest().withKey(key_to_get).withTableName(table_name).withProjectionExpression(projection_expression);
    } else {
        request = new GetItemRequest().withKey(key_to_get).withTableName(table_name);
    }
    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
    try {
        Map<String, AttributeValue> returned_item = ddb.getItem(request).getItem();
        if (returned_item != null) {
            Set<String> keys = returned_item.keySet();
            for (String key : keys) {
                System.out.format("%s: %s\n", key, returned_item.get(key).toString());
            }
        } else {
            System.out.format("No item found with the key %s!\n", name);
        }
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) HashMap(java.util.HashMap) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) GetItemRequest(com.amazonaws.services.dynamodbv2.model.GetItemRequest)

Aggregations

AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)32 HashMap (java.util.HashMap)23 Test (org.junit.Test)8 Condition (com.amazonaws.services.dynamodbv2.model.Condition)5 AmazonServiceException (com.amazonaws.AmazonServiceException)4 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)4 ConsumedCapacity (com.amazonaws.services.dynamodbv2.model.ConsumedCapacity)4 Map (java.util.Map)4 ExpectedAttributeValue (com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue)3 GetItemRequest (com.amazonaws.services.dynamodbv2.model.GetItemRequest)3 ScanResult (com.amazonaws.services.dynamodbv2.model.ScanResult)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 TicketDefinition (org.apereo.cas.ticket.TicketDefinition)3 DynamoDBScanExpression (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression)2 AttributeValueUpdate (com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate)2 ComparisonOperator (com.amazonaws.services.dynamodbv2.model.ComparisonOperator)2 DeleteItemRequest (com.amazonaws.services.dynamodbv2.model.DeleteItemRequest)2 DeleteItemResult (com.amazonaws.services.dynamodbv2.model.DeleteItemResult)2 KeysAndAttributes (com.amazonaws.services.dynamodbv2.model.KeysAndAttributes)2