Search in sources :

Example 46 with AmazonClientException

use of com.amazonaws.AmazonClientException 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 47 with AmazonClientException

use of com.amazonaws.AmazonClientException 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 48 with AmazonClientException

use of com.amazonaws.AmazonClientException 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 49 with AmazonClientException

use of com.amazonaws.AmazonClientException 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)

Example 50 with AmazonClientException

use of com.amazonaws.AmazonClientException 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)

Aggregations

AmazonClientException (com.amazonaws.AmazonClientException)202 IOException (java.io.IOException)70 AmazonServiceException (com.amazonaws.AmazonServiceException)32 ArrayList (java.util.ArrayList)32 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)23 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)19 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)17 HashMap (java.util.HashMap)16 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)14 Test (org.junit.Test)14 SienaException (siena.SienaException)12 AWSCredentials (com.amazonaws.auth.AWSCredentials)11 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)11 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)11 ListObjectsRequest (com.amazonaws.services.s3.model.ListObjectsRequest)11 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)10 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)10 InterruptedIOException (java.io.InterruptedIOException)10 DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)9 File (java.io.File)9