use of org.apache.gora.util.GoraException in project gora by apache.
the class HBaseStore method execute.
@Override
public org.apache.gora.query.Result<K, T> execute(Query<K, T> query) throws GoraException {
try {
// check if query.fields is null
query.setFields(getFieldsToQuery(query.getFields()));
if (query.getStartKey() != null && query.getStartKey().equals(query.getEndKey())) {
Get get = new Get(toBytes(query.getStartKey()));
addFields(get, query.getFields());
addTimeRange(get, query);
Result result = table.get(get);
return new HBaseGetResult<>(this, query, result);
} else {
ResultScanner scanner = createScanner(query);
org.apache.gora.query.Result<K, T> result = new HBaseScannerResult<>(this, query, scanner);
return result;
}
} catch (IOException ex) {
throw new GoraException(ex);
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class HBaseStore method deleteSchema.
@Override
public void deleteSchema() throws GoraException {
Admin admin = null;
try {
admin = table.getAdmin();
if (!schemaExists()) {
return;
}
admin.disableTable(mapping.getTable().getTableName());
admin.deleteTable(mapping.getTable().getTableName());
} catch (GoraException e) {
throw e;
} catch (Exception e) {
throw new GoraException(e);
} finally {
try {
if (admin != null) {
admin.close();
}
} catch (IOException e) {
LOG.error("An error occurred whilst closing HBase Admin. ", e);
}
}
}
use of org.apache.gora.util.GoraException in project gora by apache.
the class HBaseStore method deleteByQuery.
@Override
public long deleteByQuery(Query<K, T> query) throws GoraException {
try {
String[] fields = getFieldsToQuery(query.getFields());
// find whether all fields are queried, which means that complete
// rows will be deleted
boolean isAllFields = Arrays.equals(fields, getFields());
org.apache.gora.query.Result<K, T> result = null;
result = query.execute();
ArrayList<Delete> deletes = new ArrayList<>();
while (result.next()) {
Delete delete = new Delete(toBytes(result.getKey()));
deletes.add(delete);
if (!isAllFields) {
addFields(delete, query);
}
}
table.delete(deletes);
return deletes.size();
} 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 HBaseStore method get.
@Override
public T get(K key, String[] fields) throws GoraException {
try {
fields = getFieldsToQuery(fields);
Get get = new Get(toBytes(key));
addFields(get, fields);
Result result = table.get(get);
return newInstance(result, fields);
} 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 HBaseStore 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.
*
* The mapping can be passed as a configuration parameter 'gora.mapping' or taken from
* gora-hbase-mapping.xml (in this order).
*
* @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 {
this.conf = HBaseConfiguration.create(getConf());
InputStream mappingInputStream;
// If there is a mapping definition in the Properties, use it.
if (properties.containsKey(XML_MAPPING_DEFINITION)) {
if (LOG.isTraceEnabled())
LOG.trace(XML_MAPPING_DEFINITION + " = " + properties.getProperty(XML_MAPPING_DEFINITION));
mappingInputStream = IOUtils.toInputStream(properties.getProperty(XML_MAPPING_DEFINITION), (Charset) null);
} else // Otherwise use the configuration from de default file gora-hbase-mapping.xml or whatever
// configured in the key "gora.hbase.mapping.file"
{
String mappingFile = DataStoreFactory.getMappingFile(properties, this, DEFAULT_MAPPING_FILE);
// configurations for mapping.file key
if (mappingFile.equals(DEFAULT_MAPPING_FILE)) {
mappingFile = getConf().get(PARSE_MAPPING_FILE_KEY, DEFAULT_MAPPING_FILE);
}
mappingInputStream = getClass().getClassLoader().getResourceAsStream(mappingFile);
}
mapping = readMapping(mappingInputStream);
filterUtil = new HBaseFilterUtil<>(this.conf);
} catch (FileNotFoundException ex) {
throw new GoraException("Mapping file '" + getConf().get(PARSE_MAPPING_FILE_KEY, DEFAULT_MAPPING_FILE) + "' not found.", ex);
} catch (Exception e) {
throw new GoraException(e);
}
// Set scanner caching option
try {
this.setScannerCaching(Integer.valueOf(DataStoreFactory.findProperty(this.properties, this, SCANNER_CACHING_PROPERTIES_KEY, String.valueOf(SCANNER_CACHING_PROPERTIES_DEFAULT))));
} catch (NumberFormatException e) {
LOG.info("Can not load {} from gora.properties. Setting to default value: {}.", SCANNER_CACHING_PROPERTIES_KEY, SCANNER_CACHING_PROPERTIES_DEFAULT);
// Default value if something is wrong
this.setScannerCaching(SCANNER_CACHING_PROPERTIES_DEFAULT);
}
try {
boolean autoflush = Boolean.valueOf(DataStoreFactory.findProperty(this.properties, this, HBASE_CLIENT_AUTO_FLUSH_PROPERTIES_KEY, String.valueOf(HBASE_CLIENT_AUTO_FLUSH_PROPERTIES_DEFAULT)));
table = new HBaseTableConnection(getConf(), getSchemaName(), autoflush);
} catch (Exception e) {
throw new GoraException(e);
}
if (autoCreateSchema) {
createSchema();
}
}
Aggregations