Search in sources :

Example 1 with DynamoDBMappingBuilder

use of org.apache.gora.dynamodb.store.DynamoDBMapping.DynamoDBMappingBuilder 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 2 with DynamoDBMappingBuilder

use of org.apache.gora.dynamodb.store.DynamoDBMapping.DynamoDBMappingBuilder in project gora by apache.

the class DynamoDBStore method readMapping.

/** 
   * Reads the schema file and converts it into a data structure to be used
   *
   * @return DynamoDBMapping Object containing all necessary information to
   *         create tables
   * @throws IOException
   */
@SuppressWarnings("unchecked")
private DynamoDBMapping readMapping() throws IOException {
    DynamoDBMappingBuilder mappingBuilder = new DynamoDBMappingBuilder();
    try {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(getClass().getClassLoader().getResourceAsStream(MAPPING_FILE));
        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"));
            mappingBuilder.setProvisionedThroughput(tableName, readCapacUnits, writeCapacUnits);
            LOG.debug("Basic table properties have been set: Name, 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 '" + tableName + "' have been read.");
            if (!keys)
                LOG.warn("Keys for table '" + tableName + "' have NOT been set.");
        }
    } catch (IOException ex) {
        LOG.error("Error while performing xml mapping.", ex.getMessage());
        throw new IOException(ex);
    } catch (Exception ex) {
        LOG.error("Error while performing xml mapping.", ex.getMessage());
        throw new RuntimeException(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) GoraException(org.apache.gora.util.GoraException) AmazonServiceException(com.amazonaws.AmazonServiceException) IOException(java.io.IOException) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)

Aggregations

KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)2 IOException (java.io.IOException)2 DynamoDBMappingBuilder (org.apache.gora.dynamodb.store.DynamoDBMapping.DynamoDBMappingBuilder)2 GoraException (org.apache.gora.util.GoraException)2 Document (org.jdom.Document)2 Element (org.jdom.Element)2 SAXBuilder (org.jdom.input.SAXBuilder)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 ResourceNotFoundException (com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)1