Search in sources :

Example 16 with SailRepository

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

the class RyaDirectExample 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);
    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();
        createPCJ(conf);
        final long start = System.currentTimeMillis();
        log.info("Running SPARQL Example: Add and Delete");
        testAddAndDelete(conn);
        log.info("Running SAIL/SPARQL Example: PCJ Search");
        testPCJSearch(conn);
        log.info("Running SAIL/SPARQL Example: Add and Temporal Search");
        testAddAndTemporalSearchWithPCJ(conn);
        log.info("Running SAIL/SPARQL Example: Add and Free Text Search with PCJ");
        testAddAndFreeTextSearchWithPCJ(conn);
        // 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 Temporal Data");
        testDeleteTemporalData(conn);
        log.info("Running SPARQL Example: Delete Free Text Data");
        testDeleteFreeTextData(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);
    }
}
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 17 with SailRepository

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

the class RyaDirectExample method createPCJ.

// private static void testDeleteGeoData(final SailRepositoryConnection conn)
// throws Exception {
// // Delete all stored points
// final String sparqlDelete = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
// + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
// + "DELETE {\n" //
// + "  ?feature a geo:Feature . "//
// + "  ?feature geo:hasGeometry ?point . "//
// + "  ?point a geo:Point . "//
// + "  ?point geo:asWKT ?wkt . "//
// + "}\n" + "WHERE { \n" + "  ?feature a geo:Feature . "//
// + "  ?feature geo:hasGeometry ?point . "//
// + "  ?point a geo:Point . "//
// + "  ?point geo:asWKT ?wkt . "//
// + "}";//
// 
// final Update deleteUpdate = conn.prepareUpdate(QueryLanguage.SPARQL,
// sparqlDelete);
// deleteUpdate.execute();
// 
// String queryString;
// TupleQuery tupleQuery;
// CountingResultHandler tupleHandler;
// 
// // Find all stored points
// queryString = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>  "//
// + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>  "//
// + "SELECT ?feature ?point ?wkt " //
// + "{" //
// + "  ?feature a geo:Feature . "//
// + "  ?feature geo:hasGeometry ?point . "//
// + "  ?point a geo:Point . "//
// + "  ?point geo:asWKT ?wkt . "//
// + "}";//
// tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
// tupleHandler = new CountingResultHandler();
// tupleQuery.evaluate(tupleHandler);
// log.info("Result count : " + tupleHandler.getCount());
// Validate.isTrue(tupleHandler.getCount() == 0);
// }
private static void createPCJ(final Configuration conf) throws RepositoryException, AccumuloException, AccumuloSecurityException, TableExistsException, PcjException, InferenceEngineException, NumberFormatException, UnknownHostException, SailException, TableNotFoundException {
    final Configuration config = new AccumuloRdfConfiguration(conf);
    config.set(ConfigUtils.USE_PCJ, "false");
    SailRepository repository = null;
    SailRepositoryConnection conn = null;
    try {
        final Sail extSail = RyaSailFactory.getInstance(config);
        repository = new SailRepository(extSail);
        conn = repository.getConnection();
        final String queryString1 = // 
        "" + // 
        "SELECT ?e ?c ?l ?o " + // 
        "{" + // 
        "  ?c a ?e . " + // 
        "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . " + // 
        "  ?e <uri:talksTo> ?o . " + // 
        "}";
        final String queryString2 = // 
        "" + // 
        "SELECT ?e ?c ?l ?o " + // 
        "{" + // 
        "  ?e a ?c . " + // 
        "  ?e <http://www.w3.org/2000/01/rdf-schema#label> ?l . " + // 
        "  ?e <uri:talksTo> ?o . " + // 
        "}";
        URI obj, subclass, talksTo;
        final URI person = new URIImpl("urn:people:alice");
        final URI feature = new URIImpl("urn:feature");
        final URI sub = new URIImpl("uri:entity");
        subclass = new URIImpl("uri:class");
        obj = new URIImpl("uri:obj");
        talksTo = new URIImpl("uri:talksTo");
        conn.add(person, RDF.TYPE, sub);
        conn.add(feature, RDF.TYPE, sub);
        conn.add(sub, RDF.TYPE, subclass);
        conn.add(sub, RDFS.LABEL, new LiteralImpl("label"));
        conn.add(sub, talksTo, obj);
        final String tablename1 = RYA_TABLE_PREFIX + "INDEX_1";
        final String tablename2 = RYA_TABLE_PREFIX + "INDEX_2";
        final Connector accCon = new MockInstance(INSTANCE).getConnector("root", new PasswordToken("".getBytes(StandardCharsets.UTF_8)));
        new PcjTables().createAndPopulatePcj(conn, accCon, tablename1, queryString1, new String[] { "e", "c", "l", "o" }, Optional.<PcjVarOrderFactory>absent());
        new PcjTables().createAndPopulatePcj(conn, accCon, tablename2, queryString2, new String[] { "e", "c", "l", "o" }, Optional.<PcjVarOrderFactory>absent());
    } catch (final RyaDAOException e) {
        throw new Error("While creating PCJ tables.", e);
    } finally {
        closeQuietly(conn);
        closeQuietly(repository);
    }
}
Also used : Connector(org.apache.accumulo.core.client.Connector) 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) URIImpl(org.openrdf.model.impl.URIImpl) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) URI(org.openrdf.model.URI) LiteralImpl(org.openrdf.model.impl.LiteralImpl) PasswordToken(org.apache.accumulo.core.client.security.tokens.PasswordToken) MockInstance(org.apache.accumulo.core.client.mock.MockInstance) Sail(org.openrdf.sail.Sail) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) PcjTables(org.apache.rya.indexing.pcj.storage.accumulo.PcjTables)

Example 18 with SailRepository

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

the class ConformanceTest method run.

@Override
public int run(final String[] args) throws Exception {
    // Validate command
    if (args.length < 1 || args.length > 2) {
        System.out.println("Usage:\n");
        System.out.println("\tConformanceTest [configuration options] " + "<test-file> <temp-dir>\n");
        System.out.println("to load test data from an RDF file " + "(configuration property " + MRUtils.FORMAT_PROP + " specifies the format, default RDF/XML); or\n");
        System.out.println("\tConformanceTest [configuration options] <temp-dir>\n");
        System.out.println("to load test data from a Rya instance (specified " + "using standard configuration properties).\n");
        System.out.println("For each test given, run the reasoner over the " + "premise ontology using a temporary Mini Accumulo instance " + "at <temp-dir>, then report conformance results.");
        System.exit(1);
    }
    final Set<Value> conformanceTestURIs = new HashSet<>();
    Collection<OwlTest> conformanceTests = new LinkedList<>();
    final List<OwlTest> successes = new LinkedList<>();
    final List<OwlTest> failures = new LinkedList<>();
    final Configuration conf = getConf();
    Repository repo;
    File workingDir;
    // If tests are in a file, stick them in a repository for querying
    if (args.length == 2) {
        workingDir = new File(PathUtils.clean(args[1]));
        RDFFormat inputFormat = RDFFormat.RDFXML;
        final String formatString = conf.get(MRUtils.FORMAT_PROP);
        if (formatString != null) {
            inputFormat = RDFFormat.valueOf(formatString);
        }
        repo = new SailRepository(new MemoryStore());
        repo.initialize();
        final RepositoryConnection conn = repo.getConnection();
        FileInputStream fileInput = new FileInputStream(PathUtils.clean(args[0]));
        conn.add(fileInput, "", inputFormat);
        fileInput.close();
        conn.close();
    } else // Otherwise, get a Rya repository
    {
        workingDir = new File(PathUtils.clean(args[0]));
        repo = MRReasoningUtils.getRepository(conf);
        repo.initialize();
    }
    // Query for the tests we're interested in
    final RepositoryConnection conn = repo.getConnection();
    conformanceTestURIs.addAll(getTestURIs(conn, TEST_INCONSISTENCY));
    conformanceTestURIs.addAll(getTestURIs(conn, TEST_CONSISTENCY));
    conformanceTestURIs.addAll(getTestURIs(conn, TEST_ENTAILMENT));
    conformanceTestURIs.addAll(getTestURIs(conn, TEST_NONENTAILMENT));
    conformanceTests = getTests(conn, conformanceTestURIs);
    conn.close();
    repo.shutDown();
    // Set up a MiniAccumulo cluster and set up conf to connect to it
    final String username = "root";
    final String password = "root";
    final MiniAccumuloCluster mini = new MiniAccumuloCluster(workingDir, password);
    mini.start();
    conf.set(MRUtils.AC_INSTANCE_PROP, mini.getInstanceName());
    conf.set(MRUtils.AC_ZK_PROP, mini.getZooKeepers());
    conf.set(MRUtils.AC_USERNAME_PROP, username);
    conf.set(MRUtils.AC_PWD_PROP, password);
    conf.setBoolean(MRUtils.AC_MOCK_PROP, false);
    conf.set(MRUtils.TABLE_PREFIX_PROPERTY, "temp_");
    // Run the conformance tests
    int result;
    for (final OwlTest test : conformanceTests) {
        System.out.println(test.uri);
        result = runTest(conf, args, test);
        if (result != 0) {
            return result;
        }
        if (test.success) {
            successes.add(test);
            System.out.println("(SUCCESS)");
        } else {
            failures.add(test);
            System.out.println("(FAIL)");
        }
    }
    mini.stop();
    System.out.println("\n" + successes.size() + " successful tests:");
    for (final OwlTest test : successes) {
        System.out.println("\t[SUCCESS] " + test.type() + " " + test.name);
    }
    System.out.println("\n" + failures.size() + " failed tests:");
    for (final OwlTest test : failures) {
        System.out.println("\t[FAIL] " + test.type() + " " + test.name);
        System.out.println("\t\t(" + test.description + ")");
        for (final Statement triple : test.error) {
            if (test.types.contains(TEST_ENTAILMENT)) {
                System.out.println("\t\tExpected: " + triple);
            } else if (test.types.contains(TEST_NONENTAILMENT)) {
                System.out.println("\t\tUnexpected: " + triple);
            }
        }
    }
    return 0;
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) Configuration(org.apache.hadoop.conf.Configuration) SailRepository(org.openrdf.repository.sail.SailRepository) Statement(org.openrdf.model.Statement) MiniAccumuloCluster(org.apache.accumulo.minicluster.MiniAccumuloCluster) LinkedList(java.util.LinkedList) FileInputStream(java.io.FileInputStream) MemoryStore(org.openrdf.sail.memory.MemoryStore) SailRepository(org.openrdf.repository.sail.SailRepository) Repository(org.openrdf.repository.Repository) Value(org.openrdf.model.Value) File(java.io.File) HashSet(java.util.HashSet) RDFFormat(org.openrdf.rio.RDFFormat)

Example 19 with SailRepository

use of org.openrdf.repository.sail.SailRepository 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);
            }
        }
    }
}
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) 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) MongoDBRdfConfiguration(org.apache.rya.mongodb.MongoDBRdfConfiguration)

Example 20 with SailRepository

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

the class MongoLoadStatementsFile 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;
    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();
        // Load the file.
        sailRepoConn.add(statementsFile.toFile(), null, format);
    } catch (SailException | RyaDAOException | InferenceEngineException | AccumuloException | AccumuloSecurityException e) {
        throw new RyaClientException("Could not load statements into Rya because of a problem while creating the Sail object.", e);
    } catch (RDFParseException | RepositoryException | IOException e) {
        throw new RyaClientException("Could not load the statements into Rya.", 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);
            }
        }
    }
}
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) IOException(java.io.IOException) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) Sail(org.openrdf.sail.Sail) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) MongoDBRdfConfiguration(org.apache.rya.mongodb.MongoDBRdfConfiguration) RDFParseException(org.openrdf.rio.RDFParseException)

Aggregations

SailRepository (org.openrdf.repository.sail.SailRepository)64 Sail (org.openrdf.sail.Sail)45 SailRepositoryConnection (org.openrdf.repository.sail.SailRepositoryConnection)41 Test (org.junit.Test)27 BindingSet (org.openrdf.query.BindingSet)14 RepositoryException (org.openrdf.repository.RepositoryException)14 TupleQueryResult (org.openrdf.query.TupleQueryResult)13 AccumuloRdfConfiguration (org.apache.rya.accumulo.AccumuloRdfConfiguration)11 TupleQuery (org.openrdf.query.TupleQuery)10 Configuration (org.apache.hadoop.conf.Configuration)9 SailException (org.openrdf.sail.SailException)9 HashSet (java.util.HashSet)8 ArrayList (java.util.ArrayList)7 AccumuloException (org.apache.accumulo.core.client.AccumuloException)7 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)7 RyaClientException (org.apache.rya.api.client.RyaClientException)7 RyaDAOException (org.apache.rya.api.persist.RyaDAOException)7 Repository (org.openrdf.repository.Repository)7 IOException (java.io.IOException)6 RyaURI (org.apache.rya.api.domain.RyaURI)6