Search in sources :

Example 11 with SailRepositoryConnection

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

the class MongoPCJIndexIT method sparqlQuery_Test.

@Test
public void sparqlQuery_Test() throws Exception {
    // Setup a Rya Client.
    final MongoConnectionDetails connectionDetails = getConnectionDetails();
    final RyaClient ryaClient = MongoRyaClientFactory.build(connectionDetails, getMongoClient());
    final String pcjQuery = "SELECT ?name WHERE {" + " ?name <urn:likes> <urn:icecream> ." + " ?name <urn:hasEyeColor> <urn:blue> ." + " }";
    // Install an instance of Rya and load statements.
    ryaClient.getInstall().install(conf.getRyaInstanceName(), InstallConfiguration.builder().setEnablePcjIndex(true).build());
    ryaClient.getLoadStatements().loadStatements(conf.getRyaInstanceName(), getStatements());
    final String pcjId = ryaClient.getCreatePCJ().createPCJ(conf.getRyaInstanceName(), pcjQuery);
    ryaClient.getBatchUpdatePCJ().batchUpdate(conf.getRyaInstanceName(), pcjId);
    // purge contents of rya triples collection
    getMongoClient().getDatabase(conf.getRyaInstanceName()).getCollection(conf.getTriplesCollectionName()).drop();
    // run the query.  since the triples collection is gone, if the results match, they came from the PCJ index.
    conf.setBoolean(ConfigUtils.USE_PCJ, true);
    conf.setBoolean(ConfigUtils.USE_OPTIMAL_PCJ, true);
    conf.setBoolean(ConfigUtils.DISPLAY_QUERY_PLAN, true);
    final Sail sail = RyaSailFactory.getInstance(conf);
    SailRepositoryConnection conn = new SailRepository(sail).getConnection();
    conn.begin();
    final TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, pcjQuery);
    tupleQuery.setBinding(RdfCloudTripleStoreConfiguration.CONF_QUERYPLAN_FLAG, RdfCloudTripleStoreConstants.VALUE_FACTORY.createLiteral(true));
    final TupleQueryResult rez = tupleQuery.evaluate();
    final Set<BindingSet> results = new HashSet<>();
    while (rez.hasNext()) {
        final BindingSet bs = rez.next();
        results.add(bs);
    }
    // Verify the correct results were loaded into the PCJ table.
    final Set<BindingSet> expectedResults = new HashSet<>();
    MapBindingSet bs = new MapBindingSet();
    bs.addBinding("name", VF.createURI("urn:Alice"));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("name", VF.createURI("urn:Bob"));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("name", VF.createURI("urn:Charlie"));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("name", VF.createURI("urn:David"));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("name", VF.createURI("urn:Eve"));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("name", VF.createURI("urn:Frank"));
    expectedResults.add(bs);
    assertEquals(6, results.size());
    assertEquals(expectedResults, results);
}
Also used : MongoConnectionDetails(org.apache.rya.api.client.mongo.MongoConnectionDetails) MapBindingSet(org.openrdf.query.impl.MapBindingSet) BindingSet(org.openrdf.query.BindingSet) SailRepository(org.openrdf.repository.sail.SailRepository) Sail(org.openrdf.sail.Sail) TupleQuery(org.openrdf.query.TupleQuery) RyaClient(org.apache.rya.api.client.RyaClient) MapBindingSet(org.openrdf.query.impl.MapBindingSet) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) TupleQueryResult(org.openrdf.query.TupleQueryResult) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 12 with SailRepositoryConnection

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

the class MongoStatementMetadataIT method simpleQueryWithoutBindingSetInvalidProperty.

/**
 * Tests if results are filtered correctly using the metadata properties. In
 * this case, the date for the ingested RyaStatement differs from the date
 * specified in the query.
 *
 * @throws MalformedQueryException
 * @throws QueryEvaluationException
 * @throws RyaDAOException
 */
@Test
public void simpleQueryWithoutBindingSetInvalidProperty() throws Exception {
    Sail sail = RyaSailFactory.getInstance(conf);
    MongoDBRyaDAO dao = new MongoDBRyaDAO();
    try {
        dao.setConf(conf);
        dao.init();
        final StatementMetadata metadata = new StatementMetadata();
        metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Doug"));
        metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-02-15"));
        final RyaStatement statement = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata);
        dao.add(statement);
        SailRepositoryConnection conn = new SailRepository(sail).getConnection();
        final TupleQueryResult result = conn.prepareTupleQuery(QueryLanguage.SPARQL, query1).evaluate();
        final List<BindingSet> bsList = new ArrayList<>();
        while (result.hasNext()) {
            bsList.add(result.next());
        }
        assertEquals(0, bsList.size());
        dao.delete(statement, conf);
    } finally {
        dao.destroy();
        sail.shutDown();
    }
}
Also used : MongoDBRyaDAO(org.apache.rya.mongodb.MongoDBRyaDAO) RyaURI(org.apache.rya.api.domain.RyaURI) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) SailRepository(org.openrdf.repository.sail.SailRepository) Sail(org.openrdf.sail.Sail) StatementMetadata(org.apache.rya.api.domain.StatementMetadata) ArrayList(java.util.ArrayList) RyaStatement(org.apache.rya.api.domain.RyaStatement) RyaType(org.apache.rya.api.domain.RyaType) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) TupleQueryResult(org.openrdf.query.TupleQueryResult) Test(org.junit.Test)

Example 13 with SailRepositoryConnection

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

the class MongoStatementMetadataIT method simpleQueryWithoutBindingSet.

@Test
public void simpleQueryWithoutBindingSet() throws Exception {
    Sail sail = RyaSailFactory.getInstance(conf);
    MongoDBRyaDAO dao = new MongoDBRyaDAO();
    try {
        dao.setConf(conf);
        dao.init();
        final StatementMetadata metadata = new StatementMetadata();
        metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Joe"));
        metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));
        final RyaStatement statement = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata);
        dao.add(statement);
        SailRepositoryConnection conn = new SailRepository(sail).getConnection();
        final TupleQueryResult result = conn.prepareTupleQuery(QueryLanguage.SPARQL, query1).evaluate();
        final QueryBindingSet bs = new QueryBindingSet();
        bs.addBinding("x", new LiteralImpl("CoffeeShop"));
        bs.addBinding("y", new LiteralImpl("Joe"));
        final List<BindingSet> bsList = new ArrayList<>();
        while (result.hasNext()) {
            bsList.add(result.next());
        }
        assertEquals(1, bsList.size());
        assertEquals(bs, bsList.get(0));
        dao.delete(statement, conf);
    } finally {
        dao.destroy();
        sail.shutDown();
    }
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) SailRepository(org.openrdf.repository.sail.SailRepository) StatementMetadata(org.apache.rya.api.domain.StatementMetadata) ArrayList(java.util.ArrayList) RyaStatement(org.apache.rya.api.domain.RyaStatement) RyaType(org.apache.rya.api.domain.RyaType) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) MongoDBRyaDAO(org.apache.rya.mongodb.MongoDBRyaDAO) RyaURI(org.apache.rya.api.domain.RyaURI) LiteralImpl(org.openrdf.model.impl.LiteralImpl) Sail(org.openrdf.sail.Sail) TupleQueryResult(org.openrdf.query.TupleQueryResult) Test(org.junit.Test)

Example 14 with SailRepositoryConnection

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

the class MongoStatementMetadataIT method simpleQueryWithBindingSetJoinPropertyToSubject.

/**
 * Tests to see if correct result is passed back when a metadata statement
 * is joined with a StatementPattern statement (i.e. a common variable
 * appears in a StatementPattern statement and a metadata statement).
 * StatementPattern statements have either rdf:subject, rdf:predicate, or
 * rdf:object as the predicate while a metadata statement is any statement
 * in the reified query whose predicate is not rdf:type and not a
 * StatementPattern predicate.
 *
 * @throws MalformedQueryException
 * @throws QueryEvaluationException
 * @throws RyaDAOException
 */
@Test
public void simpleQueryWithBindingSetJoinPropertyToSubject() throws Exception {
    Sail sail = RyaSailFactory.getInstance(conf);
    MongoDBRyaDAO dao = new MongoDBRyaDAO();
    try {
        dao.setConf(conf);
        dao.init();
        final StatementMetadata metadata1 = new StatementMetadata();
        metadata1.addMetadata(new RyaURI("http://createdBy"), new RyaURI("http://Doug"));
        metadata1.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));
        final StatementMetadata metadata2 = new StatementMetadata();
        metadata2.addMetadata(new RyaURI("http://createdBy"), new RyaURI("http://Bob"));
        metadata2.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-02-04"));
        final RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaURI("http://BurgerShack"), new RyaURI("http://context"), "", metadata1);
        final RyaStatement statement2 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://talksTo"), new RyaURI("http://Betty"), new RyaURI("http://context"), "", metadata1);
        final RyaStatement statement3 = new RyaStatement(new RyaURI("http://Fred"), new RyaURI("http://talksTo"), new RyaURI("http://Amanda"), new RyaURI("http://context"), "", metadata1);
        final RyaStatement statement4 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://talksTo"), new RyaURI("http://Wanda"), new RyaURI("http://context"), "", metadata2);
        dao.add(statement1);
        dao.add(statement2);
        dao.add(statement3);
        dao.add(statement4);
        SailRepositoryConnection conn = new SailRepository(sail).getConnection();
        final TupleQueryResult result = conn.prepareTupleQuery(QueryLanguage.SPARQL, query2).evaluate();
        final Set<BindingSet> expected = new HashSet<>();
        final QueryBindingSet expected1 = new QueryBindingSet();
        expected1.addBinding("b", new URIImpl("http://Betty"));
        expected1.addBinding("a", new URIImpl("http://Joe"));
        expected1.addBinding("c", new URIImpl("http://Doug"));
        expected.add(expected1);
        final Set<BindingSet> bsSet = new HashSet<>();
        while (result.hasNext()) {
            bsSet.add(result.next());
        }
        assertEquals(expected, bsSet);
        dao.delete(statement1, conf);
        dao.delete(statement2, conf);
        dao.delete(statement3, conf);
        dao.delete(statement4, conf);
    } finally {
        dao.destroy();
        sail.shutDown();
    }
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) SailRepository(org.openrdf.repository.sail.SailRepository) StatementMetadata(org.apache.rya.api.domain.StatementMetadata) RyaStatement(org.apache.rya.api.domain.RyaStatement) URIImpl(org.openrdf.model.impl.URIImpl) RyaType(org.apache.rya.api.domain.RyaType) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) MongoDBRyaDAO(org.apache.rya.mongodb.MongoDBRyaDAO) RyaURI(org.apache.rya.api.domain.RyaURI) Sail(org.openrdf.sail.Sail) TupleQueryResult(org.openrdf.query.TupleQueryResult) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with SailRepositoryConnection

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

the class MongoStatementMetadataIT method simpleQueryWithBindingSet.

@Test
public void simpleQueryWithBindingSet() throws Exception {
    Sail sail = RyaSailFactory.getInstance(conf);
    MongoDBRyaDAO dao = new MongoDBRyaDAO();
    try {
        dao.setConf(conf);
        dao.init();
        final StatementMetadata metadata = new StatementMetadata();
        metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Joe"));
        metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));
        final RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata);
        final RyaStatement statement2 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("HardwareStore"), new RyaURI("http://context"), "", metadata);
        dao.add(statement1);
        dao.add(statement2);
        SailRepositoryConnection conn = new SailRepository(sail).getConnection();
        final TupleQueryResult result = conn.prepareTupleQuery(QueryLanguage.SPARQL, query1).evaluate();
        final Set<BindingSet> expected = new HashSet<>();
        final QueryBindingSet expected1 = new QueryBindingSet();
        expected1.addBinding("x", new LiteralImpl("CoffeeShop"));
        expected1.addBinding("y", new LiteralImpl("Joe"));
        final QueryBindingSet expected2 = new QueryBindingSet();
        expected2.addBinding("x", new LiteralImpl("HardwareStore"));
        expected2.addBinding("y", new LiteralImpl("Joe"));
        expected.add(expected1);
        expected.add(expected2);
        final Set<BindingSet> bsSet = new HashSet<>();
        while (result.hasNext()) {
            bsSet.add(result.next());
        }
        assertEquals(expected, bsSet);
        dao.delete(statement1, conf);
        dao.delete(statement2, conf);
    } finally {
        dao.destroy();
        sail.shutDown();
    }
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) SailRepository(org.openrdf.repository.sail.SailRepository) StatementMetadata(org.apache.rya.api.domain.StatementMetadata) RyaStatement(org.apache.rya.api.domain.RyaStatement) RyaType(org.apache.rya.api.domain.RyaType) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) MongoDBRyaDAO(org.apache.rya.mongodb.MongoDBRyaDAO) RyaURI(org.apache.rya.api.domain.RyaURI) LiteralImpl(org.openrdf.model.impl.LiteralImpl) Sail(org.openrdf.sail.Sail) TupleQueryResult(org.openrdf.query.TupleQueryResult) 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