Search in sources :

Example 11 with AmazonDynamoDB

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

the class MadeTable method create.

/**
 * Create table.
 * @throws InterruptedException If something fails
 */
public void create() throws InterruptedException {
    final AmazonDynamoDB aws = this.region.aws();
    final String name = this.request.getTableName();
    aws.createTable(this.request);
    Logger.info(this, "DynamoDB table '%s' creation requested...", name);
    final DescribeTableRequest req = new DescribeTableRequest().withTableName(name);
    while (true) {
        final DescribeTableResult result = aws.describeTable(req);
        if ("ACTIVE".equals(result.getTable().getTableStatus())) {
            Logger.info(this, "DynamoDB table '%s' is %s", name, result.getTable().getTableStatus());
            break;
        }
        Logger.info(this, "waiting for DynamoDB table '%s': %s", name, result.getTable().getTableStatus());
        TimeUnit.SECONDS.sleep((long) Tv.TEN);
    }
}
Also used : AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) DescribeTableResult(com.amazonaws.services.dynamodbv2.model.DescribeTableResult)

Example 12 with AmazonDynamoDB

use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB 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 13 with AmazonDynamoDB

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

the class AwsTable method put.

@Override
public Item put(final Map<String, AttributeValue> attributes) throws IOException {
    final AmazonDynamoDB aws = this.credentials.aws();
    try {
        final PutItemRequest request = new PutItemRequest();
        request.setTableName(this.self);
        request.setItem(attributes);
        request.setReturnValues(ReturnValue.NONE);
        request.setReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
        final PutItemResult result = aws.putItem(request);
        final long start = System.currentTimeMillis();
        Logger.info(this, "#put('%[text]s'): created item in '%s', %s, in %[ms]s", attributes, this.self, new PrintableConsumedCapacity(result.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
        return new AwsItem(this.credentials, this.frame(), this.self, new Attributes(attributes).only(this.keys()), new Array<String>(this.keys()));
    } catch (final AmazonClientException ex) {
        throw new IOException(String.format("Failed to put into \"%s\" with %s", this.self, attributes), ex);
    } finally {
        aws.shutdown();
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) PutItemRequest(com.amazonaws.services.dynamodbv2.model.PutItemRequest) PutItemResult(com.amazonaws.services.dynamodbv2.model.PutItemResult) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) ToString(lombok.ToString) IOException(java.io.IOException)

Example 14 with AmazonDynamoDB

use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB 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 15 with AmazonDynamoDB

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

the class AwsTable method delete.

@Override
public void delete(final Map<String, AttributeValue> attributes) throws IOException {
    final AmazonDynamoDB aws = this.credentials.aws();
    try {
        final DeleteItemRequest request = new DeleteItemRequest();
        request.setTableName(this.self);
        request.setKey(attributes);
        request.setReturnValues(ReturnValue.NONE);
        request.setReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
        final DeleteItemResult result = aws.deleteItem(request);
        final long start = System.currentTimeMillis();
        Logger.info(this, "#delete('%[text]s'): deleted item in '%s', %s, in %[ms]s", attributes, this.self, new PrintableConsumedCapacity(result.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
    } catch (final AmazonClientException ex) {
        throw new IOException(String.format("Failed to delete at \"%s\" by keys %s", this.self, attributes), ex);
    } finally {
        aws.shutdown();
    }
}
Also used : DeleteItemRequest(com.amazonaws.services.dynamodbv2.model.DeleteItemRequest) AmazonClientException(com.amazonaws.AmazonClientException) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DeleteItemResult(com.amazonaws.services.dynamodbv2.model.DeleteItemResult) IOException(java.io.IOException)

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