Search in sources :

Example 6 with AttributeValueUpdate

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();
    }
}
Also used : UpdateItemRequest(com.amazonaws.services.dynamodbv2.model.UpdateItemRequest) AmazonClientException(com.amazonaws.AmazonClientException) UpdateItemResult(com.amazonaws.services.dynamodbv2.model.UpdateItemResult) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) IOException(java.io.IOException)

Example 7 with AttributeValueUpdate

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));
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) AttributeValueUpdate(com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate) Test(org.junit.Test)

Example 8 with AttributeValueUpdate

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();
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) AttributeValueUpdate(com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate) Test(org.junit.Test)

Example 9 with AttributeValueUpdate

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"));
}
Also used : Item(com.jcabi.dynamo.Item) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) Table(com.jcabi.dynamo.Table) AttributeValueUpdate(com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate) Attributes(com.jcabi.dynamo.Attributes) Region(com.jcabi.dynamo.Region) Test(org.junit.Test)

Example 10 with AttributeValueUpdate

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);
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) UpdateItemRequest(com.amazonaws.services.dynamodbv2.model.UpdateItemRequest) AttributeValueUpdate(com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate) HashMap(java.util.HashMap)

Aggregations

AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)11 AttributeValueUpdate (com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate)11 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)3 Attributes (com.jcabi.dynamo.Attributes)3 Item (com.jcabi.dynamo.Item)3 AmazonClientException (com.amazonaws.AmazonClientException)2 UpdateItemRequest (com.amazonaws.services.dynamodbv2.model.UpdateItemRequest)2 Region (com.jcabi.dynamo.Region)2 Table (com.jcabi.dynamo.Table)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 DefaultAWSCredentialsProviderChain (com.amazonaws.auth.DefaultAWSCredentialsProviderChain)1 AmazonDynamoDBStreams (com.amazonaws.services.dynamodbv2.AmazonDynamoDBStreams)1 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)1 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)1 DescribeStreamRequest (com.amazonaws.services.dynamodbv2.model.DescribeStreamRequest)1 DescribeStreamResult (com.amazonaws.services.dynamodbv2.model.DescribeStreamResult)1