use of org.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project incubator-rya by apache.
the class GeowaveDirectExample method main.
public static void main(final String[] args) throws Exception {
final Configuration conf = getConf();
conf.set(PrecomputedJoinIndexerConfig.PCJ_STORAGE_TYPE, PrecomputedJoinStorageType.ACCUMULO.name());
conf.setBoolean(ConfigUtils.DISPLAY_QUERY_PLAN, PRINT_QUERIES);
conf.setBoolean(OptionalConfigUtils.USE_GEO, true);
conf.setEnum(OptionalConfigUtils.GEO_INDEXER_TYPE, GeoIndexerType.GEO_WAVE);
log.info("Creating the tables as root.");
SailRepository repository = null;
SailRepositoryConnection conn = null;
try {
log.info("Connecting to Geo Sail Repository.");
final Sail extSail = GeoRyaSailFactory.getInstance(conf);
repository = new SailRepository(extSail);
conn = repository.getConnection();
final long start = System.currentTimeMillis();
log.info("Running SPARQL Example: Add Point and Geo Search with PCJ");
testAddPointAndWithinSearchWithPCJ(conn);
log.info("Running SPARQL Example: Temporal, Freetext, and Geo Search");
testTemporalFreeGeoSearch(conn);
log.info("Running SPARQL Example: Geo, Freetext, and PCJ Search");
testGeoFreetextWithPCJSearch(conn);
log.info("Running SPARQL Example: Delete Geo Data");
testDeleteGeoData(conn);
log.info("TIME: " + (System.currentTimeMillis() - start) / 1000.);
} finally {
log.info("Shutting down");
closeQuietly(conn);
closeQuietly(repository);
}
}
use of org.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project incubator-rya by apache.
the class MongoSpinIT method executeQuery.
private Set<BindingSet> executeQuery(final URL queryFile) throws Exception {
final SailRepositoryConnection conn = repository.getConnection();
try (final InputStream queryIS = queryFile.openStream();
final BufferedReader br = new BufferedReader(new InputStreamReader(queryIS, StandardCharsets.UTF_8))) {
final String query = br.lines().collect(Collectors.joining("\n"));
final TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
final Set<BindingSet> solutions = new HashSet<>();
tupleQuery.evaluate(new AbstractTupleQueryResultHandler() {
@Override
public void handleSolution(final BindingSet bindingSet) throws TupleQueryResultHandlerException {
solutions.add(bindingSet);
}
});
return solutions;
} finally {
closeQuietly(conn);
}
}
use of org.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project incubator-rya by apache.
the class MongoSpinIT method insertDataFile.
private void insertDataFile(final URL dataFile, final String defaultNamespace) throws Exception {
final RDFFormat format = Rio.getParserFormatForFileName(dataFile.getFile()).get();
final SailRepositoryConnection conn = repository.getConnection();
try {
conn.add(dataFile, defaultNamespace, format);
} finally {
closeQuietly(conn);
}
}
use of org.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project incubator-rya by apache.
the class AccumuloLoadStatementsFile method loadStatements.
@Override
public void loadStatements(final String ryaInstanceName, final Path statementsFile, final RDFFormat format) throws InstanceDoesNotExistException, RyaClientException {
requireNonNull(ryaInstanceName);
requireNonNull(statementsFile);
requireNonNull(format);
// 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(statementsFile.toFile(), null, format);
} 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 RepositoryException | RDFParseException | UnsupportedRDFormatException | IOException e) {
log.warn("Exception while loading:", e);
throw new RyaClientException("A problem processing the RDF file 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.eclipse.rdf4j.repository.sail.SailRepositoryConnection in project incubator-rya by apache.
the class MongoBatchUpdatePCJ method updatePCJResults.
private void updatePCJResults(final String ryaInstanceName, final String pcjId, final MongoClient client) throws InstanceDoesNotExistException, PCJDoesNotExistException, RyaClientException {
// Things that have to be closed before we exit.
Sail sail = null;
SailConnection sailConn = null;
try (final PrecomputedJoinStorage pcjStorage = new MongoPcjStorage(client, ryaInstanceName)) {
// Create an instance of Sail backed by the Rya instance.
sail = connectToRya(ryaInstanceName);
final SailRepository sailRepo = new SailRepository(sail);
final SailRepositoryConnection sailRepoConn = sailRepo.getConnection();
// Purge the old results from the PCJ.
try {
pcjStorage.purge(pcjId);
} catch (final PCJStorageException e) {
throw new RyaClientException("Could not batch update PCJ with ID '" + pcjId + "' because the old " + "results could not be purged from it.", e);
}
// Parse the PCJ's SPARQL query.
final PcjMetadata metadata = pcjStorage.getPcjMetadata(pcjId);
final String sparql = metadata.getSparql();
sailConn = sail.getConnection();
final TupleQuery tupleQuery = sailRepoConn.prepareTupleQuery(QueryLanguage.SPARQL, sparql);
// Execute the query.
final List<VisibilityBindingSet> batch = new ArrayList<>(1000);
tupleQuery.evaluate(new AbstractTupleQueryResultHandler() {
@Override
public void handleSolution(final BindingSet bindingSet) throws TupleQueryResultHandlerException {
final VisibilityBindingSet result = new VisibilityBindingSet(bindingSet, "");
log.warn("Visibility information on the binding set is lost during a batch update." + " This can create data leaks.");
batch.add(result);
if (batch.size() == 1000) {
try {
pcjStorage.addResults(pcjId, batch);
} catch (final PCJStorageException e) {
throw new TupleQueryResultHandlerException("Fail to batch load new results into the PCJ with ID '" + pcjId + "'.", e);
}
batch.clear();
}
}
});
if (!batch.isEmpty()) {
pcjStorage.addResults(pcjId, batch);
batch.clear();
}
} catch (final MalformedQueryException | PCJStorageException | SailException | QueryEvaluationException | RepositoryException | TupleQueryResultHandlerException e) {
throw new RyaClientException("Fail to batch load new results into the PCJ with ID '" + pcjId + "'.", e);
} finally {
if (sailConn != null) {
try {
sailConn.close();
} catch (final SailException e) {
log.warn(e.getMessage(), e);
}
}
if (sail != null) {
try {
sail.shutDown();
} catch (final SailException e) {
log.warn(e.getMessage(), e);
}
}
}
}
Aggregations