Search in sources :

Example 6 with SailRepositoryConnection

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

the class RulesetCopyIT method runQuery.

private Set<BindingSet> runQuery(final String query, final Configuration conf) throws Exception {
    SailRepository repository = null;
    SailRepositoryConnection conn = null;
    try {
        final Sail extSail = RyaSailFactory.getInstance(conf);
        repository = new SailRepository(extSail);
        conn = repository.getConnection();
        final ResultHandler handler = new ResultHandler();
        final TupleQuery tq = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
        tq.evaluate(handler);
        return handler.getSolutions();
    } finally {
        if (conn != null) {
            conn.close();
        }
        if (repository != null) {
            repository.shutDown();
        }
    }
}
Also used : SailRepository(org.eclipse.rdf4j.repository.sail.SailRepository) Sail(org.eclipse.rdf4j.sail.Sail) TupleQuery(org.eclipse.rdf4j.query.TupleQuery) TupleQueryResultHandler(org.eclipse.rdf4j.query.TupleQueryResultHandler) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection)

Example 7 with SailRepositoryConnection

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

the class PcjDocumentsIntegrationTest method createAndPopulatePcj.

/**
 * Ensure the method that creates a new PCJ table, scans Rya for matches, and
 * stores them in the PCJ table works.
 * <p>
 * The method being tested is: {@link PcjTables#createAndPopulatePcj(RepositoryConnection, Connector, String, String, String[], Optional)}
 */
@Test
public void createAndPopulatePcj() throws Exception {
    final MongoDBRyaDAO dao = new MongoDBRyaDAO();
    dao.setConf(new StatefulMongoDBRdfConfiguration(conf, getMongoClient()));
    dao.init();
    final RdfCloudTripleStore<StatefulMongoDBRdfConfiguration> ryaStore = new RdfCloudTripleStore<>();
    ryaStore.setRyaDAO(dao);
    ryaStore.initialize();
    final SailRepositoryConnection ryaConn = new RyaSailRepository(ryaStore).getConnection();
    ryaConn.begin();
    try {
        // Load some Triples into Rya.
        final Set<Statement> triples = new HashSet<>();
        triples.add(VF.createStatement(VF.createIRI("http://Alice"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(14))));
        triples.add(VF.createStatement(VF.createIRI("http://Alice"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")));
        triples.add(VF.createStatement(VF.createIRI("http://Bob"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(16))));
        triples.add(VF.createStatement(VF.createIRI("http://Bob"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")));
        triples.add(VF.createStatement(VF.createIRI("http://Charlie"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(12))));
        triples.add(VF.createStatement(VF.createIRI("http://Charlie"), VF.createIRI("http://playsSport"), VF.createLiteral("Soccer")));
        triples.add(VF.createStatement(VF.createIRI("http://Eve"), VF.createIRI("http://hasAge"), VF.createLiteral(BigInteger.valueOf(43))));
        triples.add(VF.createStatement(VF.createIRI("http://Eve"), VF.createIRI("http://playsSport"), VF.createLiteral("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 " + "{" + "FILTER(?age < 30) ." + "?name <http://hasAge> ?age." + "?name <http://playsSport> \"Soccer\" " + "}";
        final String pcjTableName = "testPcj";
        // Create and populate the PCJ table.
        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(3, metadata.getCardinality());
        // Scan Accumulo for the stored results.
        final Collection<BindingSet> fetchedResults = loadPcjResults(pcjTableName);
        // Ensure the expected results match those that were stored.
        final MapBindingSet alice = new MapBindingSet();
        alice.addBinding("name", VF.createIRI("http://Alice"));
        alice.addBinding("age", VF.createLiteral(BigInteger.valueOf(14)));
        final MapBindingSet bob = new MapBindingSet();
        bob.addBinding("name", VF.createIRI("http://Bob"));
        bob.addBinding("age", VF.createLiteral(BigInteger.valueOf(16)));
        final MapBindingSet charlie = new MapBindingSet();
        charlie.addBinding("name", VF.createIRI("http://Charlie"));
        charlie.addBinding("age", VF.createLiteral(BigInteger.valueOf(12)));
        final Set<BindingSet> expected = Sets.<BindingSet>newHashSet(alice, bob, charlie);
        assertEquals(expected, fetchedResults);
    } finally {
        ryaConn.close();
        ryaStore.shutDown();
    }
}
Also used : RdfCloudTripleStore(org.apache.rya.rdftriplestore.RdfCloudTripleStore) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) MapBindingSet(org.eclipse.rdf4j.query.impl.MapBindingSet) StatefulMongoDBRdfConfiguration(org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration) Statement(org.eclipse.rdf4j.model.Statement) RyaSailRepository(org.apache.rya.rdftriplestore.RyaSailRepository) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection) MongoDBRyaDAO(org.apache.rya.mongodb.MongoDBRyaDAO) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) MapBindingSet(org.eclipse.rdf4j.query.impl.MapBindingSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with SailRepositoryConnection

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

the class CreateDeleteIT method loadData.

private String loadData(final String sparql, final Collection<Statement> statements) throws Exception {
    requireNonNull(sparql);
    requireNonNull(statements);
    // Register the PCJ with Rya.
    final RyaClient ryaClient = AccumuloRyaClientFactory.build(createConnectionDetails(), getAccumuloConnector());
    final String pcjId = ryaClient.getCreatePCJ().createPCJ(getRyaInstanceName(), sparql, Sets.newHashSet());
    // Write the data to Rya.
    final SailRepositoryConnection ryaConn = getRyaSailRepository().getConnection();
    ryaConn.begin();
    ryaConn.add(statements);
    ryaConn.commit();
    ryaConn.close();
    // Wait for the Fluo application to finish computing the end result.
    getMiniFluo().waitForObservers();
    // The PCJ Id is the topic name the results will be written to.
    return pcjId;
}
Also used : RyaClient(org.apache.rya.api.client.RyaClient) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection)

Example 9 with SailRepositoryConnection

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

the class InputIT method historicThenStreamedResults.

/**
 * Simulates the case where a Triple is added to Rya, a new query that includes
 * that triple as a historic match is inserted into Fluo, and then some new
 * triple that matches the query is streamed into Fluo. The query's results
 * must include both the historic result and the newly streamed result.
 */
@Test
public void historicThenStreamedResults() throws Exception {
    // A query that finds people who talk to Eve and work at Chipotle.
    final String sparql = "SELECT ?x WHERE { " + "?x <http://talksTo> <http://Eve>. " + "?x <http://worksAt> <http://Chipotle>." + "}";
    // Triples that are loaded into Rya before the PCJ is created.
    final ValueFactory vf = SimpleValueFactory.getInstance();
    final Set<Statement> historicTriples = Sets.newHashSet(vf.createStatement(vf.createIRI("http://Alice"), vf.createIRI("http://talksTo"), vf.createIRI("http://Eve")), vf.createStatement(vf.createIRI("http://Alice"), vf.createIRI("http://worksAt"), vf.createIRI("http://Chipotle")));
    // Triples that will be streamed into Fluo after the PCJ has been created.
    final Set<RyaStatement> streamedTriples = Sets.newHashSet(new RyaStatement(new RyaIRI("http://Frank"), new RyaIRI("http://talksTo"), new RyaIRI("http://Eve")), new RyaStatement(new RyaIRI("http://Frank"), new RyaIRI("http://worksAt"), new RyaIRI("http://Chipotle")));
    // Load the historic data into Rya.
    final SailRepositoryConnection ryaConn = super.getRyaSailRepository().getConnection();
    for (final Statement triple : historicTriples) {
        ryaConn.add(triple);
    }
    ryaConn.close();
    // Create the PCJ table.
    final Connector accumuloConn = super.getAccumuloConnector();
    final PrecomputedJoinStorage pcjStorage = new AccumuloPcjStorage(accumuloConn, getRyaInstanceName());
    final String pcjId = pcjStorage.createPcj(sparql);
    try (FluoClient fluoClient = FluoFactory.newClient(super.getFluoConfiguration())) {
        // Tell the Fluo app to maintain the PCJ.
        new CreateFluoPcj().withRyaIntegration(pcjId, pcjStorage, fluoClient, accumuloConn, getRyaInstanceName());
        // Ensure Alice is a match.
        super.getMiniFluo().waitForObservers();
        final Set<BindingSet> expected = new HashSet<>();
        MapBindingSet bs = new MapBindingSet();
        bs.addBinding("x", vf.createIRI("http://Alice"));
        expected.add(bs);
        Set<BindingSet> results = new HashSet<>();
        try (CloseableIterator<BindingSet> resultsIt = pcjStorage.listResults(pcjId)) {
            while (resultsIt.hasNext()) {
                results.add(resultsIt.next());
            }
        }
        assertEquals(expected, results);
        // Stream the data into Fluo.
        new InsertTriples().insert(fluoClient, streamedTriples, Optional.absent());
        // Verify the end results of the query also include Frank.
        super.getMiniFluo().waitForObservers();
        bs = new MapBindingSet();
        bs.addBinding("x", vf.createIRI("http://Frank"));
        expected.add(bs);
        results = new HashSet<>();
        try (CloseableIterator<BindingSet> resultsIt = pcjStorage.listResults(pcjId)) {
            while (resultsIt.hasNext()) {
                results.add(resultsIt.next());
            }
        }
        assertEquals(expected, results);
    }
}
Also used : Connector(org.apache.accumulo.core.client.Connector) BindingSet(org.eclipse.rdf4j.query.BindingSet) MapBindingSet(org.eclipse.rdf4j.query.impl.MapBindingSet) FluoClient(org.apache.fluo.api.client.FluoClient) AccumuloPcjStorage(org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage) RyaIRI(org.apache.rya.api.domain.RyaIRI) InsertTriples(org.apache.rya.indexing.pcj.fluo.api.InsertTriples) RyaStatement(org.apache.rya.api.domain.RyaStatement) Statement(org.eclipse.rdf4j.model.Statement) RyaStatement(org.apache.rya.api.domain.RyaStatement) CreateFluoPcj(org.apache.rya.indexing.pcj.fluo.api.CreateFluoPcj) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection) PrecomputedJoinStorage(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage) MapBindingSet(org.eclipse.rdf4j.query.impl.MapBindingSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with SailRepositoryConnection

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

the class QueryIT method addStatementsAndWait.

private void addStatementsAndWait(final Collection<Statement> statements) throws RepositoryException, Exception {
    // Write the data to Rya.
    final SailRepositoryConnection ryaConn = super.getRyaSailRepository().getConnection();
    ryaConn.begin();
    ryaConn.add(statements);
    ryaConn.commit();
    ryaConn.close();
    // Wait for the Fluo application to finish computing the end result.
    super.getMiniFluo().waitForObservers();
}
Also used : SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection)

Aggregations

SailRepositoryConnection (org.eclipse.rdf4j.repository.sail.SailRepositoryConnection)65 SailRepository (org.eclipse.rdf4j.repository.sail.SailRepository)44 Sail (org.eclipse.rdf4j.sail.Sail)37 Test (org.junit.Test)35 BindingSet (org.eclipse.rdf4j.query.BindingSet)24 HashSet (java.util.HashSet)17 MapBindingSet (org.eclipse.rdf4j.query.impl.MapBindingSet)15 TupleQueryResult (org.eclipse.rdf4j.query.TupleQueryResult)13 Statement (org.eclipse.rdf4j.model.Statement)11 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)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 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)8 RyaStatement (org.apache.rya.api.domain.RyaStatement)7 AccumuloPcjStorage (org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage)7 StatefulMongoDBRdfConfiguration (org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration)7 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)7 RyaIRI (org.apache.rya.api.domain.RyaIRI)6