use of com.amazonaws.services.dynamodbv2.document.Table in project archaius by Netflix.
the class DynamoDbConfigurationSource method loadPropertiesFromTable.
@Override
protected synchronized Map<String, Object> loadPropertiesFromTable(String table) {
Map<String, Object> propertyMap = new HashMap<String, Object>();
Map<String, AttributeValue> lastKeysEvaluated = null;
do {
ScanRequest scanRequest = new ScanRequest().withTableName(table).withExclusiveStartKey(lastKeysEvaluated);
ScanResult result = dbScanWithThroughputBackOff(scanRequest);
for (Map<String, AttributeValue> item : result.getItems()) {
propertyMap.put(item.get(keyAttributeName.get()).getS(), item.get(valueAttributeName.get()).getS());
}
lastKeysEvaluated = result.getLastEvaluatedKey();
} while (lastKeysEvaluated != null);
return propertyMap;
}
use of com.amazonaws.services.dynamodbv2.document.Table 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.document.Table 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.document.Table in project camel by apache.
the class DdbEndpoint method waitForTableToBecomeAvailable.
private void waitForTableToBecomeAvailable(String tableName) {
LOG.trace("Waiting for [{}] to become ACTIVE...", tableName);
long waitTime = 5 * 60 * 1000;
while (waitTime > 0) {
try {
Thread.sleep(1000 * 5);
waitTime -= 5000;
} catch (Exception e) {
}
try {
DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
TableDescription tableDescription = getDdbClient().describeTable(request).getTable();
if (isTableActive(tableDescription)) {
LOG.trace("Table [{}] became active", tableName);
return;
}
LOG.trace("Table [{}] not active yet", tableName);
} catch (AmazonServiceException ase) {
if (!ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException")) {
throw ase;
}
}
}
throw new RuntimeException("Table " + tableName + " never went active");
}
use of com.amazonaws.services.dynamodbv2.document.Table in project camel by apache.
the class DdbEndpoint method doStart.
@Override
public void doStart() throws Exception {
super.doStart();
ddbClient = configuration.getAmazonDDBClient() != null ? configuration.getAmazonDDBClient() : createDdbClient();
if (ObjectHelper.isNotEmpty(configuration.getAmazonDdbEndpoint())) {
ddbClient.setEndpoint(configuration.getAmazonDdbEndpoint());
}
String tableName = getConfiguration().getTableName();
LOG.trace("Querying whether table [{}] already exists...", tableName);
try {
DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
TableDescription tableDescription = ddbClient.describeTable(request).getTable();
if (!isTableActive(tableDescription)) {
waitForTableToBecomeAvailable(tableName);
}
LOG.trace("Table [{}] already exists", tableName);
return;
} catch (ResourceNotFoundException e) {
LOG.trace("Table [{}] doesn't exist yet", tableName);
LOG.trace("Creating table [{}]...", tableName);
TableDescription tableDescription = createTable(tableName);
if (!isTableActive(tableDescription)) {
waitForTableToBecomeAvailable(tableName);
}
LOG.trace("Table [{}] created", tableName);
}
}
Aggregations