use of com.amazonaws.services.dynamodbv2.document.DynamoDB 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.document.DynamoDB 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.document.DynamoDB in project iep by Netflix.
the class PaginationTest method dynamoDB.
@Test
public void dynamoDB() throws Exception {
Map<String, AttributeValue> nextPage = new HashMap<>();
nextPage.put("abc", new AttributeValue());
Map<String, AttributeValue> donePage = new HashMap<>();
Function<ScanRequest, ScanResult> f = r -> {
if (r.getExclusiveStartKey() != null) {
Assert.assertTrue(r.getExclusiveStartKey().containsKey("abc"));
}
return new ScanResult().withLastEvaluatedKey((r.getExclusiveStartKey() == null) ? nextPage : donePage);
};
Publisher<ScanResult> publisher = Pagination.createPublisher(new ScanRequest(), f);
Iterable<ScanResult> iter = Flowable.fromPublisher(publisher).blockingIterable();
int count = 0;
for (ScanResult r : iter) {
++count;
}
Assert.assertEquals(2, count);
}
use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project qpp-conversion-tool by CMSgov.
the class DynamoDbConfig method dynamoDbMapper.
/**
* Creates a DynamoDB object mapper {@link Bean} that interacts with {@link DynamoDBTable} annotated POJOs.
* Based on an available {@link AmazonDynamoDB} and {@link AWSKMS} {@link Bean}.
*
* Creates a DynamoDB object that depends on two different environment variables. {@code DYNAMO_TABLE_NAME} and
* {@code KMS_KEY}.
*
* If {@code DYNAMO_TABLE_NAME} is specified, the value will be used for all read/write from/to the table of that name
* regardless of what is specified for {@link DynamoDBTable}. Else, the table specified for {@link DynamoDBTable} will be
* used like normal.
*
* If {@code KMS_KEY} is specified, items in the tables will be encrypted. Else, the items will not be encrypted.
*
* @param dynamoDbClient The {@link AmazonDynamoDB} {@link Bean}.
* @return The DynamoDB object mapper
*/
@Bean
public DynamoDBMapper dynamoDbMapper(AmazonDynamoDB dynamoDbClient) {
DynamoDBMapper dynamoDbMapper;
final Optional<String> kmsKey = getOptionalProperty(Constants.KMS_KEY_ENV_VARIABLE);
final Optional<String> tableName = getOptionalProperty(Constants.DYNAMO_TABLE_NAME_ENV_VARIABLE);
final Optional<String> noAudit = getOptionalProperty(Constants.NO_AUDIT_ENV_VARIABLE);
if (!noAudit.isPresent()) {
if (tableName.isPresent() && kmsKey.isPresent()) {
API_LOG.info("Using DynamoDB table name {} and KMS key {}.", tableName, kmsKey);
dynamoDbMapper = createDynamoDbMapper(dynamoDbClient, tableNameOverrideConfig(tableName.get()), encryptionTransformer(kmsKey.get()));
} else if (kmsKey.isPresent()) {
API_LOG.warn("Using KMS key {}, but no DynamoDB table name specified.", tableName);
dynamoDbMapper = createDynamoDbMapper(dynamoDbClient, getDynamoDbMapperConfig(), encryptionTransformer(kmsKey.get()));
} else {
API_LOG.error(NO_KMS_KEY + " This is a fatal error.");
throw new BeanInitializationException(NO_KMS_KEY);
}
} else {
API_LOG.info("Will not save any audit information.");
dynamoDbMapper = null;
}
return dynamoDbMapper;
}
use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project qpp-conversion-tool by CMSgov.
the class DynamoDbConfigFactoryTest method testFactory.
@Test
void testFactory() {
DynamoDBMapper dynamoDBMapper = DynamoDbConfigFactory.createDynamoDbMapper(null, null, null);
assertWithMessage("The DynamoDB mapper must not be null.").that(dynamoDBMapper).isNotNull();
}
Aggregations