Search in sources :

Example 6 with SailRepositoryConnection

use of org.openrdf.repository.sail.SailRepositoryConnection in project incubator-rya by apache.

the class EntityDirectExample method main.

public static void main(final String[] args) throws Exception {
    final Configuration conf = getConf();
    conf.setBoolean(ConfigUtils.DISPLAY_QUERY_PLAN, PRINT_QUERIES);
    log.info("Creating the tables as root.");
    SailRepository repository = null;
    SailRepositoryConnection conn = null;
    try {
        log.info("Connecting to Indexing Sail Repository.");
        final Sail extSail = RyaSailFactory.getInstance(conf);
        repository = new SailRepository(extSail);
        conn = repository.getConnection();
        log.info("Running SPARQL Example: Add and Delete");
        testAddAndDelete(conn);
        log.info("Running SAIL/SPARQL Example: Add and Temporal Search");
        testAddAndTemporalSearchWithPCJ(conn);
    } finally {
        log.info("Shutting down");
        closeQuietly(conn);
        closeQuietly(repository);
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) AccumuloIndexingConfiguration(org.apache.rya.indexing.accumulo.AccumuloIndexingConfiguration) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) SailRepository(org.openrdf.repository.sail.SailRepository) Sail(org.openrdf.sail.Sail) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection)

Example 7 with SailRepositoryConnection

use of org.openrdf.repository.sail.SailRepositoryConnection 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);
            }
        }
    }
}
Also used : AccumuloException(org.apache.accumulo.core.client.AccumuloException) RyaClientException(org.apache.rya.api.client.RyaClientException) TupleQueryResultHandlerException(org.openrdf.query.TupleQueryResultHandlerException) SailRepository(org.openrdf.repository.sail.SailRepository) DecimalFormat(java.text.DecimalFormat) TupleQuery(org.openrdf.query.TupleQuery) InferenceEngineException(org.apache.rya.rdftriplestore.inference.InferenceEngineException) RepositoryException(org.openrdf.repository.RepositoryException) InstanceDoesNotExistException(org.apache.rya.api.client.InstanceDoesNotExistException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SailException(org.openrdf.sail.SailException) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Sail(org.openrdf.sail.Sail) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) MalformedQueryException(org.openrdf.query.MalformedQueryException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException)

Example 8 with SailRepositoryConnection

use of org.openrdf.repository.sail.SailRepositoryConnection 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);
            }
        }
    }
}
Also used : AccumuloException(org.apache.accumulo.core.client.AccumuloException) RyaClientException(org.apache.rya.api.client.RyaClientException) SailRepository(org.openrdf.repository.sail.SailRepository) InferenceEngineException(org.apache.rya.rdftriplestore.inference.InferenceEngineException) RepositoryException(org.openrdf.repository.RepositoryException) InstanceDoesNotExistException(org.apache.rya.api.client.InstanceDoesNotExistException) SailException(org.openrdf.sail.SailException) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) InferenceEngineException(org.apache.rya.rdftriplestore.inference.InferenceEngineException) RyaClientException(org.apache.rya.api.client.RyaClientException) SailException(org.openrdf.sail.SailException) InstanceDoesNotExistException(org.apache.rya.api.client.InstanceDoesNotExistException) AccumuloException(org.apache.accumulo.core.client.AccumuloException) RepositoryException(org.openrdf.repository.RepositoryException) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) Sail(org.openrdf.sail.Sail) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException)

Example 9 with SailRepositoryConnection

use of org.openrdf.repository.sail.SailRepositoryConnection in project incubator-rya by apache.

the class RdfController method queryRdf.

@RequestMapping(value = "/queryrdf", method = { RequestMethod.GET, RequestMethod.POST })
public void queryRdf(@RequestParam("query") final String query, @RequestParam(value = RdfCloudTripleStoreConfiguration.CONF_QUERY_AUTH, required = false) String auth, @RequestParam(value = RdfCloudTripleStoreConfiguration.CONF_CV, required = false) final String vis, @RequestParam(value = RdfCloudTripleStoreConfiguration.CONF_INFER, required = false) final String infer, @RequestParam(value = "nullout", required = false) final String nullout, @RequestParam(value = RdfCloudTripleStoreConfiguration.CONF_RESULT_FORMAT, required = false) final String emit, @RequestParam(value = "padding", required = false) final String padding, @RequestParam(value = "callback", required = false) final String callback, final HttpServletRequest request, final HttpServletResponse response) {
    // WARNING: if you add to the above request variables,
    // Be sure to validate and encode since they come from the outside and could contain odd damaging character sequences.
    SailRepositoryConnection conn = null;
    final Thread queryThread = Thread.currentThread();
    auth = StringUtils.arrayToCommaDelimitedString(provider.getUserAuths(request));
    final Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            log.debug("interrupting");
            queryThread.interrupt();
        }
    }, QUERY_TIME_OUT_SECONDS * 1000);
    try {
        final ServletOutputStream os = response.getOutputStream();
        conn = repository.getConnection();
        final Boolean isBlankQuery = StringUtils.isEmpty(query);
        final ParsedOperation operation = QueryParserUtil.parseOperation(QueryLanguage.SPARQL, query, null);
        final Boolean requestedCallback = !StringUtils.isEmpty(callback);
        final Boolean requestedFormat = !StringUtils.isEmpty(emit);
        if (!isBlankQuery) {
            if (operation instanceof ParsedGraphQuery) {
                // Perform Graph Query
                final RDFHandler handler = new RDFXMLWriter(os);
                response.setContentType("text/xml");
                performGraphQuery(query, conn, auth, infer, nullout, handler);
            } else if (operation instanceof ParsedTupleQuery) {
                // Perform Tuple Query
                TupleQueryResultHandler handler;
                if (requestedFormat && emit.equalsIgnoreCase("json")) {
                    handler = new SPARQLResultsJSONWriter(os);
                    response.setContentType("application/json");
                } else {
                    handler = new SPARQLResultsXMLWriter(os);
                    response.setContentType("text/xml");
                }
                performQuery(query, conn, auth, infer, nullout, handler);
            } else if (operation instanceof ParsedUpdate) {
                // Perform Update Query
                performUpdate(query, conn, os, infer, vis);
            } else {
                throw new MalformedQueryException("Cannot process query. Query type not supported.");
            }
        }
        if (requestedCallback) {
            os.print(")");
        }
    } catch (final Exception e) {
        log.error("Error running query", e);
        throw new RuntimeException(e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (final RepositoryException e) {
                log.error("Error closing connection", e);
            }
        }
    }
    timer.cancel();
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ParsedGraphQuery(org.openrdf.query.parser.ParsedGraphQuery) RepositoryException(org.openrdf.repository.RepositoryException) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) SPARQLResultsJSONWriter(org.openrdf.query.resultio.sparqljson.SPARQLResultsJSONWriter) ParsedOperation(org.openrdf.query.parser.ParsedOperation) QueryResultHandlerException(org.openrdf.query.QueryResultHandlerException) RepositoryException(org.openrdf.repository.RepositoryException) TupleQueryResultHandlerException(org.openrdf.query.TupleQueryResultHandlerException) RDFParseException(org.openrdf.rio.RDFParseException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) UpdateExecutionException(org.openrdf.query.UpdateExecutionException) MalformedQueryException(org.openrdf.query.MalformedQueryException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) IOException(java.io.IOException) SPARQLResultsXMLWriter(org.openrdf.query.resultio.sparqlxml.SPARQLResultsXMLWriter) ParsedUpdate(org.openrdf.query.parser.ParsedUpdate) Timer(java.util.Timer) TimerTask(java.util.TimerTask) RDFXMLWriter(org.openrdf.rio.rdfxml.RDFXMLWriter) MalformedQueryException(org.openrdf.query.MalformedQueryException) TupleQueryResultHandler(org.openrdf.query.TupleQueryResultHandler) RDFHandler(org.openrdf.rio.RDFHandler) ParsedTupleQuery(org.openrdf.query.parser.ParsedTupleQuery) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with SailRepositoryConnection

use of org.openrdf.repository.sail.SailRepositoryConnection in project incubator-rya by apache.

the class PcjDocumentsWithMockTest method populatePcj.

@Test
public void populatePcj() throws Exception {
    final RdfCloudTripleStore ryaStore = new RdfCloudTripleStore();
    final MongoDBRyaDAO dao = new MongoDBRyaDAO();
    dao.setConf(new StatefulMongoDBRdfConfiguration(conf, getMongoClient()));
    dao.init();
    ryaStore.setRyaDAO(dao);
    ryaStore.initialize();
    final SailRepositoryConnection ryaConn = new RyaSailRepository(ryaStore).getConnection();
    try {
        // Load some Triples into Rya.
        final Set<Statement> triples = new HashSet<>();
        triples.add(new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://hasAge"), new NumericLiteralImpl(14, XMLSchema.INTEGER)));
        triples.add(new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")));
        triples.add(new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://hasAge"), new NumericLiteralImpl(16, XMLSchema.INTEGER)));
        triples.add(new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")));
        triples.add(new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://hasAge"), new NumericLiteralImpl(12, XMLSchema.INTEGER)));
        triples.add(new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")));
        triples.add(new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://hasAge"), new NumericLiteralImpl(43, XMLSchema.INTEGER)));
        triples.add(new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")));
        for (final Statement triple : triples) {
            ryaConn.add(triple);
        }
        // Create a PCJ table that will include those triples in its results.
        final String sparql = "SELECT ?name ?age " + "{" + "?name <http://hasAge> ?age." + "?name <http://playsSport> \"Soccer\" " + "}";
        final String pcjTableName = new PcjTableNameFactory().makeTableName(conf.getRyaInstanceName(), "testPcj");
        final MongoPcjDocuments pcjs = new MongoPcjDocuments(getMongoClient(), conf.getRyaInstanceName());
        pcjs.createAndPopulatePcj(ryaConn, pcjTableName, sparql);
        // Make sure the cardinality was updated.
        final PcjMetadata metadata = pcjs.getPcjMetadata(pcjTableName);
        assertEquals(4, metadata.getCardinality());
    } finally {
        ryaConn.close();
        ryaStore.shutDown();
    }
}
Also used : RdfCloudTripleStore(org.apache.rya.rdftriplestore.RdfCloudTripleStore) StatefulMongoDBRdfConfiguration(org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration) Statement(org.openrdf.model.Statement) RyaSailRepository(org.apache.rya.rdftriplestore.RyaSailRepository) URIImpl(org.openrdf.model.impl.URIImpl) PcjTableNameFactory(org.apache.rya.indexing.pcj.storage.accumulo.PcjTableNameFactory) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) MongoDBRyaDAO(org.apache.rya.mongodb.MongoDBRyaDAO) NumericLiteralImpl(org.openrdf.model.impl.NumericLiteralImpl) LiteralImpl(org.openrdf.model.impl.LiteralImpl) NumericLiteralImpl(org.openrdf.model.impl.NumericLiteralImpl) StatementImpl(org.openrdf.model.impl.StatementImpl) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

SailRepositoryConnection (org.openrdf.repository.sail.SailRepositoryConnection)58 SailRepository (org.openrdf.repository.sail.SailRepository)41 Sail (org.openrdf.sail.Sail)39 Test (org.junit.Test)33 BindingSet (org.openrdf.query.BindingSet)23 HashSet (java.util.HashSet)17 MapBindingSet (org.openrdf.query.impl.MapBindingSet)15 TupleQueryResult (org.openrdf.query.TupleQueryResult)13 Statement (org.openrdf.model.Statement)11 LiteralImpl (org.openrdf.model.impl.LiteralImpl)9 RepositoryException (org.openrdf.repository.RepositoryException)9 ArrayList (java.util.ArrayList)8 Connector (org.apache.accumulo.core.client.Connector)8 Configuration (org.apache.hadoop.conf.Configuration)8 PrecomputedJoinStorage (org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage)8 URIImpl (org.openrdf.model.impl.URIImpl)8 TupleQuery (org.openrdf.query.TupleQuery)8 RyaClientException (org.apache.rya.api.client.RyaClientException)7 RyaStatement (org.apache.rya.api.domain.RyaStatement)7 AccumuloPcjStorage (org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage)7