Search in sources :

Example 21 with Table

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;
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) TableDescription(com.amazonaws.services.dynamodbv2.model.TableDescription)

Example 22 with Table

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;
}
Also used : DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) ArrayList(java.util.ArrayList) HistoricItem(org.openhab.core.persistence.HistoricItem) Item(org.openhab.core.items.Item) AmazonServiceException(com.amazonaws.AmazonServiceException) HistoricItem(org.openhab.core.persistence.HistoricItem)

Example 23 with Table

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) {
        }
    }
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) HashMap(java.util.HashMap) NotImplementedException(org.apache.commons.lang.NotImplementedException) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) ItemRegistry(org.openhab.core.items.ItemRegistry) DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) CallItem(org.openhab.library.tel.items.CallItem) Item(org.openhab.core.items.Item) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) StringItem(org.openhab.core.library.items.StringItem) ContactItem(org.openhab.core.library.items.ContactItem) NumberItem(org.openhab.core.library.items.NumberItem) LocationItem(org.openhab.core.library.items.LocationItem) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) ItemRegistryChangeListener(org.openhab.core.items.ItemRegistryChangeListener) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException) SwitchItem(org.openhab.core.library.items.SwitchItem) ContactItem(org.openhab.core.library.items.ContactItem) ItemNotUniqueException(org.openhab.core.items.ItemNotUniqueException) StringItem(org.openhab.core.library.items.StringItem) NumberItem(org.openhab.core.library.items.NumberItem) Collection(java.util.Collection) CallItem(org.openhab.library.tel.items.CallItem) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException) BeforeClass(org.junit.BeforeClass)

Example 24 with Table

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");
        }
    });
}
Also used : GetShardIteratorRequest(com.amazonaws.services.dynamodbv2.model.GetShardIteratorRequest) ListStreamsRequest(com.amazonaws.services.dynamodbv2.model.ListStreamsRequest) DescribeStreamRequest(com.amazonaws.services.dynamodbv2.model.DescribeStreamRequest) StreamDescription(com.amazonaws.services.dynamodbv2.model.StreamDescription) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ListStreamsResult(com.amazonaws.services.dynamodbv2.model.ListStreamsResult) GetShardIteratorResult(com.amazonaws.services.dynamodbv2.model.GetShardIteratorResult) Stream(com.amazonaws.services.dynamodbv2.model.Stream) DescribeStreamResult(com.amazonaws.services.dynamodbv2.model.DescribeStreamResult) Before(org.junit.Before)

Example 25 with Table

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);
    }
}
Also used : DeleteTableRequest(com.amazonaws.services.dynamodbv2.model.DeleteTableRequest) DeleteTableResult(com.amazonaws.services.dynamodbv2.model.DeleteTableResult) GoraException(org.apache.gora.util.GoraException) AmazonServiceException(com.amazonaws.AmazonServiceException) IOException(java.io.IOException) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)

Aggregations

AmazonServiceException (com.amazonaws.AmazonServiceException)16 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)12 ResourceNotFoundException (com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)12 TableDescription (com.amazonaws.services.dynamodbv2.model.TableDescription)11 HashMap (java.util.HashMap)10 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)9 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)9 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)9 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)8 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)8 ScanRequest (com.amazonaws.services.dynamodbv2.model.ScanRequest)8 ScanResult (com.amazonaws.services.dynamodbv2.model.ScanResult)7 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 DeleteTableRequest (com.amazonaws.services.dynamodbv2.model.DeleteTableRequest)5 GoraException (org.apache.gora.util.GoraException)4 AmazonClientException (com.amazonaws.AmazonClientException)3 DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)2 Table (com.amazonaws.services.dynamodbv2.document.Table)2