use of com.amazonaws.services.dynamodbv2.document.Table in project openhab1-addons by openhab.
the class DynamoDBPersistenceService method waitForTableToBecomeActive.
private boolean waitForTableToBecomeActive(String tableName) {
try {
logger.debug("Checking if table '{}' is created...", tableName);
TableDescription tableDescription = db.getDynamoDB().getTable(tableName).waitForActiveOrDelete();
if (tableDescription == null) {
// table has been deleted
logger.warn("Table '{}' deleted unexpectedly", tableName);
return false;
}
boolean success = TableStatus.ACTIVE.equals(TableStatus.fromValue(tableDescription.getTableStatus()));
if (success) {
logger.debug("Creation of table '{}' successful, table status is now {}", tableName, tableDescription.getTableStatus());
} else {
logger.warn("Creation of table '{}' unsuccessful, table status is now {}", tableName, tableDescription.getTableStatus());
}
return success;
} catch (AmazonClientException e) {
logger.error("Exception when checking table status (describe): {}", e.getMessage());
return false;
} catch (InterruptedException e) {
logger.error("Interrupted while trying to check table status: {}", e.getMessage());
return false;
}
}
use of com.amazonaws.services.dynamodbv2.document.Table in project openhab1-addons by openhab.
the class DynamoDBPersistenceService method query.
/**
* {@inheritDoc}
*/
@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
logger.debug("got a query");
if (!isProperlyConfigured) {
logger.warn("Configuration for dynamodb not yet loaded or broken. Not storing item.");
return Collections.emptyList();
}
if (!maybeConnectAndCheckConnection()) {
logger.warn("DynamoDB not connected. Not storing item.");
return Collections.emptyList();
}
String itemName = filter.getItemName();
Item item = getItemFromRegistry(itemName);
if (item == null) {
logger.warn("Could not get item {} from registry!", itemName);
return Collections.emptyList();
}
Class<DynamoDBItem<?>> dtoClass = AbstractDynamoDBItem.getDynamoItemClass(item.getClass());
String tableName = tableNameResolver.fromClass(dtoClass);
DynamoDBMapper mapper = getDBMapper(tableName);
logger.debug("item {} (class {}) will be tried to query using dto class {} from table {}", itemName, item.getClass(), dtoClass, tableName);
List<HistoricItem> historicItems = new ArrayList<HistoricItem>();
DynamoDBQueryExpression<DynamoDBItem<?>> queryExpression = createQueryExpression(dtoClass, filter);
@SuppressWarnings("rawtypes") final PaginatedQueryList<? extends DynamoDBItem> paginatedList;
try {
paginatedList = mapper.query(dtoClass, queryExpression);
} catch (AmazonServiceException e) {
logger.error("DynamoDB query raised unexpected exception: {}. Returning empty collection. " + "Status code 400 (resource not found) might occur if table was just created.", e.getMessage());
return Collections.emptyList();
}
for (int itemIndexOnPage = 0; itemIndexOnPage < filter.getPageSize(); itemIndexOnPage++) {
int itemIndex = filter.getPageNumber() * filter.getPageSize() + itemIndexOnPage;
DynamoDBItem<?> dynamoItem;
try {
dynamoItem = paginatedList.get(itemIndex);
} catch (IndexOutOfBoundsException e) {
logger.debug("Index {} is out-of-bounds", itemIndex);
break;
}
if (dynamoItem != null) {
HistoricItem historicItem = dynamoItem.asHistoricItem(item);
logger.trace("Dynamo item {} converted to historic item: {}", item, historicItem);
historicItems.add(historicItem);
}
}
return historicItems;
}
use of com.amazonaws.services.dynamodbv2.document.Table in project openhab1-addons by openhab.
the class BaseIntegrationTest method initService.
@BeforeClass
public static void initService() throws InterruptedException {
items.put("dimmer", new DimmerItem("dimmer"));
items.put("number", new NumberItem("number"));
items.put("string", new StringItem("string"));
items.put("switch", new SwitchItem("switch"));
items.put("contact", new ContactItem("contact"));
items.put("color", new ColorItem("color"));
items.put("rollershutter", new RollershutterItem("rollershutter"));
items.put("datetime", new DateTimeItem("datetime"));
items.put("call", new CallItem("call"));
items.put("location", new LocationItem("location"));
service = new DynamoDBPersistenceService();
service.setItemRegistry(new ItemRegistry() {
@Override
public void removeItemRegistryChangeListener(ItemRegistryChangeListener listener) {
throw new NotImplementedException();
}
@Override
public boolean isValidItemName(String itemName) {
throw new NotImplementedException();
}
@Override
public Collection<Item> getItems(String pattern) {
throw new NotImplementedException();
}
@Override
public Collection<Item> getItems() {
throw new NotImplementedException();
}
@Override
public Item getItemByPattern(String name) throws ItemNotFoundException, ItemNotUniqueException {
throw new NotImplementedException();
}
@Override
public Item getItem(String name) throws ItemNotFoundException {
Item item = items.get(name);
if (item == null) {
throw new ItemNotFoundException(name);
}
return item;
}
@Override
public void addItemRegistryChangeListener(ItemRegistryChangeListener listener) {
throw new NotImplementedException();
}
});
HashMap<String, Object> config = new HashMap<>();
config.put("region", System.getProperty("DYNAMODBTEST_REGION"));
config.put("accessKey", System.getProperty("DYNAMODBTEST_ACCESS"));
config.put("secretKey", System.getProperty("DYNAMODBTEST_SECRET"));
config.put("tablePrefix", "dynamodb-integration-tests-");
for (Entry<String, Object> entry : config.entrySet()) {
if (entry.getValue() == null) {
logger.warn(String.format("Expecting %s to have value for integration tests. Integration tests will be skipped", entry.getKey()));
service = null;
return;
}
}
service.activate(null, config);
// Clear data
for (String table : new String[] { "dynamodb-integration-tests-bigdecimal", "dynamodb-integration-tests-string" }) {
try {
service.getDb().getDynamoClient().deleteTable(table);
service.getDb().getDynamoDB().getTable(table).waitForDelete();
} catch (ResourceNotFoundException e) {
}
}
}
use of com.amazonaws.services.dynamodbv2.document.Table in project camel by apache.
the class ShardIteratorHandlerTest method setup.
@Before
public void setup() throws Exception {
endpoint.setAmazonDynamoDbStreamsClient(amazonDynamoDBStreams);
undertest = new ShardIteratorHandler(endpoint);
when(amazonDynamoDBStreams.listStreams(any(ListStreamsRequest.class))).thenReturn(new ListStreamsResult().withStreams(new Stream().withStreamArn("arn:aws:dynamodb:region:12345:table/table_name/stream/timestamp")));
when(amazonDynamoDBStreams.describeStream(any(DescribeStreamRequest.class))).thenReturn(new DescribeStreamResult().withStreamDescription(new StreamDescription().withTableName("table_name").withShards(ShardListTest.createShardsWithSequenceNumbers(null, "a", "1", "5", "b", "8", "15", "c", "16", "16", "d", "20", null))));
when(amazonDynamoDBStreams.getShardIterator(any(GetShardIteratorRequest.class))).thenAnswer(new Answer<GetShardIteratorResult>() {
@Override
public GetShardIteratorResult answer(InvocationOnMock invocation) throws Throwable {
return new GetShardIteratorResult().withShardIterator("shard_iterator_" + ((GetShardIteratorRequest) invocation.getArguments()[0]).getShardId() + "_000");
}
});
}
use of com.amazonaws.services.dynamodbv2.document.Table in project gora by apache.
the class DynamoDBStore method executeDeleteTableRequest.
/**
* Executes a delete table request using the DynamoDB client
*
* @param pTableName
*/
public void executeDeleteTableRequest(String pTableName) {
try {
DeleteTableRequest deleteTableRequest = new DeleteTableRequest().withTableName(pTableName);
DeleteTableResult result = getDynamoDBClient().deleteTable(deleteTableRequest);
waitForTableToBeDeleted(pTableName);
LOG.debug("Schema: " + result.getTableDescription() + " deleted successfully.");
} catch (Exception e) {
LOG.debug("Schema: {} deleted.", pTableName, e.getMessage());
throw new RuntimeException(e);
}
}
Aggregations