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();
}
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();
}
Aggregations