Search in sources :

Example 76 with Table

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

the class GoraDynamoDBCompiler method readMapping.

/**
 * Reads the schema file and converts it into a data structure to be used
 * @param pMapFile
 *          schema file to be mapped into a table
 * @return
 * @throws IOException
 */
@SuppressWarnings("unchecked")
private DynamoDBMapping readMapping(File pMapFile) throws IOException {
    DynamoDBMappingBuilder mappingBuilder = new DynamoDBMappingBuilder();
    try {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(pMapFile);
        if (doc == null || doc.getRootElement() == null)
            throw new GoraException("Unable to load " + MAPPING_FILE + ". Please check its existance!");
        Element root = doc.getRootElement();
        List<Element> tableElements = root.getChildren("table");
        boolean keys = false;
        for (Element tableElement : tableElements) {
            String tableName = tableElement.getAttributeValue("name");
            long readCapacUnits = Long.parseLong(tableElement.getAttributeValue("readcunit"));
            long writeCapacUnits = Long.parseLong(tableElement.getAttributeValue("writecunit"));
            this.packageName = tableElement.getAttributeValue("package");
            mappingBuilder.setProvisionedThroughput(tableName, readCapacUnits, writeCapacUnits);
            log.debug("Table properties have been set for name, package and provisioned throughput.");
            // Retrieving attributes
            List<Element> fieldElements = tableElement.getChildren("attribute");
            for (Element fieldElement : fieldElements) {
                String key = fieldElement.getAttributeValue("key");
                String attributeName = fieldElement.getAttributeValue("name");
                String attributeType = fieldElement.getAttributeValue("type");
                mappingBuilder.addAttribute(tableName, attributeName, attributeType);
                // Retrieving key's features
                if (key != null) {
                    mappingBuilder.setKeySchema(tableName, attributeName, key);
                    keys = true;
                }
            }
            log.debug("Attributes for table '{}' have been read.", tableName);
            if (!keys)
                log.warn("Keys for table '{}' have NOT been set.", tableName);
        }
    } catch (IOException ex) {
        log.error("Error while performing xml mapping.", ex.getMessage());
        throw new RuntimeException(ex);
    } catch (Exception ex) {
        log.error("An error occured whilst reading the xml mapping file!", ex.getMessage());
        throw new IOException(ex);
    }
    return mappingBuilder.build();
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) GoraException(org.apache.gora.util.GoraException) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) Element(org.jdom.Element) DynamoDBMappingBuilder(org.apache.gora.dynamodb.store.DynamoDBMapping.DynamoDBMappingBuilder) IOException(java.io.IOException) Document(org.jdom.Document) IOException(java.io.IOException) GoraException(org.apache.gora.util.GoraException)

Example 77 with Table

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

the class DynamoDBUtils method waitForTableToBecomeAvailable.

/**
 * Waits up to 6 minutes to confirm if a table has been created or not
 *
 * @param awsClient
 * @param tableName
 */
public static void waitForTableToBecomeAvailable(AmazonDynamoDB awsClient, String tableName) {
    LOG.debug("Waiting for {} to become available", tableName);
    long startTime = System.currentTimeMillis();
    long endTime = startTime + WAIT_TIME;
    while (System.currentTimeMillis() < endTime) {
        try {
            Thread.sleep(SLEEP_TIME);
        } catch (Exception e) {
        }
        try {
            DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
            TableDescription tableDescription = awsClient.describeTable(request).getTable();
            String tableStatus = tableDescription.getTableStatus();
            LOG.debug("{} - current state: {}", tableName, tableStatus);
            if (tableStatus.equals(TableStatus.ACTIVE.toString()))
                return;
        } catch (AmazonServiceException ase) {
            if (ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException") == false)
                throw ase;
        }
    }
    throw new RuntimeException("Table " + tableName + " never became active");
}
Also used : AmazonServiceException(com.amazonaws.AmazonServiceException) DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) TableDescription(com.amazonaws.services.dynamodbv2.model.TableDescription) ResourceInUseException(com.amazonaws.services.dynamodbv2.model.ResourceInUseException) AmazonServiceException(com.amazonaws.AmazonServiceException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 78 with Table

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

the class DynamoDBUtils method executeCreateTableRequest.

/**
 * Executes a create table request using the DynamoDB client and waits the
 * default time until it's been created.
 *
 * @param awsClient
 * @param keySchema
 * @param tableName
 * @param proThrou
 */
public static void executeCreateTableRequest(AmazonDynamoDB awsClient, String tableName, ArrayList<KeySchemaElement> keySchema, Map<String, String> attrs, ProvisionedThroughput proThrou) {
    CreateTableRequest createTableRequest = buildCreateTableRequest(tableName, keySchema, proThrou, attrs);
    // use the client to perform the request
    try {
        awsClient.createTable(createTableRequest).getTableDescription();
        // wait for table to become active
        waitForTableToBecomeAvailable(awsClient, tableName);
    } catch (ResourceInUseException ex) {
        LOG.warn("Table '{}' already exists.", tableName);
    } finally {
        LOG.info("Table '{}' is available.", tableName);
    }
}
Also used : ResourceInUseException(com.amazonaws.services.dynamodbv2.model.ResourceInUseException) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest)

Example 79 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 80 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)

Aggregations

Table (com.amazonaws.services.dynamodbv2.document.Table)63 Item (com.amazonaws.services.dynamodbv2.document.Item)40 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)38 AmazonServiceException (com.amazonaws.AmazonServiceException)35 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)31 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)28 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)28 IOException (java.io.IOException)24 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 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)22 TableDescription (com.amazonaws.services.dynamodbv2.model.TableDescription)22 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 Test (org.junit.Test)14 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)10