use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project athenz by yahoo.
the class DynamoDBSSHRecordStoreConnectionTest method testGetSSHCertRecordNotFoundException.
@Test
public void testGetSSHCertRecordNotFoundException() {
Mockito.doThrow(new AmazonDynamoDBException("item not found")).when(table).getItem("primaryKey", "cn:1234");
DynamoDBSSHRecordStoreConnection dbConn = new DynamoDBSSHRecordStoreConnection(dynamoDB, tableName);
SSHCertRecord certRecord = dbConn.getSSHCertRecord("1234", "cn");
assertNull(certRecord);
dbConn.close();
}
use of com.amazonaws.services.dynamodbv2.document.DynamoDB 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.DynamoDB 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.DynamoDB in project cas by apereo.
the class DynamoDbServiceRegistryFacilitator method createServicesTable.
/**
* Create tables.
*
* @param deleteTables the delete tables
*/
@SneakyThrows
public void createServicesTable(final boolean deleteTables) {
LOGGER.debug("Attempting to create DynamoDb services table");
final CreateTableRequest request = new CreateTableRequest().withAttributeDefinitions(new AttributeDefinition(ColumnNames.ID.getColumnName(), ScalarAttributeType.S)).withKeySchema(new KeySchemaElement(ColumnNames.ID.getColumnName(), KeyType.HASH)).withProvisionedThroughput(new ProvisionedThroughput(dynamoDbProperties.getReadCapacity(), dynamoDbProperties.getWriteCapacity())).withTableName(dynamoDbProperties.getTableName());
if (deleteTables) {
final DeleteTableRequest delete = new DeleteTableRequest(request.getTableName());
LOGGER.debug("Sending delete request [{}] to remove table if necessary", delete);
TableUtils.deleteTableIfExists(amazonDynamoDBClient, delete);
}
LOGGER.debug("Sending delete request [{}] to create table", request);
TableUtils.createTableIfNotExists(amazonDynamoDBClient, request);
LOGGER.debug("Waiting until table [{}] becomes active...", request.getTableName());
TableUtils.waitUntilActive(amazonDynamoDBClient, request.getTableName());
final DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(request.getTableName());
LOGGER.debug("Sending request [{}] to obtain table description...", describeTableRequest);
final TableDescription tableDescription = amazonDynamoDBClient.describeTable(describeTableRequest).getTable();
LOGGER.debug("Located newly created table with description: [{}]", tableDescription);
}
use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project nifi by apache.
the class PutDynamoDB method onTrigger.
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
List<FlowFile> flowFiles = session.get(context.getProperty(BATCH_SIZE).evaluateAttributeExpressions().asInteger());
if (flowFiles == null || flowFiles.size() == 0) {
return;
}
Map<ItemKeys, FlowFile> keysToFlowFileMap = new HashMap<>();
final String table = context.getProperty(TABLE).evaluateAttributeExpressions().getValue();
final String hashKeyName = context.getProperty(HASH_KEY_NAME).evaluateAttributeExpressions().getValue();
final String hashKeyValueType = context.getProperty(HASH_KEY_VALUE_TYPE).getValue();
final String rangeKeyName = context.getProperty(RANGE_KEY_NAME).evaluateAttributeExpressions().getValue();
final String rangeKeyValueType = context.getProperty(RANGE_KEY_VALUE_TYPE).getValue();
final String jsonDocument = context.getProperty(JSON_DOCUMENT).evaluateAttributeExpressions().getValue();
final String charset = context.getProperty(DOCUMENT_CHARSET).evaluateAttributeExpressions().getValue();
TableWriteItems tableWriteItems = new TableWriteItems(table);
for (FlowFile flowFile : flowFiles) {
final Object hashKeyValue = getValue(context, HASH_KEY_VALUE_TYPE, HASH_KEY_VALUE, flowFile);
final Object rangeKeyValue = getValue(context, RANGE_KEY_VALUE_TYPE, RANGE_KEY_VALUE, flowFile);
if (!isHashKeyValueConsistent(hashKeyName, hashKeyValue, session, flowFile)) {
continue;
}
if (!isRangeKeyValueConsistent(rangeKeyName, rangeKeyValue, session, flowFile)) {
continue;
}
if (!isDataValid(flowFile, jsonDocument)) {
flowFile = session.putAttribute(flowFile, AWS_DYNAMO_DB_ITEM_SIZE_ERROR, "Max size of item + attribute should be 400kb but was " + flowFile.getSize() + jsonDocument.length());
session.transfer(flowFile, REL_FAILURE);
continue;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
session.exportTo(flowFile, baos);
try {
if (rangeKeyValue == null || StringUtils.isBlank(rangeKeyValue.toString())) {
tableWriteItems.addItemToPut(new Item().withKeyComponent(hashKeyName, hashKeyValue).withJSON(jsonDocument, IOUtils.toString(baos.toByteArray(), charset)));
} else {
tableWriteItems.addItemToPut(new Item().withKeyComponent(hashKeyName, hashKeyValue).withKeyComponent(rangeKeyName, rangeKeyValue).withJSON(jsonDocument, IOUtils.toString(baos.toByteArray(), charset)));
}
} catch (IOException ioe) {
getLogger().error("IOException while creating put item : " + ioe.getMessage());
flowFile = session.putAttribute(flowFile, DYNAMODB_ITEM_IO_ERROR, ioe.getMessage());
session.transfer(flowFile, REL_FAILURE);
}
keysToFlowFileMap.put(new ItemKeys(hashKeyValue, rangeKeyValue), flowFile);
}
if (keysToFlowFileMap.isEmpty()) {
return;
}
final DynamoDB dynamoDB = getDynamoDB();
try {
BatchWriteItemOutcome outcome = dynamoDB.batchWriteItem(tableWriteItems);
handleUnprocessedItems(session, keysToFlowFileMap, table, hashKeyName, hashKeyValueType, rangeKeyName, rangeKeyValueType, outcome);
// Handle any remaining flowfiles
for (FlowFile flowFile : keysToFlowFileMap.values()) {
getLogger().debug("Successful posted items to dynamodb : " + table);
session.transfer(flowFile, REL_SUCCESS);
}
} catch (AmazonServiceException exception) {
getLogger().error("Could not process flowFiles due to service exception : " + exception.getMessage());
List<FlowFile> failedFlowFiles = processServiceException(session, flowFiles, exception);
session.transfer(failedFlowFiles, REL_FAILURE);
} catch (AmazonClientException exception) {
getLogger().error("Could not process flowFiles due to client exception : " + exception.getMessage());
List<FlowFile> failedFlowFiles = processClientException(session, flowFiles, exception);
session.transfer(failedFlowFiles, REL_FAILURE);
} catch (Exception exception) {
getLogger().error("Could not process flowFiles due to exception : " + exception.getMessage());
List<FlowFile> failedFlowFiles = processException(session, flowFiles, exception);
session.transfer(failedFlowFiles, REL_FAILURE);
}
}
Aggregations