Search in sources :

Example 91 with Table

use of com.amazonaws.services.dynamodbv2.document.Table 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 92 with Table

use of com.amazonaws.services.dynamodbv2.document.Table 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)

Example 93 with Table

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

the class AwsTableTest method deletesItemFromDynamo.

/**
 * AwsTable can delete an item.
 * @throws Exception If some problem inside
 */
@Test
public void deletesItemFromDynamo() 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 DeleteItemResult().withConsumedCapacity(new ConsumedCapacity().withCapacityUnits(1.0d))).when(aws).deleteItem(Mockito.any(DeleteItemRequest.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-2";
    final AttributeValue value = new AttributeValue("value-2");
    final String name = "table-name-2";
    final Table table = new AwsTable(credentials, Mockito.mock(Region.class), name);
    table.delete(new Attributes().with(attr, value));
    Mockito.verify(aws).deleteItem(DeleteItemRequest.class.cast(MockitoHamcrest.argThat(Matchers.allOf(Matchers.hasProperty(AwsTableTest.TABLE_NAME, Matchers.equalTo(name)), Matchers.hasProperty(AwsTableTest.KEY, Matchers.hasEntry(Matchers.equalTo(attr), Matchers.equalTo(value)))))));
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) DeleteItemRequest(com.amazonaws.services.dynamodbv2.model.DeleteItemRequest) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DeleteItemResult(com.amazonaws.services.dynamodbv2.model.DeleteItemResult) 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) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) Test(org.junit.Test)

Example 94 with Table

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

the class ScanValve method fetch.

// @checkstyle ParameterNumber (5 lines)
@Override
public Dosage fetch(final Credentials credentials, final String table, final Map<String, Condition> conditions, final Collection<String> keys) throws IOException {
    final AmazonDynamoDB aws = credentials.aws();
    try {
        final Collection<String> attrs = new HashSet<String>(Arrays.asList(this.attributes));
        attrs.addAll(keys);
        final ScanRequest request = new ScanRequest().withTableName(table).withAttributesToGet(attrs).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL).withScanFilter(conditions).withLimit(this.limit);
        final long start = System.currentTimeMillis();
        final ScanResult result = aws.scan(request);
        Logger.info(this, // @checkstyle LineLength (1 line)
        "#items(): loaded %d item(s) from '%s' and stooped at %s, using %s, %s, in %[ms]s", result.getCount(), table, result.getLastEvaluatedKey(), conditions, new PrintableConsumedCapacity(result.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
        return new ScanValve.NextDosage(credentials, request, result);
    } catch (final AmazonClientException ex) {
        throw new IOException(String.format("Failed to fetch from \"%s\" by %s and %s", table, conditions, keys), ex);
    } finally {
        aws.shutdown();
    }
}
Also used : ScanRequest(com.amazonaws.services.dynamodbv2.model.ScanRequest) ScanResult(com.amazonaws.services.dynamodbv2.model.ScanResult) AmazonClientException(com.amazonaws.AmazonClientException) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) ToString(lombok.ToString) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 95 with Table

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

the class H2Data method iterate.

@Override
public Iterable<Attributes> iterate(final String table, final Conditions conds) throws IOException {
    try {
        final StringBuilder sql = new StringBuilder("SELECT * FROM ").append(H2Data.encodeTableName(table));
        if (!conds.isEmpty()) {
            sql.append(" WHERE ");
            Joiner.on(H2Data.AND).appendTo(sql, Iterables.transform(conds.keySet(), H2Data.WHERE));
        }
        JdbcSession session = new JdbcSession(this.connection()).sql(sql.toString());
        for (final Condition cond : conds.values()) {
            if (cond.getAttributeValueList().size() != 1) {
                throw new UnsupportedOperationException("at the moment only one value of condition is supported");
            }
            if (!cond.getComparisonOperator().equals(ComparisonOperator.EQ.toString())) {
                throw new UnsupportedOperationException(String.format("at the moment only EQ operator is supported: %s", cond.getComparisonOperator()));
            }
            final AttributeValue val = cond.getAttributeValueList().get(0);
            session = session.set(H2Data.value(val));
        }
        return session.select(H2Data.OUTCOME);
    } catch (final SQLException ex) {
        throw new IOException(ex);
    }
}
Also used : JdbcSession(com.jcabi.jdbc.JdbcSession) Condition(com.amazonaws.services.dynamodbv2.model.Condition) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Aggregations

Table (com.amazonaws.services.dynamodbv2.document.Table)63 Item (com.amazonaws.services.dynamodbv2.document.Item)40 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)38 AmazonServiceException (com.amazonaws.AmazonServiceException)35 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)31 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)28 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)28 IOException (java.io.IOException)24 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)23 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)23 ResourceNotFoundException (com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)23 HashMap (java.util.HashMap)23 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)22 TableDescription (com.amazonaws.services.dynamodbv2.model.TableDescription)22 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)17 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)17 ArrayList (java.util.ArrayList)17 AmazonClientException (com.amazonaws.AmazonClientException)14 Test (org.junit.Test)14 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)10