Search in sources :

Example 51 with AmazonDynamoDB

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

the class AwsItem method get.

@Override
public AttributeValue get(final String attr) throws IOException {
    final String attrib = String.format(Locale.ENGLISH, attr);
    AttributeValue value = this.attributes.get(attrib);
    if (value == null) {
        final AmazonDynamoDB aws = this.credentials.aws();
        try {
            final GetItemRequest request = this.makeItemRequestFor(attrib);
            final long start = System.currentTimeMillis();
            final GetItemResult result = aws.getItem(request);
            value = result.getItem().get(attrib);
            Logger.info(this, // @checkstyle LineLength (1 line)
            "#get('%s'): loaded '%[text]s' from DynamoDB, %s, in %[ms]s", attrib, value, new PrintableConsumedCapacity(result.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
        } catch (final AmazonClientException ex) {
            throw new IOException(String.format("Failed to get \"%s\" from \"%s\" by %s", attr, this.name, this.keys), ex);
        } finally {
            aws.shutdown();
        }
    }
    if (value == null) {
        throw new NoSuchElementException(String.format("attribute \"%s\" not found", attr));
    }
    return value;
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) GetItemResult(com.amazonaws.services.dynamodbv2.model.GetItemResult) AmazonClientException(com.amazonaws.AmazonClientException) ToString(lombok.ToString) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException) GetItemRequest(com.amazonaws.services.dynamodbv2.model.GetItemRequest)

Example 52 with AmazonDynamoDB

use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB 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 53 with AmazonDynamoDB

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

the class AwsIterator method remove.

@Override
@SuppressWarnings("PMD.UseConcurrentHashMap")
public void remove() {
    synchronized (this.dosage) {
        final AmazonDynamoDB aws = this.credentials.aws();
        try {
            final Dosage prev = this.dosage.get();
            final List<Map<String, AttributeValue>> items = new ArrayList<Map<String, AttributeValue>>(prev.items());
            final Map<String, AttributeValue> item = items.remove(this.position);
            final long start = System.currentTimeMillis();
            final DeleteItemResult res = aws.deleteItem(new DeleteItemRequest().withTableName(this.name).withKey(new Attributes(item).only(this.keys)).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL).withExpected(new Attributes(item).only(this.keys).asKeys()));
            this.dosage.set(new AwsIterator.Fixed(prev, items));
            --this.position;
            Logger.info(this, "#remove(): item #%d removed from DynamoDB, %s, in %[ms]s", this.position, new PrintableConsumedCapacity(res.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
        } finally {
            aws.shutdown();
        }
    }
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) DeleteItemRequest(com.amazonaws.services.dynamodbv2.model.DeleteItemRequest) ArrayList(java.util.ArrayList) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) ToString(lombok.ToString) DeleteItemResult(com.amazonaws.services.dynamodbv2.model.DeleteItemResult) Map(java.util.Map)

Example 54 with AmazonDynamoDB

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

the class QueryValve method count.

@Override
public int count(final Credentials credentials, final String table, final Map<String, Condition> conditions) throws IOException {
    final AmazonDynamoDB aws = credentials.aws();
    try {
        QueryRequest request = new QueryRequest().withTableName(table).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL).withKeyConditions(conditions).withConsistentRead(this.consistent).withSelect(Select.COUNT).withLimit(Integer.MAX_VALUE);
        if (!this.index.isEmpty()) {
            request = request.withIndexName(this.index);
        }
        final long start = System.currentTimeMillis();
        final QueryResult rslt = aws.query(request);
        final int count = rslt.getCount();
        Logger.info(this, // @checkstyle LineLength (1 line)
        "#total(): COUNT=%d in '%s' using %s, %s, in %[ms]s", count, request.getTableName(), request.getQueryFilter(), new PrintableConsumedCapacity(rslt.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
        return count;
    } catch (final AmazonClientException ex) {
        throw new IOException(String.format("Failed to count from \"%s\" by %s", table, conditions), ex);
    } finally {
        aws.shutdown();
    }
}
Also used : QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest) AmazonClientException(com.amazonaws.AmazonClientException) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) IOException(java.io.IOException)

Example 55 with AmazonDynamoDB

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

the class AwsTableTest method savesItemToDynamo.

/**
 * AwsTable can save an item.
 * @throws Exception If some problem inside
 */
@Test
public void savesItemToDynamo() throws Exception {
    final Credentials credentials = Mockito.mock(Credentials.class);
    final AmazonDynamoDB aws = Mockito.mock(AmazonDynamoDB.class);
    Mockito.doReturn(aws).when(credentials).aws();
    Mockito.doReturn(new PutItemResult().withConsumedCapacity(new ConsumedCapacity().withCapacityUnits(1.0d))).when(aws).putItem(Mockito.any(PutItemRequest.class));
    Mockito.doReturn(new DescribeTableResult().withTable(new TableDescription().withKeySchema(new KeySchemaElement().withAttributeName(AwsTableTest.KEY)))).when(aws).describeTable(Mockito.any(DescribeTableRequest.class));
    final String attr = "attribute-1";
    final AttributeValue value = new AttributeValue("value-1");
    final String name = "table-name";
    final Table table = new AwsTable(credentials, Mockito.mock(Region.class), name);
    table.put(new Attributes().with(attr, value));
    Mockito.verify(aws).putItem(PutItemRequest.class.cast(MockitoHamcrest.argThat(Matchers.allOf(Matchers.hasProperty(AwsTableTest.TABLE_NAME, Matchers.equalTo(name)), Matchers.hasProperty("item", Matchers.hasEntry(Matchers.equalTo(attr), Matchers.equalTo(value)))))));
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DescribeTableResult(com.amazonaws.services.dynamodbv2.model.DescribeTableResult) DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) TableDescription(com.amazonaws.services.dynamodbv2.model.TableDescription) ConsumedCapacity(com.amazonaws.services.dynamodbv2.model.ConsumedCapacity) PutItemResult(com.amazonaws.services.dynamodbv2.model.PutItemResult) PutItemRequest(com.amazonaws.services.dynamodbv2.model.PutItemRequest) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) Test(org.junit.Test)

Aggregations

AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)70 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)16 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)14 Test (org.junit.Test)13 Table (com.amazonaws.services.dynamodbv2.document.Table)12 IOException (java.io.IOException)12 AmazonServiceException (com.amazonaws.AmazonServiceException)11 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)11 HashMap (java.util.HashMap)11 AmazonClientException (com.amazonaws.AmazonClientException)10 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)10 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)9 ScanRequest (com.amazonaws.services.dynamodbv2.model.ScanRequest)9 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)8 ToString (lombok.ToString)7 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)6 ArrayList (java.util.ArrayList)6 DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)5 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)5 DescribeTableResult (com.amazonaws.services.dynamodbv2.model.DescribeTableResult)5