Search in sources :

Example 16 with AmazonDynamoDB

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();
    }
}
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) ToString(lombok.ToString) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 17 with AmazonDynamoDB

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());
}
Also used : ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) Test(org.junit.Test)

Example 18 with AmazonDynamoDB

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));
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) ScanResult(com.amazonaws.services.dynamodbv2.model.ScanResult) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) ImmutableMap(com.google.common.collect.ImmutableMap) ConsumedCapacity(com.amazonaws.services.dynamodbv2.model.ConsumedCapacity) ScanRequest(com.amazonaws.services.dynamodbv2.model.ScanRequest) Test(org.junit.Test)

Example 19 with AmazonDynamoDB

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));
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) ImmutableMap(com.google.common.collect.ImmutableMap) ConsumedCapacity(com.amazonaws.services.dynamodbv2.model.ConsumedCapacity) QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) Test(org.junit.Test)

Example 20 with AmazonDynamoDB

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;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) Bean(org.springframework.context.annotation.Bean)

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