public static void main(String[] args) { AmazonDynamoDBClient client = new AmazonDynamoDBClient(); client.setEndpoint("http://localhost:8000"); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("Movies"); // Conditional delete (will fail) DeleteItemSpec deleteItemSpec = new DeleteItemSpec() .withPrimaryKey(new PrimaryKey("year", 2015, "title", "The Big New Movie")) .withConditionExpression("info.rating <= :val") .withValueMap(new ValueMap() .withNumber(":val", 5.0)); System.out.println("Attempting a conditional delete..."); try { table.deleteItem(deleteItemSpec); System.out.println("DeleteItem succeeded"); } catch (Exception e) { e.printStackTrace(); System.out.println("DeleteItem failed"); } }
@Override public Void removeRelation(Group group, String relationHashKey, String relationRangeKey) { Table table = dynamoDB.getTable(groupGraphTableName); final PrimaryKey key = new PrimaryKey( GroupStorage.SUBJECT_KEY, relationHashKey, GroupStorage.OBJECT_RELATION_KEY, relationRangeKey ); DynamoDbCommand<DeleteItemOutcome> cmd = new DynamoDbCommand<>("removeRelation", () -> table.deleteItem(key), () -> { throw new RuntimeException("removeRelation"); }, dynamodbGraphWriteHystrix, metrics); final DeleteItemOutcome deleteItemOutcome = cmd.execute(); logger.info("{} /dynamodb_remove_item_result=[{}]", kvp("op", "removeRelation", "appkey", group.getKey(), "hash_key", relationHashKey, "range_key", relationRangeKey, "result", "ok"), deleteItemOutcome.getDeleteItemResult().toString()); return null; }
boolean updateItem(String key, long currentTimeMillis, int expiredIntervalMillis, Context context) { AmazonDynamoDB client = createDynamoDBClient(cc); String functionName = context.getFunctionName(); try { long sec = currentTimeMillis - expiredIntervalMillis; DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable(TABLE_NAME); Map<String, String> expressionAttributeNames = new HashMap<>(); expressionAttributeNames.put("#created_time", COL_CREATED_TIME); Map<String, Object> expressionAttributeValues = new HashMap<>(); expressionAttributeValues.put(":now", currentTimeMillis); expressionAttributeValues.put(":expired", sec); table.updateItem(new PrimaryKey(COL_FUNCTION_NAME, functionName, COL_KEY, key), "set #created_time = :now", // UpdateExpression "#created_time < :expired", // ConditionExpression expressionAttributeNames, expressionAttributeValues); return true; } catch (ConditionalCheckFailedException e) { return false; } finally { client.shutdown(); } }
private PrimaryKey toPrimaryKey(IndexKey key) { PrimaryKey pk = new PrimaryKey() .addComponent(_hkName, key.getHashKey()); if ( null != _rkName ) { pk.addComponent(_rkName, key.getRangeKey()); } return pk; }
@Override public String handleRequest(Book request, Context context) { Item outcome = DynamoDBUtil.getTable().getItem(new PrimaryKey("id", request.getId())); if (null != outcome) { return outcome.toJSONPretty(); } return null; }
public static void main(String[] args) { AmazonDynamoDBClient client = new AmazonDynamoDBClient(); client.setEndpoint("http://localhost:8000"); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("Movies"); int year = 2015; String title = "The Big New Movie"; final Map<String, Object> infoMap = new HashMap<String, Object>(); infoMap.put("plot", "Nothing happens at all."); infoMap.put("rating", 0.0); Item item = new Item() .withPrimaryKey(new PrimaryKey("year", year, "title", title)) .withMap("info", infoMap); // Attempt a conditional write. We expect this to fail. PutItemSpec putItemSpec = new PutItemSpec() .withItem(item) .withConditionExpression("attribute_not_exists(#yr) and attribute_not_exists(title)") .withNameMap(new NameMap() .with("#yr", "year")); System.out.println("Attempting a conditional write..."); try { table.putItem(putItemSpec); System.out.println("PutItem succeeded: " + table.getItem("year", year, "title", title).toJSONPretty()); } catch (ConditionalCheckFailedException e) { e.printStackTrace(System.err); System.out.println("PutItem failed"); } }
public static void main(String[] args) { AmazonDynamoDBClient client = new AmazonDynamoDBClient(); client.setEndpoint("http://localhost:8000"); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("Movies"); int year = 2015; String title = "The Big New Movie"; // Conditional update (will fail) UpdateItemSpec updateItemSpec = new UpdateItemSpec() .withPrimaryKey(new PrimaryKey("year", 2015, "title", "The Big New Movie")) .withUpdateExpression("remove info.actors[0]") .withConditionExpression("size(info.actors) > :num") .withValueMap(new ValueMap().withNumber(":num", 3)); System.out.println("Attempting a conditional update..."); try { table.updateItem(updateItemSpec); System.out.println("UpdateItem succeeded: " + table.getItem("year", year, "title", title).toJSONPretty()); } catch (ConditionalCheckFailedException e) { e.printStackTrace(); System.out.println("UpdateItem failed"); } }
@Override public void truncate() { getAll() .parallelStream() .forEach(p -> table.deleteItem(new PrimaryKey("id", p.getId()))); }
@Override public void delete(final String id) { table.deleteItem(new PrimaryKey("id", id)); }
protected PrimaryKey createEventId(ILoggingEvent event) { return new PrimaryKey(primaryKey, UUID.randomUUID().toString()); }
@Override public String handleRequest(Book request, Context context) { DeleteItemOutcome outcome = DynamoDBUtil.getTable().deleteItem(new PrimaryKey("id", request.getId())); String result = "Item deleted: " + outcome.getDeleteItemResult().getSdkHttpMetadata().getHttpStatusCode(); return result; }
private void setPrimaryKey(FilterLeaf leaf, PrimaryKey pk) { }