use of org.apache.rya.rdftriplestore.inference.InferenceEngineException in project incubator-rya by apache.
the class AccumuloExecuteSparqlQuery method executeSparqlQuery.
@Override
public String executeSparqlQuery(final String ryaInstanceName, final String sparqlQuery) throws InstanceDoesNotExistException, RyaClientException {
requireNonNull(ryaInstanceName);
requireNonNull(sparqlQuery);
// Ensure the Rya Instance exists.
if (!instanceExists.exists(ryaInstanceName)) {
throw new InstanceDoesNotExistException(String.format("There is no Rya instance named '%s'.", ryaInstanceName));
}
Sail sail = null;
SailRepository sailRepo = null;
SailRepositoryConnection sailRepoConn = null;
try {
// Get a Sail object that is connected to the Rya instance.
final AccumuloRdfConfiguration ryaConf = getAccumuloConnectionDetails().buildAccumuloRdfConfiguration(ryaInstanceName);
sail = RyaSailFactory.getInstance(ryaConf);
sailRepo = new SailRepository(sail);
sailRepoConn = sailRepo.getConnection();
// Execute the query.
final long start = System.currentTimeMillis();
final TupleQuery tupleQuery = sailRepoConn.prepareTupleQuery(QueryLanguage.SPARQL, sparqlQuery);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final CountingSPARQLResultsCSVWriter handler = new CountingSPARQLResultsCSVWriter(baos);
tupleQuery.evaluate(handler);
final StringBuilder sb = new StringBuilder();
final String newline = "\n";
sb.append("Query Result:").append(newline);
sb.append(new String(baos.toByteArray(), StandardCharsets.UTF_8));
final String seconds = new DecimalFormat("0.0##").format((System.currentTimeMillis() - start) / 1000.0);
sb.append("Retrieved ").append(handler.getCount()).append(" results in ").append(seconds).append(" seconds.");
return sb.toString();
} catch (final SailException | AccumuloException | AccumuloSecurityException | RyaDAOException | InferenceEngineException e) {
throw new RyaClientException("A problem connecting to the Rya instance named '" + ryaInstanceName + "' has caused the query to fail.", e);
} catch (final MalformedQueryException e) {
throw new RyaClientException("There was a problem parsing the supplied query.", e);
} catch (final QueryEvaluationException | TupleQueryResultHandlerException e) {
throw new RyaClientException("There was a problem evaluating the supplied query.", e);
} catch (final RepositoryException e) {
throw new RyaClientException("There was a problem executing the query against the Rya instance named " + ryaInstanceName + ".", e);
} finally {
// Shut it all down.
if (sailRepoConn != null) {
try {
sailRepoConn.close();
} catch (final RepositoryException e) {
log.warn("Couldn't close the SailRepoConnection that is attached to the Rya instance.", e);
}
}
if (sailRepo != null) {
try {
sailRepo.shutDown();
} catch (final RepositoryException e) {
log.warn("Couldn't shut down the SailRepository that is attached to the Rya instance.", e);
}
}
if (sail != null) {
try {
sail.shutDown();
} catch (final SailException e) {
log.warn("Couldn't shut down the Sail that is attached to the Rya instance.", e);
}
}
}
}
use of org.apache.rya.rdftriplestore.inference.InferenceEngineException in project incubator-rya by apache.
the class AccumuloInstall method install.
@Override
public void install(final String instanceName, final InstallConfiguration installConfig) throws DuplicateInstanceNameException, RyaClientException {
requireNonNull(instanceName);
requireNonNull(installConfig);
// Check to see if a Rya instance has already been installed with this name.
if (instanceExists.exists(instanceName)) {
throw new DuplicateInstanceNameException("An instance of Rya has already been installed to this Rya storage " + "with the name '" + instanceName + "'. Try again with a different name.");
}
// Initialize the Rya Details table.
RyaDetails details;
try {
details = initializeRyaDetails(instanceName, installConfig, getConnector().whoami());
} catch (final AlreadyInitializedException e) {
// This can only happen if somebody else installs an instance of Rya with the name between the check and now.
throw new DuplicateInstanceNameException("An instance of Rya has already been installed to this Rya storage " + "with the name '" + instanceName + "'. Try again with a different name.");
} catch (final RyaDetailsRepositoryException e) {
throw new RyaClientException("The RyaDetails couldn't be initialized. Details: " + e.getMessage(), e);
}
// Initialize the rest of the tables used by the Rya instance.
final AccumuloRdfConfiguration ryaConfig = makeRyaConfig(getAccumuloConnectionDetails(), details);
try {
final Sail ryaSail = RyaSailFactory.getInstance(ryaConfig);
ryaSail.shutDown();
} catch (final AccumuloException | AccumuloSecurityException | RyaDAOException | InferenceEngineException e) {
throw new RyaClientException("Could not initialize all of the tables for the new Rya instance. " + "This instance may be left in a bad state.", e);
} catch (final SailException e) {
throw new RyaClientException("Problem shutting down the Sail object used to install Rya.", e);
}
}
use of org.apache.rya.rdftriplestore.inference.InferenceEngineException in project incubator-rya by apache.
the class AccumuloLoadStatements method loadStatements.
@Override
public void loadStatements(final String ryaInstanceName, final Iterable<? extends Statement> statements) throws InstanceDoesNotExistException, RyaClientException {
requireNonNull(ryaInstanceName);
requireNonNull(statements);
// Ensure the Rya Instance exists.
if (!instanceExists.exists(ryaInstanceName)) {
throw new InstanceDoesNotExistException(String.format("There is no Rya instance named '%s'.", ryaInstanceName));
}
Sail sail = null;
SailRepository sailRepo = null;
SailRepositoryConnection sailRepoConn = null;
try {
// Get a Sail object that is connected to the Rya instance.
final AccumuloRdfConfiguration ryaConf = getAccumuloConnectionDetails().buildAccumuloRdfConfiguration(ryaInstanceName);
// RYA-327 should address this hardcoded value.
ryaConf.setFlush(false);
sail = RyaSailFactory.getInstance(ryaConf);
// Load the file.
sailRepo = new SailRepository(sail);
sailRepoConn = sailRepo.getConnection();
sailRepoConn.add(statements);
} catch (final SailException | AccumuloException | AccumuloSecurityException | RyaDAOException | InferenceEngineException e) {
log.warn("Exception while loading:", e);
throw new RyaClientException("A problem connecting to the Rya instance named '" + ryaInstanceName + "' has caused the load to fail.", e);
} catch (final Exception e) {
log.warn("Exception while loading:", e);
throw new RyaClientException("A problem processing the RDF statements has caused the load into Rya instance named " + ryaInstanceName + "to fail.", e);
} finally {
// Shut it all down.
if (sailRepoConn != null) {
try {
sailRepoConn.close();
} catch (final RepositoryException e) {
log.warn("Couldn't close the SailRepoConnection that is attached to the Rya instance.", e);
}
}
if (sailRepo != null) {
try {
sailRepo.shutDown();
} catch (final RepositoryException e) {
log.warn("Couldn't shut down the SailRepository that is attached to the Rya instance.", e);
}
}
if (sail != null) {
try {
sail.shutDown();
} catch (final SailException e) {
log.warn("Couldn't shut down the Sail that is attached to the Rya instance.", e);
}
}
}
}
use of org.apache.rya.rdftriplestore.inference.InferenceEngineException in project incubator-rya by apache.
the class MongoExecuteSparqlQuery method executeSparqlQuery.
@Override
public String executeSparqlQuery(final String ryaInstanceName, final String sparqlQuery) throws InstanceDoesNotExistException, RyaClientException {
requireNonNull(ryaInstanceName);
requireNonNull(sparqlQuery);
// Ensure the Rya Instance exists.
if (!instanceExists.exists(ryaInstanceName)) {
throw new InstanceDoesNotExistException(String.format("There is no Rya instance named '%s'.", ryaInstanceName));
}
Sail sail = null;
SailRepositoryConnection sailRepoConn = null;
try {
// Get a Sail object that is connected to the Rya instance.
final MongoDBRdfConfiguration ryaConf = connectionDetails.build(ryaInstanceName);
sail = RyaSailFactory.getInstance(ryaConf);
final SailRepository sailRepo = new SailRepository(sail);
sailRepoConn = sailRepo.getConnection();
// Execute the query.
final long start = System.currentTimeMillis();
final TupleQuery tupleQuery = sailRepoConn.prepareTupleQuery(QueryLanguage.SPARQL, sparqlQuery);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final CountingSPARQLResultsCSVWriter handler = new CountingSPARQLResultsCSVWriter(baos);
tupleQuery.evaluate(handler);
final long end = System.currentTimeMillis();
// Format and return the result of the query.
final String queryResult = new String(baos.toByteArray(), StandardCharsets.UTF_8);
final String queryDuration = new DecimalFormat("0.0##").format((end - start) / 1000.0);
return "Query Result:\n" + queryResult + "Retrieved " + handler.getCount() + " results in " + queryDuration + " seconds.";
} catch (SailException | RyaDAOException | InferenceEngineException | AccumuloException | AccumuloSecurityException e) {
throw new RyaClientException("Could not create the Sail object used to query the RYA instance.", e);
} catch (final MalformedQueryException | QueryEvaluationException | TupleQueryResultHandlerException | RepositoryException e) {
throw new RyaClientException("Could not execute the SPARQL query.", e);
} finally {
// Close the resources that were opened.
if (sailRepoConn != null) {
try {
sailRepoConn.close();
} catch (final RepositoryException e) {
log.error("Couldn't close the SailRepositoryConnection object.", e);
}
}
if (sail != null) {
try {
sail.shutDown();
} catch (final SailException e) {
log.error("Couldn't close the Sail object.", e);
}
}
}
}
use of org.apache.rya.rdftriplestore.inference.InferenceEngineException in project incubator-rya by apache.
the class MongoInstall method install.
@Override
public void install(final String instanceName, final InstallConfiguration installConfig) throws DuplicateInstanceNameException, RyaClientException {
requireNonNull(instanceName);
requireNonNull(installConfig);
// Check to see if a Rya instance has already been installed with this name.
if (instanceExists.exists(instanceName)) {
throw new DuplicateInstanceNameException("An instance of Rya has already been installed to this Rya storage " + "with the name '" + instanceName + "'. Try again with a different name.");
}
// Initialize the Rya Details table.
final RyaDetails ryaDetails;
try {
ryaDetails = initializeRyaDetails(instanceName, installConfig);
} catch (final AlreadyInitializedException e) {
// This can only happen if somebody else installs an instance of Rya with the name between the check and now.
throw new DuplicateInstanceNameException("An instance of Rya has already been installed to this Rya storage " + "with the name '" + instanceName + "'. Try again with a different name.");
} catch (final RyaDetailsRepositoryException e) {
throw new RyaClientException("The RyaDetails couldn't be initialized. Details: " + e.getMessage(), e);
}
// Initialize the rest of the collections used by the Rya instance.
final MongoDBRdfConfiguration ryaConfig = makeRyaConfig(connectionDetails, ryaDetails);
try {
final Sail ryaSail = RyaSailFactory.getInstance(ryaConfig);
ryaSail.shutDown();
} catch (final AccumuloException | AccumuloSecurityException | RyaDAOException | InferenceEngineException e) {
throw new RyaClientException("Could not initialize all of the tables for the new Rya instance. " + "This instance may be left in a bad state.", e);
} catch (final SailException e) {
throw new RyaClientException("Problem shutting down the Sail object used to install Rya.", e);
}
}
Aggregations