Search in sources :

Example 26 with DynamoDB

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;
}
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 27 with DynamoDB

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) {
        }
    }
}
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 28 with DynamoDB

use of com.amazonaws.services.dynamodbv2.document.DynamoDB 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)

Example 29 with DynamoDB

use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project gora by apache.

the class DynamoDBNativeStore method execute.

/**
   * Executes a query after building a DynamoDB specific query based on the
   * received one
   */
@Override
public Result<K, T> execute(Query<K, T> query) {
    DynamoDBQuery<K, T> dynamoDBQuery = buildDynamoDBQuery(query);
    DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBStoreHandler.getDynamoDbClient());
    List<T> objList = null;
    if (DynamoDBQuery.getType().equals(DynamoDBQuery.RANGE_QUERY))
        objList = mapper.scan(persistentClass, (DynamoDBScanExpression) dynamoDBQuery.getQueryExpression());
    if (DynamoDBQuery.getType().equals(DynamoDBQuery.SCAN_QUERY))
        objList = mapper.scan(persistentClass, (DynamoDBScanExpression) dynamoDBQuery.getQueryExpression());
    return new DynamoDBResult<K, T>(this, query, objList);
}
Also used : DynamoDBResult(org.apache.gora.dynamodb.query.DynamoDBResult) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)

Example 30 with DynamoDB

use of com.amazonaws.services.dynamodbv2.document.DynamoDB in project cas by apereo.

the class AmazonDynamoDbClientFactory method createAmazonDynamoDb.

/**
 * Create amazon dynamo db instance.
 *
 * @param dynamoDbProperties the dynamo db properties
 * @return the amazon dynamo db
 */
@SneakyThrows
public AmazonDynamoDB createAmazonDynamoDb(final AbstractDynamoDbProperties dynamoDbProperties) {
    if (dynamoDbProperties.isLocalInstance()) {
        LOGGER.debug("Creating DynamoDb standard client with endpoint [{}] and region [{}]", dynamoDbProperties.getEndpoint(), dynamoDbProperties.getRegion());
        final AwsClientBuilder.EndpointConfiguration endpoint = new AwsClientBuilder.EndpointConfiguration(dynamoDbProperties.getEndpoint(), dynamoDbProperties.getRegion());
        return AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(endpoint).build();
    }
    final AWSCredentialsProvider provider = ChainingAWSCredentialsProvider.getInstance(dynamoDbProperties.getCredentialAccessKey(), dynamoDbProperties.getCredentialSecretKey(), dynamoDbProperties.getCredentialsPropertiesFile());
    LOGGER.debug("Creating DynamoDb client configuration...");
    final ClientConfiguration cfg = new ClientConfiguration();
    cfg.setConnectionTimeout(dynamoDbProperties.getConnectionTimeout());
    cfg.setMaxConnections(dynamoDbProperties.getMaxConnections());
    cfg.setRequestTimeout(dynamoDbProperties.getRequestTimeout());
    cfg.setSocketTimeout(dynamoDbProperties.getSocketTimeout());
    cfg.setUseGzip(dynamoDbProperties.isUseGzip());
    cfg.setUseReaper(dynamoDbProperties.isUseReaper());
    cfg.setUseThrottleRetries(dynamoDbProperties.isUseThrottleRetries());
    cfg.setUseTcpKeepAlive(dynamoDbProperties.isUseTcpKeepAlive());
    cfg.setProtocol(Protocol.valueOf(dynamoDbProperties.getProtocol().toUpperCase()));
    cfg.setClientExecutionTimeout(dynamoDbProperties.getClientExecutionTimeout());
    cfg.setCacheResponseMetadata(dynamoDbProperties.isCacheResponseMetadata());
    if (StringUtils.isNotBlank(dynamoDbProperties.getLocalAddress())) {
        LOGGER.debug("Creating DynamoDb client local address [{}]", dynamoDbProperties.getLocalAddress());
        cfg.setLocalAddress(InetAddress.getByName(dynamoDbProperties.getLocalAddress()));
    }
    LOGGER.debug("Creating DynamoDb client instance...");
    final AmazonDynamoDBClient client = new AmazonDynamoDBClient(provider, cfg);
    if (StringUtils.isNotBlank(dynamoDbProperties.getEndpoint())) {
        LOGGER.debug("Setting DynamoDb client endpoint [{}]", dynamoDbProperties.getEndpoint());
        client.setEndpoint(dynamoDbProperties.getEndpoint());
    }
    if (StringUtils.isNotBlank(dynamoDbProperties.getRegion())) {
        LOGGER.debug("Setting DynamoDb client region [{}]", dynamoDbProperties.getRegion());
        client.setRegion(Region.getRegion(Regions.valueOf(dynamoDbProperties.getRegion())));
    }
    if (StringUtils.isNotBlank(dynamoDbProperties.getRegionOverride())) {
        LOGGER.debug("Setting DynamoDb client region override [{}]", dynamoDbProperties.getRegionOverride());
        client.setSignerRegionOverride(dynamoDbProperties.getRegionOverride());
    }
    if (StringUtils.isNotBlank(dynamoDbProperties.getServiceNameIntern())) {
        client.setServiceNameIntern(dynamoDbProperties.getServiceNameIntern());
    }
    if (dynamoDbProperties.getTimeOffset() != 0) {
        client.setTimeOffset(dynamoDbProperties.getTimeOffset());
    }
    return client;
}
Also used : AwsClientBuilder(com.amazonaws.client.builder.AwsClientBuilder) AmazonDynamoDBClient(com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient) ChainingAWSCredentialsProvider(org.apereo.cas.aws.ChainingAWSCredentialsProvider) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider) ClientConfiguration(com.amazonaws.ClientConfiguration) SneakyThrows(lombok.SneakyThrows)

Aggregations

DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)24 Test (org.junit.Test)16 TestRunner (org.apache.nifi.util.TestRunner)15 MockFlowFile (org.apache.nifi.util.MockFlowFile)14 AmazonClientException (com.amazonaws.AmazonClientException)12 AmazonServiceException (com.amazonaws.AmazonServiceException)10 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)10 TableWriteItems (com.amazonaws.services.dynamodbv2.document.TableWriteItems)10 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)9 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 List (java.util.List)9 TableKeysAndAttributes (com.amazonaws.services.dynamodbv2.document.TableKeysAndAttributes)8 Map (java.util.Map)8 IOException (java.io.IOException)6 AmazonDynamoDBClient (com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient)5 DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)5 BatchGetItemOutcome (com.amazonaws.services.dynamodbv2.document.BatchGetItemOutcome)5 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)5 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)5