.withInt("zipcode", 98052));
}
@Test
public void howToAddElementsToSet() {
Table table = dynamo.getTable(TABLE_NAME);
// Add a set of phone numbers to the attribute "phone"
final String phoneNumber1 = "123-456-7890";
table.updateItem(HASH_KEY, FIRST_CUSTOMER_ID, RANGE_KEY, ADDRESS_TYPE_WORK,
new AttributeUpdate("phone").put(new FluentHashSet<String>(phoneNumber1)));
GetItemOutcome outcome = table.getItemOutcome(new GetItemSpec()
.withPrimaryKey(HASH_KEY, FIRST_CUSTOMER_ID, RANGE_KEY, ADDRESS_TYPE_WORK)
.withConsistentRead(true)
);
Item item = outcome.getItem();
Set<String> phoneNumbers = item.getStringSet("phone");
assertTrue(1 == phoneNumbers.size());
System.out.println(phoneNumbers);
// Add a 2nd phone number to the set
final String phoneNumber2 = "987-654-3210";
table.updateItem(HASH_KEY, FIRST_CUSTOMER_ID, RANGE_KEY, ADDRESS_TYPE_WORK,
new AttributeUpdate("phone").addElements(phoneNumber2));
outcome = table.getItemOutcome(new GetItemSpec()
.withPrimaryKey(HASH_KEY, FIRST_CUSTOMER_ID, RANGE_KEY, ADDRESS_TYPE_WORK)
.withConsistentRead(true));
item = outcome.getItem();
phoneNumbers = item.getStringSet("phone");
System.out.println(phoneNumbers);
assertTrue(2== phoneNumbers.size());
// removes the 2nd phone number from the set
table.updateItem(HASH_KEY, FIRST_CUSTOMER_ID, RANGE_KEY, ADDRESS_TYPE_WORK,
new AttributeUpdate("phone").removeElements(phoneNumber2));
outcome = table.getItemOutcome(new GetItemSpec()
.withPrimaryKey(HASH_KEY, FIRST_CUSTOMER_ID, RANGE_KEY, ADDRESS_TYPE_WORK)
.withConsistentRead(true));
item = outcome.getItem();
phoneNumbers = item.getStringSet("phone");
System.out.println(phoneNumbers);
assertTrue(1 == phoneNumbers.size());
// deletes the phone attribute
table.updateItem(HASH_KEY, FIRST_CUSTOMER_ID, RANGE_KEY, ADDRESS_TYPE_WORK,
new AttributeUpdate("phone").delete());
outcome = table.getItemOutcome(new GetItemSpec()
.withPrimaryKey(HASH_KEY, FIRST_CUSTOMER_ID, RANGE_KEY, ADDRESS_TYPE_WORK)
.withConsistentRead(true));
item = outcome.getItem();
phoneNumbers = item.getStringSet("phone");
assertNull(phoneNumbers);