List of usage examples for com.amazonaws.services.dynamodbv2.model AttributeAction ADD
AttributeAction ADD
To view the source code for com.amazonaws.services.dynamodbv2.model AttributeAction ADD.
Click Source Link
From source file:com.lvl6.mobsters.dynamo.setup.TransactionExamples.java
License:Open Source License
/** * This example shows an example of how to handle errors *///w w w . j a va 2s . co m public void badRequest() throws RuntimeException { print("\n*** badRequest() ***\n"); // Create a "success" flag and set it to false. We'll roll back the transaction in a finally {} if this wasn't set to true by then. boolean success = false; Transaction t1 = txManager.newTransaction(); try { // Construct a request that we know DynamoDB will reject. Map<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put(EXAMPLE_TABLE_HASH_KEY, new AttributeValue("Item1")); // You cannot "add" a String type attribute. This request will be rejected by DynamoDB. Map<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>(); updates.put("Will this work?", new AttributeValueUpdate().withAction(AttributeAction.ADD) .withValue(new AttributeValue("Nope."))); // The transaction library will make the request here, so we actually see print("Making invalid request"); t1.updateItem(new UpdateItemRequest().withTableName(EXAMPLE_TABLE_NAME).withKey(key) .withAttributeUpdates(updates)); t1.commit(); success = true; throw new RuntimeException("This should NOT have happened (actually should have failed before commit)"); } catch (AmazonServiceException e) { print("Caught a ValidationException. This is what we expected. The transaction will be rolled back: " + e.getMessage()); // in a real application, you'll probably want to throw an exception to your caller } finally { if (!success) { print("The transaction didn't work, as expected. Rolling back."); // It can be a good idea to use a "success" flag in this way, to ensure that you roll back if you get any exceptions // from the transaction library, or from DynamoDB, or any from the DynamoDB client library. These 3 exception base classes are: // TransactionException, AmazonServiceException, or AmazonClientExeption. // If you forget to roll back, no problem - another transaction will come along and roll yours back eventually. try { t1.rollback(); } catch (TransactionException te) { } // ignore, but should probably log } try { t1.delete(); } catch (TransactionException te) { } // ignore, but should probably log } }
From source file:com.nandanu.halomama.controller.DynamoDBRouter.java
License:Open Source License
public void incrementSeen(String userNameTwitter, String createdDate) { UpdateItemRequest upd = new UpdateItemRequest(); upd.setTableName(Constants.TABLE_NAME); AttributeValue unt = new AttributeValue(); unt.setS(userNameTwitter);/* ww w .jav a 2 s.c om*/ AttributeValue cd = new AttributeValue(); cd.setS(createdDate); upd.addKeyEntry(Constants.TAG_USERNAME, unt); upd.addKeyEntry(Constants.TAG_CREATED_DATE, cd); AttributeValue s = new AttributeValue(); s.setN("1"); AttributeValueUpdate seen = new AttributeValueUpdate(s, AttributeAction.ADD); upd.addAttributeUpdatesEntry(Constants.TAG_SEEN, seen); dDBClient.updateItem(upd); }