Search in sources :

Example 6 with MongoDBRyaDAO

use of org.apache.rya.mongodb.MongoDBRyaDAO 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 7 with MongoDBRyaDAO

use of org.apache.rya.mongodb.MongoDBRyaDAO 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)

Example 8 with MongoDBRyaDAO

use of org.apache.rya.mongodb.MongoDBRyaDAO in project incubator-rya by apache.

the class PipelineQueryIT method setupTest.

@Before
@Override
public void setupTest() throws Exception {
    super.setupTest();
    dao = new MongoDBRyaDAO();
    dao.setConf(conf);
    dao.init();
}
Also used : MongoDBRyaDAO(org.apache.rya.mongodb.MongoDBRyaDAO) Before(org.junit.Before)

Example 9 with MongoDBRyaDAO

use of org.apache.rya.mongodb.MongoDBRyaDAO 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 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(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 " + "{" + "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", new URIImpl("http://Alice"));
        alice.addBinding("age", new NumericLiteralImpl(14, XMLSchema.INTEGER));
        final MapBindingSet bob = new MapBindingSet();
        bob.addBinding("name", new URIImpl("http://Bob"));
        bob.addBinding("age", new NumericLiteralImpl(16, XMLSchema.INTEGER));
        final MapBindingSet charlie = new MapBindingSet();
        charlie.addBinding("name", new URIImpl("http://Charlie"));
        charlie.addBinding("age", new NumericLiteralImpl(12, XMLSchema.INTEGER));
        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) MapBindingSet(org.openrdf.query.impl.MapBindingSet) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) BindingSet(org.openrdf.query.BindingSet) StatefulMongoDBRdfConfiguration(org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration) Statement(org.openrdf.model.Statement) RyaSailRepository(org.apache.rya.rdftriplestore.RyaSailRepository) URIImpl(org.openrdf.model.impl.URIImpl) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) MongoDBRyaDAO(org.apache.rya.mongodb.MongoDBRyaDAO) LiteralImpl(org.openrdf.model.impl.LiteralImpl) NumericLiteralImpl(org.openrdf.model.impl.NumericLiteralImpl) NumericLiteralImpl(org.openrdf.model.impl.NumericLiteralImpl) StatementImpl(org.openrdf.model.impl.StatementImpl) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) MapBindingSet(org.openrdf.query.impl.MapBindingSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with MongoDBRyaDAO

use of org.apache.rya.mongodb.MongoDBRyaDAO in project incubator-rya by apache.

the class RyaSailFactory method getMongoDAO.

/**
 * Connects to MongoDB and creates a MongoDBRyaDAO.
 * @param config - user configuration
 * @return - MongoDBRyaDAO with Indexers configured according to user's specification
 * @throws RyaDAOException if the DAO can't be initialized
 */
public static MongoDBRyaDAO getMongoDAO(MongoDBRdfConfiguration mongoConfig) throws RyaDAOException {
    // Create the MongoClient that will be used by the Sail object's components.
    final MongoClient client = createMongoClient(mongoConfig);
    // Add the Indexer and Optimizer names to the configuration object that are configured to be used.
    ConfigUtils.setIndexers(mongoConfig);
    // Populate the configuration using previously stored Rya Details if this instance uses them.
    try {
        final MongoRyaInstanceDetailsRepository ryaDetailsRepo = new MongoRyaInstanceDetailsRepository(client, mongoConfig.getRyaInstanceName());
        RyaDetailsToConfiguration.addRyaDetailsToConfiguration(ryaDetailsRepo.getRyaInstanceDetails(), mongoConfig);
    } catch (final RyaDetailsRepositoryException e) {
        LOG.info("Instance does not have a rya details collection, skipping.");
    }
    // Set the configuration to the stateful configuration that is used to pass the constructed objects around.
    final StatefulMongoDBRdfConfiguration statefulConfig = new StatefulMongoDBRdfConfiguration(mongoConfig, client);
    final List<MongoSecondaryIndex> indexers = statefulConfig.getInstances(AccumuloRdfConfiguration.CONF_ADDITIONAL_INDEXERS, MongoSecondaryIndex.class);
    statefulConfig.setIndexers(indexers);
    // Create the DAO that is able to interact with MongoDB.
    final MongoDBRyaDAO mongoDao = new MongoDBRyaDAO();
    mongoDao.setConf(statefulConfig);
    mongoDao.init();
    return mongoDao;
}
Also used : MongoClient(com.mongodb.MongoClient) MongoDBRyaDAO(org.apache.rya.mongodb.MongoDBRyaDAO) StatefulMongoDBRdfConfiguration(org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration) MongoRyaInstanceDetailsRepository(org.apache.rya.mongodb.instance.MongoRyaInstanceDetailsRepository) RyaDetailsRepositoryException(org.apache.rya.api.instance.RyaDetailsRepository.RyaDetailsRepositoryException) MongoSecondaryIndex(org.apache.rya.mongodb.MongoSecondaryIndex)

Aggregations

MongoDBRyaDAO (org.apache.rya.mongodb.MongoDBRyaDAO)19 Test (org.junit.Test)13 BindingSet (org.openrdf.query.BindingSet)12 RyaStatement (org.apache.rya.api.domain.RyaStatement)10 RyaType (org.apache.rya.api.domain.RyaType)10 RyaURI (org.apache.rya.api.domain.RyaURI)10 StatementMetadata (org.apache.rya.api.domain.StatementMetadata)10 LiteralImpl (org.openrdf.model.impl.LiteralImpl)10 QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)10 ArrayList (java.util.ArrayList)8 StatefulMongoDBRdfConfiguration (org.apache.rya.mongodb.StatefulMongoDBRdfConfiguration)8 SailRepositoryConnection (org.openrdf.repository.sail.SailRepositoryConnection)7 HashSet (java.util.HashSet)6 StatementMetadataNode (org.apache.rya.indexing.statement.metadata.matching.StatementMetadataNode)6 MongoDBRdfConfiguration (org.apache.rya.mongodb.MongoDBRdfConfiguration)6 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)6 StatementPattern (org.openrdf.query.algebra.StatementPattern)6 ParsedQuery (org.openrdf.query.parser.ParsedQuery)6 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)6 MongoClient (com.mongodb.MongoClient)5