@Override public Void create(Group group) { Item item = preparePutItem(group); PutItemSpec putItemSpec = new PutItemSpec() .withItem(item) .withConditionExpression("attribute_not_exists(#ns_key)") .withNameMap(new NameMap().with("#ns_key", HASH_KEY)); Table table = dynamoDB.getTable(groupTableName); final Supplier<PutItemOutcome> putItemOutcomeSupplier = () -> { try { return table.putItem(putItemSpec); } catch (ConditionalCheckFailedException e) { throwConflictAlreadyExists(group); return null; } }; return putItem(group, putItemOutcomeSupplier); }
private boolean tryAddMissingPartition(String dyanmoDBTaableName,DynamoDB dynamoDBClient, Partition partition){ Table ddbTable= dynamoDBClient.getTable(dyanmoDBTaableName); Item item=new Item() .withPrimaryKey("PartitionSpec",partition.spec()) .withString("PartitionPath",partition.path()) .withString("PartitionName", partition.name()); PutItemSpec itemSpec=new PutItemSpec() .withItem(item) .withConditionExpression("attribute_not_exists(#ps)") .withNameMap(new NameMap() .with("#ps","PartitionSpec")); try{ ddbTable.putItem(itemSpec); System.out.println("Item was added to the table.PartitionSpec="+partition.spec()+"; Path="+partition.path()); return true; } catch(ConditionalCheckFailedException e){ System.out.println(e.toString()); System.out.println("Item already exists. PartitionSpec="+partition.spec()+"; Path="+partition.path()); return false; } }
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"); 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)); ItemCollection<ScanOutcome> items = table.scan(scanSpec); Iterator<Item> iter = items.iterator(); while (iter.hasNext()) { Item item = iter.next(); System.out.println(item.toString()); } }
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()); } }
@Override public Void createFeature(Feature feature) { final String key = feature.getKey(); final String group = feature.getGroup(); final Item item = preparePutItem(feature); final PutItemSpec putItemSpec = new PutItemSpec() .withItem(item) .withConditionExpression("attribute_not_exists(#featurekey)") .withNameMap(new NameMap().with("#featurekey", RANGE_KEY)); final Supplier<PutItemOutcome> putItemOutcomeSupplier = () -> { try { return dynamoDB.getTable(featureTableName).putItem(putItemSpec); } catch (ConditionalCheckFailedException e) { logger.error("err=conflict_feature_already_exists feature_key={} {}", feature.getKey(), e.getMessage()); throwConflictAlreadyExists(feature); return null; } }; final DynamoDbCommand<PutItemOutcome> cmd = new DynamoDbCommand<>("createFeature", putItemOutcomeSupplier, () -> { throw new RuntimeException("createFeature"); }, hystrixWriteConfiguration, metrics); final PutItemOutcome outcome = cmd.execute(); logger.info("{} /dynamodb_put_item_result=[{}]", kvp("op", "createFeature", HASH_KEY, group, RANGE_KEY, key, "result", "ok"), outcome.getPutItemResult().toString()); return null; }
@Override public void announce(long stateVersion) { Table table = dynamoDB.getTable(tableName); UpdateItemSpec updateItemSpec = new UpdateItemSpec() .withPrimaryKey("namespace", blobNamespace) .withUpdateExpression("set #version = :ver") .withNameMap(new NameMap().with("#version", "version")) .withValueMap(new ValueMap().withNumber(":ver", stateVersion)); table.updateItem(updateItemSpec); }
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"); } }
private static void updateAddNewAttribute() { Table table = dynamoDB.getTable(tableName); try { Map<String, String> expressionAttributeNames = new HashMap<String, String>(); expressionAttributeNames.put("#na", "NewAttribute"); UpdateItemSpec updateItemSpec = new UpdateItemSpec() .withPrimaryKey("Id", 121) .withUpdateExpression("set #na = :val1") .withNameMap(new NameMap() .with("#na", "NewAttribute")) .withValueMap(new ValueMap() .withString(":val1", "Some value")) .withReturnValues(ReturnValue.ALL_NEW); UpdateItemOutcome outcome = table.updateItem(updateItemSpec); // Check the response. System.out.println("Printing item after adding new attribute..."); System.out.println(outcome.getItem().toJSONPretty()); } catch (Exception e) { System.err.println("Failed to add new attribute in " + tableName); System.err.println(e.getMessage()); } }
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()); } }
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()); } }
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"); 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(new NameMap().with("#yr", "year")) .withValueMap(valueMap); ItemCollection<QueryOutcome> items = table.query(querySpec); Iterator<Item> iterator = items.iterator(); Item item = null; System.out.println("Movies from 1985"); while (iterator.hasNext()) { item = iterator.next(); System.out.println(item.getNumber("year") + ": " + item.getString("title")); } 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); items = table.query(querySpec); iterator = items.iterator(); System.out.println("Movies from 1992 - titles A-L, with genres and lead actor"); while (iterator.hasNext()) { item = iterator.next(); System.out.println(item.toString()); } }