use of org.apache.gora.util.GoraException in project gora by apache.
the class RethinkDBStore method put.
/**
* {@inheritDoc}
*/
@Override
public void put(K key, T val) throws GoraException {
if (val.isDirty()) {
try {
boolean isExists = r.db(rethinkDBStoreParameters.getDatabaseName()).table(rethinkDBMapping.getDocumentClass()).getAll(key).count().run(connection, Boolean.class).first();
if (!isExists) {
MapObject<String, Object> document = convertAvroBeanToRethinkDBDocument(key, val);
r.db(rethinkDBStoreParameters.getDatabaseName()).table(rethinkDBMapping.getDocumentClass()).insert(document).run(connection);
} else {
MapObject<String, Object> document = convertAvroBeanToRethinkDBDocument(key, val);
r.db(rethinkDBStoreParameters.getDatabaseName()).table(rethinkDBMapping.getDocumentClass()).get(key).replace(document).run(connection);
}
} catch (Exception e) {
throw new GoraException(e);
}
} else {
if (LOG.isDebugEnabled()) {
LOG.info("Ignored putting persistent bean {} in the store as it is neither " + "new, neither dirty.", new Object[] { val });
}
}
}
use of org.apache.gora.util.GoraException 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();
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class AccumuloStore method put.
@Override
public void put(K key, T val) throws GoraException {
try {
Mutation m = new Mutation(new Text(toBytes(key)));
Schema schema = val.getSchema();
List<Field> fields = schema.getFields();
int count = 0;
for (int i = 0; i < fields.size(); i++) {
if (!val.isDirty(i)) {
continue;
}
Field field = fields.get(i);
Object o = val.get(field.pos());
Pair<Text, Text> col = mapping.fieldMap.get(field.name());
if (col == null) {
throw new GoraException("Please define the gora to accumulo mapping for field " + field.name());
}
switch(field.schema().getType()) {
case MAP:
count = putMap(m, count, field.schema().getValueType(), o, col, field.name());
break;
case ARRAY:
count = putArray(m, count, o, col, field.name());
break;
case // default value of null acts like union with null
UNION:
Schema effectiveSchema = field.schema().getTypes().get(firstNotNullSchemaTypeIndex(field.schema()));
// map and array need to compute qualifier
if (effectiveSchema.getType() == Type.ARRAY) {
count = putArray(m, count, o, col, field.name());
break;
} else if (effectiveSchema.getType() == Type.MAP) {
count = putMap(m, count, effectiveSchema.getValueType(), o, col, field.name());
break;
}
// continue like a regular top-level union
case RECORD:
final SpecificDatumWriter<Object> writer = new SpecificDatumWriter<>(field.schema());
final byte[] byteData = IOUtils.serialize(writer, o);
m.put(col.getFirst(), col.getSecond(), new Value(byteData));
count++;
break;
default:
m.put(col.getFirst(), col.getSecond(), new Value(toBytes(o)));
count++;
}
}
if (count > 0)
try {
getBatchWriter().addMutation(m);
} catch (MutationsRejectedException e) {
LOG.error(e.getMessage(), e);
}
} catch (GoraException e) {
throw e;
} catch (Exception e) {
throw new GoraException(e);
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class AccumuloStore method exists.
@Override
public boolean exists(K key) throws GoraException {
try {
Scanner scanner = new IsolatedScanner(conn.createScanner(mapping.tableName, Authorizations.EMPTY));
Range rowRange = new Range(new Text(toBytes(key)));
scanner.setRange(rowRange);
scanner.clearColumns();
return scanner.iterator().hasNext();
} catch (Exception e) {
throw new GoraException(e);
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class AccumuloStore method initialize.
/**
* Initialize the data store by reading the credentials, setting the client's properties up and
* reading the mapping file. Initialize is called when then the call to
* {@link org.apache.gora.store.DataStoreFactory#createDataStore} is made.
*
* @param keyClass
* @param persistentClass
* @param properties
*/
@Override
public void initialize(Class<K> keyClass, Class<T> persistentClass, Properties properties) throws GoraException {
super.initialize(keyClass, persistentClass, properties);
try {
String mock = DataStoreFactory.findProperty(properties, this, MOCK_PROPERTY, null);
String mappingFile = DataStoreFactory.getMappingFile(properties, this, DEFAULT_MAPPING_FILE);
String user = DataStoreFactory.findProperty(properties, this, USERNAME_PROPERTY, null);
String password = DataStoreFactory.findProperty(properties, this, PASSWORD_PROPERTY, null);
mapping = readMapping(mappingFile);
if (mapping.encoder == null || "".equals(mapping.encoder)) {
encoder = new BinaryEncoder();
} else {
encoder = (Encoder) getClass().getClassLoader().loadClass(mapping.encoder).getDeclaredConstructor().newInstance();
}
AuthenticationToken token = new PasswordToken(password);
if (mock == null || !mock.equals("true")) {
String instance = DataStoreFactory.findProperty(properties, this, INSTANCE_NAME_PROPERTY, null);
String zookeepers = DataStoreFactory.findProperty(properties, this, ZOOKEEPERS_NAME_PROPERTY, null);
conn = new ZooKeeperInstance(instance, zookeepers).getConnector(user, token);
} else {
conn = new MockInstance().getConnector(user, token);
}
credentials = new Credentials(user, token);
if (autoCreateSchema && !schemaExists())
createSchema();
} catch (IOException | InstantiationException | IllegalAccessException | ClassNotFoundException | AccumuloException | AccumuloSecurityException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
throw new GoraException(e);
}
}
Aggregations