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