Search in sources :

Example 1 with Table

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;
}
Also used : ScanRequest(com.amazonaws.services.dynamodbv2.model.ScanRequest) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) ScanResult(com.amazonaws.services.dynamodbv2.model.ScanResult) HashMap(java.util.HashMap)

Example 2 with Table

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

Example 3 with Table

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());
    }
}
Also used : UnDefType(org.openhab.core.types.UnDefType) State(org.openhab.core.types.State) AmazonClientException(com.amazonaws.AmazonClientException) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) Date(java.util.Date)

Example 4 with Table

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

Example 5 with Table

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

Aggregations

Table (com.amazonaws.services.dynamodbv2.document.Table)66 Item (com.amazonaws.services.dynamodbv2.document.Item)43 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)42 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)37 AmazonServiceException (com.amazonaws.AmazonServiceException)35 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)28 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)28 IOException (java.io.IOException)25 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)23 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)23 ResourceNotFoundException (com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)23 HashMap (java.util.HashMap)23 Test (org.testng.annotations.Test)23 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)22 TableDescription (com.amazonaws.services.dynamodbv2.model.TableDescription)22 UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)18 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)17 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)17 ArrayList (java.util.ArrayList)17 AmazonClientException (com.amazonaws.AmazonClientException)14