Search in sources :

Example 21 with DynamoDB

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

the class AwsItem method has.

@Override
public boolean has(final String attr) throws IOException {
    final String attrib = String.format(Locale.ENGLISH, attr);
    boolean has = this.attributes.containsKey(attrib);
    if (!has) {
        final AmazonDynamoDB aws = this.credentials.aws();
        try {
            final GetItemRequest request = this.makeItemRequestFor(attr);
            final long start = System.currentTimeMillis();
            final GetItemResult result = aws.getItem(request);
            has = result.getItem().get(attrib) != null;
            Logger.info(this, "#has('%s'): %B from DynamoDB, %s, in %[ms]s", attr, has, new PrintableConsumedCapacity(result.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
        } catch (final AmazonClientException ex) {
            throw new IOException(String.format("Failed to check existence of \"%s\" at \"%s\" by %s", attr, this.name, this.keys), ex);
        } finally {
            aws.shutdown();
        }
    }
    return has;
}
Also used : GetItemResult(com.amazonaws.services.dynamodbv2.model.GetItemResult) AmazonClientException(com.amazonaws.AmazonClientException) ToString(lombok.ToString) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) IOException(java.io.IOException) GetItemRequest(com.amazonaws.services.dynamodbv2.model.GetItemRequest)

Example 22 with DynamoDB

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

the class AwsTable method keys.

/**
 * Get names of keys.
 * @return Names of attributes, which are primary keys
 * @throws IOException If DynamoDB fails
 */
@Cacheable(forever = true)
public Collection<String> keys() throws IOException {
    final AmazonDynamoDB aws = this.credentials.aws();
    try {
        final long start = System.currentTimeMillis();
        final DescribeTableResult result = aws.describeTable(new DescribeTableRequest().withTableName(this.self));
        final Collection<String> keys = new LinkedList<String>();
        for (final KeySchemaElement key : result.getTable().getKeySchema()) {
            keys.add(key.getAttributeName());
        }
        Logger.info(this, "#keys(): table %s described, in %[ms]s", this.self, System.currentTimeMillis() - start);
        return keys;
    } catch (final AmazonClientException ex) {
        throw new IOException(String.format("Failed to describe \"%s\"", this.self), ex);
    } finally {
        aws.shutdown();
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DescribeTableResult(com.amazonaws.services.dynamodbv2.model.DescribeTableResult) DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) ToString(lombok.ToString) IOException(java.io.IOException) LinkedList(java.util.LinkedList) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) Cacheable(com.jcabi.aspects.Cacheable)

Example 23 with DynamoDB

use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project iep by Netflix.

the class PaginationTest method dynamoDB.

@Test
public void dynamoDB() throws Exception {
    Map<String, AttributeValue> nextPage = new HashMap<>();
    nextPage.put("abc", new AttributeValue());
    Map<String, AttributeValue> donePage = new HashMap<>();
    Function<ScanRequest, ScanResult> f = r -> {
        if (r.getExclusiveStartKey() != null) {
            Assert.assertTrue(r.getExclusiveStartKey().containsKey("abc"));
        }
        return new ScanResult().withLastEvaluatedKey((r.getExclusiveStartKey() == null) ? nextPage : donePage);
    };
    Publisher<ScanResult> publisher = Pagination.createPublisher(new ScanRequest(), f);
    Iterable<ScanResult> iter = Flowable.fromPublisher(publisher).blockingIterable();
    int count = 0;
    for (ScanResult r : iter) {
        ++count;
    }
    Assert.assertEquals(2, count);
}
Also used : ListHostedZonesResult(com.amazonaws.services.route53.model.ListHostedZonesResult) ListMetricsRequest(com.amazonaws.services.cloudwatch.model.ListMetricsRequest) SortedSet(java.util.SortedSet) ScanResult(com.amazonaws.services.dynamodbv2.model.ScanResult) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) ListMetricsResult(com.amazonaws.services.cloudwatch.model.ListMetricsResult) DescribeLoadBalancersRequest(com.amazonaws.services.elasticloadbalancing.model.DescribeLoadBalancersRequest) Function(java.util.function.Function) TreeSet(java.util.TreeSet) PutMetricDataRequest(com.amazonaws.services.cloudwatch.model.PutMetricDataRequest) DescribeTargetGroupsRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsRequest) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) Map(java.util.Map) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) ListClustersResult(com.amazonaws.services.elasticmapreduce.model.ListClustersResult) DescribeTargetGroupsResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsResult) Iterator(java.util.Iterator) ScanRequest(com.amazonaws.services.dynamodbv2.model.ScanRequest) Publisher(org.reactivestreams.Publisher) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) ListResourceRecordSetsResult(com.amazonaws.services.route53.model.ListResourceRecordSetsResult) ListHostedZonesRequest(com.amazonaws.services.route53.model.ListHostedZonesRequest) ListClustersRequest(com.amazonaws.services.elasticmapreduce.model.ListClustersRequest) DescribeLoadBalancersResult(com.amazonaws.services.elasticloadbalancing.model.DescribeLoadBalancersResult) PutMetricDataResult(com.amazonaws.services.cloudwatch.model.PutMetricDataResult) Assert(org.junit.Assert) ListResourceRecordSetsRequest(com.amazonaws.services.route53.model.ListResourceRecordSetsRequest) ScanRequest(com.amazonaws.services.dynamodbv2.model.ScanRequest) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) ScanResult(com.amazonaws.services.dynamodbv2.model.ScanResult) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 24 with DynamoDB

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

Example 25 with DynamoDB

use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project qpp-conversion-tool by CMSgov.

the class DynamoDbConfigFactoryTest method testFactory.

@Test
void testFactory() {
    DynamoDBMapper dynamoDBMapper = DynamoDbConfigFactory.createDynamoDbMapper(null, null, null);
    assertWithMessage("The DynamoDB mapper must not be null.").that(dynamoDBMapper).isNotNull();
}
Also used : DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) Test(org.junit.jupiter.api.Test)

Aggregations

DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)24 Test (org.junit.Test)16 TestRunner (org.apache.nifi.util.TestRunner)15 MockFlowFile (org.apache.nifi.util.MockFlowFile)14 AmazonClientException (com.amazonaws.AmazonClientException)12 AmazonServiceException (com.amazonaws.AmazonServiceException)10 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)10 TableWriteItems (com.amazonaws.services.dynamodbv2.document.TableWriteItems)10 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)9 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 List (java.util.List)9 TableKeysAndAttributes (com.amazonaws.services.dynamodbv2.document.TableKeysAndAttributes)8 Map (java.util.Map)8 IOException (java.io.IOException)6 AmazonDynamoDBClient (com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient)5 DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)5 BatchGetItemOutcome (com.amazonaws.services.dynamodbv2.document.BatchGetItemOutcome)5 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)5 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)5