use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper in project openhab1-addons by openhab.
the class DynamoDBPersistenceService method createTable.
/**
* Create table (if not present) and wait for table to become active.
*
* Synchronized in order to ensure that at most single thread is creating the table at a time
*
* @param mapper
* @param dtoClass
* @return whether table creation succeeded.
*/
private synchronized boolean createTable(DynamoDBMapper mapper, Class<?> dtoClass) {
if (db == null) {
return false;
}
String tableName;
try {
ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput(dbConfig.getReadCapacityUnits(), dbConfig.getWriteCapacityUnits());
CreateTableRequest request = mapper.generateCreateTableRequest(dtoClass);
request.setProvisionedThroughput(provisionedThroughput);
if (request.getGlobalSecondaryIndexes() != null) {
for (GlobalSecondaryIndex index : request.getGlobalSecondaryIndexes()) {
index.setProvisionedThroughput(provisionedThroughput);
}
}
tableName = request.getTableName();
try {
db.getDynamoClient().describeTable(tableName);
} catch (ResourceNotFoundException e) {
// No table present, continue with creation
db.getDynamoClient().createTable(request);
} catch (AmazonClientException e) {
logger.error("Table creation failed due to error in describeTable operation", e);
return false;
}
// table found or just created, wait
return waitForTableToBecomeActive(tableName);
} catch (AmazonClientException e) {
logger.error("Exception when creating table", e);
return false;
}
}
use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper in project openhab1-addons by openhab.
the class DynamoDBPersistenceService method store.
/**
* {@inheritDoc}
*/
@Override
public void store(Item item, String alias) {
if (item.getState() instanceof UnDefType) {
logger.debug("Undefined item state received. Not storing item.");
return;
}
if (!isProperlyConfigured) {
logger.warn("Configuration for dynamodb not yet loaded or broken. Not storing item.");
return;
}
if (!maybeConnectAndCheckConnection()) {
logger.warn("DynamoDB not connected. Not storing item.");
return;
}
String realName = item.getName();
String name = (alias != null) ? alias : realName;
Date time = new Date(System.currentTimeMillis());
State state = item.getState();
logger.trace("Tried to get item from item class {}, state is {}", item.getClass(), state.toString());
DynamoDBItem<?> dynamoItem = AbstractDynamoDBItem.fromState(name, state, time);
DynamoDBMapper mapper = getDBMapper(tableNameResolver.fromItem(dynamoItem));
if (!createTable(mapper, dynamoItem.getClass())) {
logger.warn("Table creation failed. Not storing item");
return;
}
try {
logger.debug("storing {} in dynamo. Serialized value {}. Original Item: {}", name, state, item);
mapper.save(dynamoItem);
logger.debug("Sucessfully stored item {}", item);
} catch (AmazonClientException e) {
logger.error("Error storing object to dynamo: {}", e.getMessage());
}
}
use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper in project tutorials by eugenp.
the class ProductInfoRepositoryIntegrationTest method setup.
@Before
public void setup() throws Exception {
try {
dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB);
CreateTableRequest tableRequest = dynamoDBMapper.generateCreateTableRequest(ProductInfo.class);
tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
amazonDynamoDB.createTable(tableRequest);
} catch (ResourceInUseException e) {
// Do nothing, table already created
}
// TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfo on table
dynamoDBMapper.batchDelete((List<ProductInfo>) repository.findAll());
}
use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper in project tutorials by eugenp.
the class ProductInfoRepositoryIntegrationTest method setupClass.
@BeforeClass
public static void setupClass() {
Properties testProperties = loadFromFileInClasspath("test.properties").filter(properties -> !isEmpty(properties.getProperty(AWS_ACCESSKEY))).filter(properties -> !isEmpty(properties.getProperty(AWS_SECRETKEY))).filter(properties -> !isEmpty(properties.getProperty(DYNAMODB_ENDPOINT))).orElseThrow(() -> new RuntimeException("Unable to get all of the required test property values"));
String amazonAWSAccessKey = testProperties.getProperty(AWS_ACCESSKEY);
String amazonAWSSecretKey = testProperties.getProperty(AWS_SECRETKEY);
String amazonDynamoDBEndpoint = testProperties.getProperty(DYNAMODB_ENDPOINT);
amazonDynamoDB = new AmazonDynamoDBClient(new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey));
amazonDynamoDB.setEndpoint(amazonDynamoDBEndpoint);
dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB);
}
use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper 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;
}
Aggregations