Search in sources :

Example 46 with DynamoDB

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

the class AwsItem method get.

@Override
public AttributeValue get(final String attr) throws IOException {
    final String attrib = String.format(Locale.ENGLISH, attr);
    AttributeValue value = this.attributes.get(attrib);
    if (value == null) {
        final AmazonDynamoDB aws = this.credentials.aws();
        try {
            final GetItemRequest request = this.makeItemRequestFor(attrib);
            final long start = System.currentTimeMillis();
            final GetItemResult result = aws.getItem(request);
            value = result.getItem().get(attrib);
            Logger.info(this, // @checkstyle LineLength (1 line)
            "#get('%s'): loaded '%[text]s' from DynamoDB, %s, in %[ms]s", attrib, value, new PrintableConsumedCapacity(result.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
        } catch (final AmazonClientException ex) {
            throw new IOException(String.format("Failed to get \"%s\" from \"%s\" by %s", attr, this.name, this.keys), ex);
        } finally {
            aws.shutdown();
        }
    }
    if (value == null) {
        throw new NoSuchElementException(String.format("attribute \"%s\" not found", attr));
    }
    return value;
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) GetItemResult(com.amazonaws.services.dynamodbv2.model.GetItemResult) AmazonClientException(com.amazonaws.AmazonClientException) ToString(lombok.ToString) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException) GetItemRequest(com.amazonaws.services.dynamodbv2.model.GetItemRequest)

Example 47 with DynamoDB

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

the class AwsItem method put.

@Override
public Map<String, AttributeValue> put(final Map<String, AttributeValueUpdate> attrs) throws IOException {
    final AmazonDynamoDB aws = this.credentials.aws();
    final Attributes expected = this.attributes.only(this.keys);
    try {
        final UpdateItemRequest request = new UpdateItemRequest().withTableName(this.name).withExpected(expected.asKeys()).withKey(expected).withAttributeUpdates(attrs).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL).withReturnValues(ReturnValue.UPDATED_NEW);
        final long start = System.currentTimeMillis();
        final UpdateItemResult result = aws.updateItem(request);
        Logger.info(this, "#put('%s'): updated item to DynamoDB, %s, in %[ms]s", attrs, new PrintableConsumedCapacity(result.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
        return result.getAttributes();
    } catch (final AmazonClientException ex) {
        throw new IOException(String.format("Failed to put %s into \"%s\" with %s", attrs, this.name, this.keys), ex);
    } finally {
        aws.shutdown();
    }
}
Also used : UpdateItemRequest(com.amazonaws.services.dynamodbv2.model.UpdateItemRequest) AmazonClientException(com.amazonaws.AmazonClientException) UpdateItemResult(com.amazonaws.services.dynamodbv2.model.UpdateItemResult) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) IOException(java.io.IOException)

Example 48 with DynamoDB

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

the class AwsIterator method remove.

@Override
@SuppressWarnings("PMD.UseConcurrentHashMap")
public void remove() {
    synchronized (this.dosage) {
        final AmazonDynamoDB aws = this.credentials.aws();
        try {
            final Dosage prev = this.dosage.get();
            final List<Map<String, AttributeValue>> items = new ArrayList<Map<String, AttributeValue>>(prev.items());
            final Map<String, AttributeValue> item = items.remove(this.position);
            final long start = System.currentTimeMillis();
            final DeleteItemResult res = aws.deleteItem(new DeleteItemRequest().withTableName(this.name).withKey(new Attributes(item).only(this.keys)).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL).withExpected(new Attributes(item).only(this.keys).asKeys()));
            this.dosage.set(new AwsIterator.Fixed(prev, items));
            --this.position;
            Logger.info(this, "#remove(): item #%d removed from DynamoDB, %s, in %[ms]s", this.position, new PrintableConsumedCapacity(res.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
        } finally {
            aws.shutdown();
        }
    }
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) DeleteItemRequest(com.amazonaws.services.dynamodbv2.model.DeleteItemRequest) ArrayList(java.util.ArrayList) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) ToString(lombok.ToString) DeleteItemResult(com.amazonaws.services.dynamodbv2.model.DeleteItemResult) Map(java.util.Map)

Example 49 with DynamoDB

use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project spring-integration-aws by spring-projects.

the class DynamoDbMetaDataStore method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    try {
        this.table.describe();
        this.createTableLatch.countDown();
        return;
    } catch (ResourceNotFoundException e) {
        if (logger.isInfoEnabled()) {
            logger.info("No table '" + this.table.getTableName() + "'. Creating one...");
        }
    }
    CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(this.table.getTableName()).withKeySchema(new KeySchemaElement(KEY, KeyType.HASH)).withAttributeDefinitions(new AttributeDefinition(KEY, ScalarAttributeType.S)).withProvisionedThroughput(new ProvisionedThroughput(this.readCapacity, this.writeCapacity));
    this.dynamoDB.createTableAsync(createTableRequest, new AsyncHandler<CreateTableRequest, CreateTableResult>() {

        @Override
        public void onError(Exception e) {
            logger.error("Cannot create DynamoDb table: " + DynamoDbMetaDataStore.this.table.getTableName(), e);
            DynamoDbMetaDataStore.this.createTableLatch.countDown();
        }

        @Override
        public void onSuccess(CreateTableRequest request, CreateTableResult createTableResult) {
            Waiter<DescribeTableRequest> waiter = DynamoDbMetaDataStore.this.dynamoDB.waiters().tableExists();
            WaiterParameters<DescribeTableRequest> waiterParameters = new WaiterParameters<>(new DescribeTableRequest(DynamoDbMetaDataStore.this.table.getTableName())).withPollingStrategy(new PollingStrategy(new MaxAttemptsRetryStrategy(DynamoDbMetaDataStore.this.createTableRetries), new FixedDelayStrategy(DynamoDbMetaDataStore.this.createTableDelay)));
            waiter.runAsync(waiterParameters, new WaiterHandler<DescribeTableRequest>() {

                @Override
                public void onWaitSuccess(DescribeTableRequest request) {
                    DynamoDbMetaDataStore.this.createTableLatch.countDown();
                    DynamoDbMetaDataStore.this.table.describe();
                }

                @Override
                public void onWaitFailure(Exception e) {
                    logger.error("Cannot describe DynamoDb table: " + DynamoDbMetaDataStore.this.table.getTableName(), e);
                    DynamoDbMetaDataStore.this.createTableLatch.countDown();
                }
            });
        }
    });
}
Also used : MaxAttemptsRetryStrategy(com.amazonaws.waiters.MaxAttemptsRetryStrategy) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException) WaiterParameters(com.amazonaws.waiters.WaiterParameters) PollingStrategy(com.amazonaws.waiters.PollingStrategy) FixedDelayStrategy(com.amazonaws.waiters.FixedDelayStrategy) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException) Waiter(com.amazonaws.waiters.Waiter) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) WaiterHandler(com.amazonaws.waiters.WaiterHandler) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) CreateTableResult(com.amazonaws.services.dynamodbv2.model.CreateTableResult)

Example 50 with DynamoDB

use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project spring-integration-aws by spring-projects.

the class DynamoDbMetadataStoreTests method setup.

@BeforeClass
public static void setup() throws Exception {
    AmazonDynamoDBAsync dynamoDB = DYNAMO_DB_RUNNING.getDynamoDB();
    try {
        dynamoDB.deleteTableAsync(TEST_TABLE);
        Waiter<DescribeTableRequest> waiter = dynamoDB.waiters().tableNotExists();
        waiter.run(new WaiterParameters<>(new DescribeTableRequest(TEST_TABLE)).withPollingStrategy(new PollingStrategy(new MaxAttemptsRetryStrategy(25), new FixedDelayStrategy(1))));
    } catch (Exception e) {
    }
    store = new DynamoDbMetaDataStore(dynamoDB, TEST_TABLE);
    store.afterPropertiesSet();
}
Also used : PollingStrategy(com.amazonaws.waiters.PollingStrategy) AmazonDynamoDBAsync(com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync) MaxAttemptsRetryStrategy(com.amazonaws.waiters.MaxAttemptsRetryStrategy) DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) FixedDelayStrategy(com.amazonaws.waiters.FixedDelayStrategy) WaiterParameters(com.amazonaws.waiters.WaiterParameters) BeforeClass(org.junit.BeforeClass)

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