Search in sources :

Example 1 with AttributeValueUpdate

use of com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate in project wring by yegor256.

the class DyEvents method post.

@Override
public void post(final String title, final String text) throws IOException {
    final Iterator<Item> items = this.items(title);
    if (items.hasNext()) {
        final Item item = items.next();
        item.put(new AttributeUpdates().with("rank", new AttributeValueUpdate().withValue(new AttributeValue().withN("1")).withAction(AttributeAction.ADD)).with("text", new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(new AttributeValue().withS(DyEvents.concat(item.get("text").getS(), text)))));
        Logger.info(this, "Event updated for %s: \"%s\"", this.urn, title);
    } else {
        final int rank;
        if (title.startsWith("io.wring.agents.")) {
            rank = -Tv.THOUSAND;
        } else {
            rank = 1;
        }
        this.table().put(new Attributes().with("urn", this.urn).with("title", title).with("text", text).with("rank", rank).with("time", System.currentTimeMillis()));
        Logger.info(this, "Event created for %s: \"%s\"", this.urn, title);
    }
}
Also used : Item(com.jcabi.dynamo.Item) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) AttributeValueUpdate(com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate) Attributes(com.jcabi.dynamo.Attributes) AttributeUpdates(com.jcabi.dynamo.AttributeUpdates)

Example 2 with AttributeValueUpdate

use of com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate in project jcabi-dynamo by jcabi.

the class H2Data method update.

@Override
public void update(final String table, final Attributes keys, final AttributeUpdates attrs) throws IOException {
    try {
        JdbcSession session = new JdbcSession(this.connection());
        for (final AttributeValueUpdate value : attrs.values()) {
            session = session.set(H2Data.value(value.getValue()));
        }
        for (final AttributeValue value : keys.values()) {
            session = session.set(H2Data.value(value));
        }
        session.sql(String.format("UPDATE %s SET %s WHERE %s", H2Data.encodeTableName(table), Joiner.on(',').join(Iterables.transform(attrs.keySet(), H2Data.WHERE)), Joiner.on(H2Data.AND).join(Iterables.transform(keys.keySet(), H2Data.WHERE))));
        session.execute();
    } catch (final SQLException ex) {
        throw new IOException(ex);
    }
}
Also used : JdbcSession(com.jcabi.jdbc.JdbcSession) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) AttributeValueUpdate(com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 3 with AttributeValueUpdate

use of com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate in project jcabi-dynamo by jcabi.

the class MkRegionTest method storesAndReadsAttributes.

/**
 * MkRegion can store and read items.
 * @throws Exception If some problem inside
 */
@Test
public void storesAndReadsAttributes() throws Exception {
    final String name = "users";
    final String key = "id";
    final String attr = "description";
    final String nattr = "thenumber";
    final Region region = new MkRegion(new H2Data().with(name, new String[] { key }, attr, nattr));
    final Table table = region.table(name);
    table.put(new Attributes().with(key, "32443").with(attr, "first value to \n\t€ save").with(nattr, "150"));
    final Item item = table.frame().iterator().next();
    MatcherAssert.assertThat(item.has(attr), Matchers.is(true));
    MatcherAssert.assertThat(item.get(attr).getS(), Matchers.containsString("\n\t\u20ac save"));
    item.put(attr, new AttributeValueUpdate().withValue(new AttributeValue("this is another value")));
    MatcherAssert.assertThat(item.get(attr).getS(), Matchers.containsString("another value"));
    MatcherAssert.assertThat(item.get(nattr).getN(), Matchers.endsWith("50"));
}
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 4 with AttributeValueUpdate

use of com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate in project camel by apache.

the class UpdateItemCommandTest method execute.

@Test
public void execute() {
    Map<String, AttributeValue> key = new HashMap<String, AttributeValue>();
    key.put("1", new AttributeValue("Key_1"));
    exchange.getIn().setHeader(DdbConstants.KEY, key);
    Map<String, AttributeValueUpdate> attributeMap = new HashMap<String, AttributeValueUpdate>();
    AttributeValueUpdate attributeValue = new AttributeValueUpdate(new AttributeValue("new value"), AttributeAction.ADD);
    attributeMap.put("name", attributeValue);
    exchange.getIn().setHeader(DdbConstants.UPDATE_VALUES, attributeMap);
    Map<String, ExpectedAttributeValue> expectedAttributeValueMap = new HashMap<String, ExpectedAttributeValue>();
    expectedAttributeValueMap.put("name", new ExpectedAttributeValue(new AttributeValue("expected value")));
    exchange.getIn().setHeader(DdbConstants.UPDATE_CONDITION, expectedAttributeValueMap);
    exchange.getIn().setHeader(DdbConstants.RETURN_VALUES, "ALL_OLD");
    command.execute();
    assertEquals("DOMAIN1", ddbClient.updateItemRequest.getTableName());
    assertEquals(attributeMap, ddbClient.updateItemRequest.getAttributeUpdates());
    assertEquals(key, ddbClient.updateItemRequest.getKey());
    assertEquals(expectedAttributeValueMap, ddbClient.updateItemRequest.getExpected());
    assertEquals("ALL_OLD", ddbClient.updateItemRequest.getReturnValues());
    assertEquals(new AttributeValue("attrValue"), exchange.getIn().getHeader(DdbConstants.ATTRIBUTES, Map.class).get("attrName"));
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) ExpectedAttributeValue(com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue) AttributeValueUpdate(com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate) HashMap(java.util.HashMap) ExpectedAttributeValue(com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue) Test(org.junit.Test)

Example 5 with AttributeValueUpdate

use of com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate in project wildfly-camel by wildfly-extras.

the class DynamoDBUtils method updItem.

public static void updItem(CamelContext camelctx, String title) {
    HashMap<String, AttributeValue> key = new HashMap<>();
    key.put("Id", new AttributeValue().withN("103"));
    HashMap<String, AttributeValueUpdate> updItem = new HashMap<>();
    AttributeValueUpdate updValue = new AttributeValueUpdate();
    updValue.setValue(new AttributeValue().withS(title));
    updItem.put("Title", updValue);
    Exchange exchange = new ExchangeBuilder(camelctx).withHeader(DdbConstants.OPERATION, DdbOperations.UpdateItem).withHeader(DdbConstants.KEY, key).withHeader(DdbConstants.UPDATE_VALUES, updItem).build();
    ProducerTemplate producer = camelctx.createProducerTemplate();
    producer.send("direct:start", exchange);
    Assert.assertNull(exchange.getException());
}
Also used : Exchange(org.apache.camel.Exchange) ProducerTemplate(org.apache.camel.ProducerTemplate) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) AttributeValueUpdate(com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate) HashMap(java.util.HashMap) ExchangeBuilder(org.apache.camel.builder.ExchangeBuilder)

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