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();
}
}
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)))))));
}
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)))))));
}
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();
}
}
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);
}
}
Aggregations