}
@SuppressWarnings("unchecked")
private HBaseMapping readMapping(String filename) throws IOException {
HBaseMappingBuilder mappingBuilder = new HBaseMappingBuilder();
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(getClass().getClassLoader()
.getResourceAsStream(filename));
Element root = doc.getRootElement();
List<Element> tableElements = root.getChildren("table");
for(Element tableElement : tableElements) {
String tableName = tableElement.getAttributeValue("name");
List<Element> fieldElements = tableElement.getChildren("family");
for(Element fieldElement : fieldElements) {
String familyName = fieldElement.getAttributeValue("name");
String compression = fieldElement.getAttributeValue("compression");
String blockCache = fieldElement.getAttributeValue("blockCache");
String blockSize = fieldElement.getAttributeValue("blockSize");
String bloomFilter = fieldElement.getAttributeValue("bloomFilter");
String maxVersions = fieldElement.getAttributeValue("maxVersions");
String timeToLive = fieldElement.getAttributeValue("timeToLive");
String inMemory = fieldElement.getAttributeValue("inMemory");
mappingBuilder.addFamilyProps(tableName, familyName, compression,
blockCache, blockSize, bloomFilter, maxVersions, timeToLive,
inMemory);
}
}
List<Element> classElements = root.getChildren("class");
for(Element classElement: classElements) {
if(classElement.getAttributeValue("keyClass").equals(
keyClass.getCanonicalName())
&& classElement.getAttributeValue("name").equals(
persistentClass.getCanonicalName())) {
LOG.debug("Keyclass and nameclass match.");
String tableNameFromMapping = classElement.getAttributeValue("table");
String tableName = getSchemaName(tableNameFromMapping, persistentClass);
//tableNameFromMapping could be null here
if (!tableName.equals(tableNameFromMapping)) {
//TODO this might not be the desired behavior as the user might have actually made a mistake.
LOG.warn("Mismatching schema's names. Mappingfile schema: '" + tableNameFromMapping
+ "'. PersistentClass schema's name: '" + tableName + "'"
+ "Assuming they are the same.");
if (tableNameFromMapping != null) {
mappingBuilder.renameTable(tableNameFromMapping, tableName);
}
}
mappingBuilder.setTableName(tableName);
List<Element> fields = classElement.getChildren("field");
for(Element field:fields) {
String fieldName = field.getAttributeValue("name");
String family = field.getAttributeValue("family");
String qualifier = field.getAttributeValue("qualifier");
mappingBuilder.addField(fieldName, family, qualifier);
mappingBuilder.addColumnFamily(tableName, family);
}
//we found a matching key and value class definition,
//do not continue on other class definitions
break;
} else {
LOG.error("KeyClass in gora-hbase-mapping is not the same as the one in the databean.");
}
}
} catch (MalformedURLException ex) {
LOG.error("Error while trying to read the mapping file {}. "
+ "Expected to be in the classpath "
+ "(ClassLoader#getResource(java.lang.String)).",
filename) ;
LOG.error("Actual classpath = {}", Arrays.asList(
((URLClassLoader) getClass().getClassLoader()).getURLs()));
throw ex ;
} catch(IOException ex) {
LOG.error(ex.getMessage());
LOG.error(ex.getStackTrace().toString());
throw ex;
} catch(Exception ex) {
LOG.error(ex.getMessage());
LOG.error(ex.getStackTrace().toString());
throw new IOException(ex);
}
return mappingBuilder.build();
}