use of com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate in project jcabi-dynamo by jcabi.
the class AwsItem method put.
@Override
public Map<String, AttributeValue> put(final Map<String, AttributeValueUpdate> attrs) throws IOException {
final AmazonDynamoDB aws = this.credentials.aws();
final Attributes expected = this.attributes.only(this.keys);
try {
final UpdateItemRequest request = new UpdateItemRequest().withTableName(this.name).withExpected(expected.asKeys()).withKey(expected).withAttributeUpdates(attrs).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL).withReturnValues(ReturnValue.UPDATED_NEW);
final long start = System.currentTimeMillis();
final UpdateItemResult result = aws.updateItem(request);
Logger.info(this, "#put('%s'): updated item to DynamoDB, %s, in %[ms]s", attrs, new PrintableConsumedCapacity(result.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
return result.getAttributes();
} catch (final AmazonClientException ex) {
throw new IOException(String.format("Failed to put %s into \"%s\" with %s", attrs, this.name, this.keys), ex);
} finally {
aws.shutdown();
}
}
use of com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate in project jcabi-dynamo by jcabi.
the class AttributeUpdatesTest method containsValue.
/**
* AttributesUpdates can tell if it contains a value.
*/
@Test
public void containsValue() {
final String value = "attrv";
final AttributeUpdates attr = new AttributeUpdates().with("attrkey", value).with("otherkey", "othervalue");
MatcherAssert.assertThat(attr.containsValue(new AttributeValueUpdate(new AttributeValue(value), AttributeAction.PUT)), Matchers.is(Boolean.TRUE));
}
use of com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate in project jcabi-dynamo by jcabi.
the class RegionITCase method worksWithAmazon.
/**
* Region.Simple can work with AWS.
* @throws Exception If some problem inside
*/
@Test
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public void worksWithAmazon() throws Exception {
final String name = RandomStringUtils.randomAlphabetic(Tv.EIGHT);
final RegionMock mock = new RegionMock();
final Table tbl = mock.get(name).table(name);
final String attr = RandomStringUtils.randomAlphabetic(Tv.EIGHT);
final String value = RandomStringUtils.randomAlphanumeric(Tv.TEN);
final String hash = RandomStringUtils.randomAlphanumeric(Tv.TEN);
for (int idx = 0; idx < Tv.FIVE; ++idx) {
tbl.put(new Attributes().with(mock.hash(), hash).with(mock.range(), idx).with(attr, value));
}
MatcherAssert.assertThat(tbl.frame().where(mock.hash(), Conditions.equalTo(hash)).through(new QueryValve().withLimit(1)), Matchers.hasSize(Tv.FIVE));
final Frame frame = tbl.frame().where(attr, Conditions.equalTo(value)).through(new ScanValve().withLimit(Tv.TEN).withAttributeToGet(attr));
MatcherAssert.assertThat(frame, Matchers.hasSize(Tv.FIVE));
final Iterator<Item> items = frame.iterator();
final Item item = items.next();
final int range = Integer.parseInt(item.get(mock.range()).getN());
MatcherAssert.assertThat(item.get(attr).getS(), Matchers.equalTo(value));
item.put(attr, new AttributeValueUpdate(new AttributeValue("empty"), AttributeAction.PUT));
MatcherAssert.assertThat(tbl.frame().where(mock.hash(), hash).where(mock.range(), Conditions.equalTo(range)).through(new ScanValve()).iterator().next().get(attr).getS(), Matchers.not(Matchers.equalTo(value)));
items.remove();
}
use of com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate in project jcabi-dynamo by jcabi.
the class MkRegionTest method storesAndReadsSingleAttribute.
/**
* MkRegion can store and read items.
* @throws Exception If some problem inside
*/
@Test
public void storesAndReadsSingleAttribute() throws Exception {
final String table = "ideas";
final String key = "number";
final String attr = "total";
final Region region = new MkRegion(new H2Data().with(table, new String[] { key }, attr));
final Table tbl = region.table(table);
tbl.put(new Attributes().with(key, "32443").with(attr, "0"));
final Item item = tbl.frame().iterator().next();
item.put(attr, new AttributeValueUpdate().withValue(new AttributeValue().withN("2")).withAction(AttributeAction.PUT));
MatcherAssert.assertThat(item.get(attr).getN(), Matchers.equalTo("2"));
}
use of com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate in project aws-doc-sdk-examples by awsdocs.
the class StreamsAdapterDemoHelper method updateItem.
public static void updateItem(AmazonDynamoDB dynamoDBClient, String tableName, String id, String val) {
java.util.Map<String, AttributeValue> key = new HashMap<String, AttributeValue>();
key.put("Id", new AttributeValue().withN(id));
Map<String, AttributeValueUpdate> attributeUpdates = new HashMap<String, AttributeValueUpdate>();
AttributeValueUpdate update = new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(new AttributeValue().withS(val));
attributeUpdates.put("attribute-2", update);
UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName(tableName).withKey(key).withAttributeUpdates(attributeUpdates);
dynamoDBClient.updateItem(updateItemRequest);
}
Aggregations