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;
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
Aggregations