use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project jcabi-dynamo by jcabi.
the class QueryValve 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);
QueryRequest request = new QueryRequest().withTableName(table).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL).withKeyConditions(conditions).withConsistentRead(this.consistent).withScanIndexForward(this.forward).withSelect(this.select).withLimit(this.limit);
if (this.select.equals(Select.SPECIFIC_ATTRIBUTES.toString())) {
request = request.withAttributesToGet(attrs);
}
if (!this.index.isEmpty()) {
request = request.withIndexName(this.index);
}
final long start = System.currentTimeMillis();
final QueryResult result = aws.query(request);
Logger.info(this, // @checkstyle LineLength (1 line)
"#items(): loaded %d item(s) from '%s' and stopped at %s, using %s, %s, in %[ms]s", result.getCount(), table, result.getLastEvaluatedKey(), conditions, new PrintableConsumedCapacity(result.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
return new QueryValve.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.AmazonDynamoDB in project jcabi-dynamo by jcabi.
the class ThroughputTest method adjustsThroughput.
/**
* Throughput can change throughput of a table.
* @throws Exception If something went wrong
*/
@Test
public final void adjustsThroughput() throws Exception {
final Table table = Mockito.mock(Table.class);
final Region region = Mockito.mock(Region.class);
final AmazonDynamoDB aws = Mockito.mock(AmazonDynamoDB.class);
Mockito.when(table.region()).thenReturn(region);
final String name = "Customers";
Mockito.when(table.name()).thenReturn(name);
Mockito.when(region.aws()).thenReturn(aws);
new Throughput(table).adjust();
Mockito.verify(aws, Mockito.times(1)).updateTable(Mockito.eq(name), Mockito.<ProvisionedThroughput>any());
}
use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project jcabi-dynamo by jcabi.
the class ScanValveTest method fetchesData.
/**
* ScanValve can fetch data from AWS.
* @throws Exception If some problem inside
*/
@Test
@SuppressWarnings("unchecked")
public void fetchesData() throws Exception {
final Valve valve = new ScanValve();
final Credentials credentials = Mockito.mock(Credentials.class);
final ImmutableMap<String, AttributeValue> item = new ImmutableMap.Builder<String, AttributeValue>().build();
final AmazonDynamoDB aws = Mockito.mock(AmazonDynamoDB.class);
Mockito.doReturn(aws).when(credentials).aws();
Mockito.doReturn(new ScanResult().withItems(Collections.<Map<String, AttributeValue>>singletonList(item)).withConsumedCapacity(new ConsumedCapacity().withCapacityUnits(1d))).when(aws).scan(Mockito.any(ScanRequest.class));
final Dosage dosage = valve.fetch(credentials, "table", new Conditions(), new ArrayList<String>(0));
MatcherAssert.assertThat(dosage.hasNext(), Matchers.is(false));
MatcherAssert.assertThat(dosage.items(), Matchers.hasItem(item));
}
use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project jcabi-dynamo by jcabi.
the class QueryValveTest method fetchesData.
/**
* QueryValve can fetch data from AWS.
* @throws Exception If some problem inside
*/
@Test
@SuppressWarnings("unchecked")
public void fetchesData() throws Exception {
final Valve valve = new QueryValve();
final Credentials credentials = Mockito.mock(Credentials.class);
final ImmutableMap<String, AttributeValue> item = new ImmutableMap.Builder<String, AttributeValue>().build();
final AmazonDynamoDB aws = Mockito.mock(AmazonDynamoDB.class);
Mockito.doReturn(aws).when(credentials).aws();
Mockito.doReturn(new QueryResult().withItems(Collections.<Map<String, AttributeValue>>singletonList(item)).withConsumedCapacity(new ConsumedCapacity().withCapacityUnits(1.0d))).when(aws).query(Mockito.any(QueryRequest.class));
final Dosage dosage = valve.fetch(credentials, "table", new Conditions(), new ArrayList<String>(0));
MatcherAssert.assertThat(dosage.hasNext(), Matchers.is(false));
MatcherAssert.assertThat(dosage.items(), Matchers.hasItem(item));
}
use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project qpp-conversion-tool by CMSgov.
the class DynamoDbConfig method dynamoDbMapper.
/**
* Creates a DynamoDB object mapper {@link Bean} that interacts with {@link DynamoDBTable} annotated POJOs.
* Based on an available {@link AmazonDynamoDB} and {@link AWSKMS} {@link Bean}.
*
* Creates a DynamoDB object that depends on two different environment variables. {@code DYNAMO_TABLE_NAME} and
* {@code KMS_KEY}.
*
* If {@code DYNAMO_TABLE_NAME} is specified, the value will be used for all read/write from/to the table of that name
* regardless of what is specified for {@link DynamoDBTable}. Else, the table specified for {@link DynamoDBTable} will be
* used like normal.
*
* If {@code KMS_KEY} is specified, items in the tables will be encrypted. Else, the items will not be encrypted.
*
* @param dynamoDbClient The {@link AmazonDynamoDB} {@link Bean}.
* @return The DynamoDB object mapper
*/
@Bean
public DynamoDBMapper dynamoDbMapper(AmazonDynamoDB dynamoDbClient) {
DynamoDBMapper dynamoDbMapper;
final Optional<String> kmsKey = getOptionalProperty(Constants.KMS_KEY_ENV_VARIABLE);
final Optional<String> tableName = getOptionalProperty(Constants.DYNAMO_TABLE_NAME_ENV_VARIABLE);
final Optional<String> noAudit = getOptionalProperty(Constants.NO_AUDIT_ENV_VARIABLE);
if (!noAudit.isPresent()) {
if (tableName.isPresent() && kmsKey.isPresent()) {
API_LOG.info("Using DynamoDB table name {} and KMS key {}.", tableName, kmsKey);
dynamoDbMapper = createDynamoDbMapper(dynamoDbClient, tableNameOverrideConfig(tableName.get()), encryptionTransformer(kmsKey.get()));
} else if (kmsKey.isPresent()) {
API_LOG.warn("Using KMS key {}, but no DynamoDB table name specified.", tableName);
dynamoDbMapper = createDynamoDbMapper(dynamoDbClient, getDynamoDbMapperConfig(), encryptionTransformer(kmsKey.get()));
} else {
API_LOG.error(NO_KMS_KEY + " This is a fatal error.");
throw new BeanInitializationException(NO_KMS_KEY);
}
} else {
API_LOG.info("Will not save any audit information.");
dynamoDbMapper = null;
}
return dynamoDbMapper;
}
Aggregations