Search in sources :

Example 1 with SimpleMongoDBStorageStrategy

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

the class MongoDBRyaBatchWriterIT method testMongoCollectionFlush.

@Test
public void testMongoCollectionFlush() throws Exception {
    final MongoDBStorageStrategy<RyaStatement> storageStrategy = new SimpleMongoDBStorageStrategy();
    final List<Document> documents = Lists.newArrayList(toDocument(storageStrategy.serialize(statement(1))), toDocument(storageStrategy.serialize(statement(2))), toDocument(storageStrategy.serialize(statement(2))), null, toDocument(storageStrategy.serialize(statement(3))), toDocument(storageStrategy.serialize(statement(3))), toDocument(storageStrategy.serialize(statement(4))));
    final MongoCollectionType mongoCollectionType = new MongoCollectionType(getRyaCollection());
    final MongoDbBatchWriterConfig mongoDbBatchWriterConfig = MongoDbBatchWriterUtils.getMongoDbBatchWriterConfig(conf);
    final MongoDbBatchWriter<Document> mongoDbBatchWriter = new MongoDbBatchWriter<>(mongoCollectionType, mongoDbBatchWriterConfig);
    mongoDbBatchWriter.start();
    mongoDbBatchWriter.addObjectsToQueue(documents);
    mongoDbBatchWriter.flush();
    Thread.sleep(1_000);
    mongoDbBatchWriter.addObjectsToQueue(documents);
    mongoDbBatchWriter.flush();
    Thread.sleep(1_000);
    mongoDbBatchWriter.shutdown();
    assertEquals(4, getRyaCollection().count());
}
Also used : MongoDbBatchWriter(org.apache.rya.mongodb.batch.MongoDbBatchWriter) RyaStatement(org.apache.rya.api.domain.RyaStatement) MongoDbBatchWriterConfig(org.apache.rya.mongodb.batch.MongoDbBatchWriterConfig) Document(org.bson.Document) SimpleMongoDBStorageStrategy(org.apache.rya.mongodb.dao.SimpleMongoDBStorageStrategy) MongoCollectionType(org.apache.rya.mongodb.batch.collection.MongoCollectionType) Test(org.junit.Test)

Example 2 with SimpleMongoDBStorageStrategy

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

the class MongoDBRyaBatchWriterIT method testDbCollectionFlush.

@Test
public void testDbCollectionFlush() throws Exception {
    final MongoDBStorageStrategy<RyaStatement> storageStrategy = new SimpleMongoDBStorageStrategy();
    final List<DBObject> objects = Lists.newArrayList(storageStrategy.serialize(statement(1)), storageStrategy.serialize(statement(2)), storageStrategy.serialize(statement(2)), null, storageStrategy.serialize(statement(3)), storageStrategy.serialize(statement(3)), storageStrategy.serialize(statement(4)));
    final DbCollectionType collectionType = new DbCollectionType(getRyaDbCollection());
    final MongoDbBatchWriterConfig mongoDbBatchWriterConfig = MongoDbBatchWriterUtils.getMongoDbBatchWriterConfig(conf);
    final MongoDbBatchWriter<DBObject> mongoDbBatchWriter = new MongoDbBatchWriter<>(collectionType, mongoDbBatchWriterConfig);
    mongoDbBatchWriter.start();
    mongoDbBatchWriter.addObjectsToQueue(objects);
    mongoDbBatchWriter.flush();
    Thread.sleep(1_000);
    mongoDbBatchWriter.addObjectsToQueue(objects);
    mongoDbBatchWriter.flush();
    Thread.sleep(1_000);
    mongoDbBatchWriter.shutdown();
    assertEquals(4, getRyaDbCollection().count());
}
Also used : MongoDbBatchWriter(org.apache.rya.mongodb.batch.MongoDbBatchWriter) RyaStatement(org.apache.rya.api.domain.RyaStatement) DbCollectionType(org.apache.rya.mongodb.batch.collection.DbCollectionType) MongoDbBatchWriterConfig(org.apache.rya.mongodb.batch.MongoDbBatchWriterConfig) SimpleMongoDBStorageStrategy(org.apache.rya.mongodb.dao.SimpleMongoDBStorageStrategy) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Example 3 with SimpleMongoDBStorageStrategy

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

the class PipelineQueryIT method insert.

private void insert(Resource subject, URI predicate, Value object, int derivationLevel) throws RyaDAOException {
    final RyaStatementBuilder builder = new RyaStatementBuilder();
    builder.setSubject(RdfToRyaConversions.convertResource(subject));
    builder.setPredicate(RdfToRyaConversions.convertURI(predicate));
    builder.setObject(RdfToRyaConversions.convertValue(object));
    final RyaStatement rstmt = builder.build();
    if (derivationLevel > 0) {
        DBObject obj = new SimpleMongoDBStorageStrategy().serialize(builder.build());
        obj.put("derivation_level", derivationLevel);
        getRyaDbCollection().insert(obj);
    } else {
        dao.add(rstmt);
    }
}
Also used : RyaStatementBuilder(org.apache.rya.api.domain.RyaStatement.RyaStatementBuilder) RyaStatement(org.apache.rya.api.domain.RyaStatement) DBObject(com.mongodb.DBObject) SimpleMongoDBStorageStrategy(org.apache.rya.mongodb.dao.SimpleMongoDBStorageStrategy)

Example 4 with SimpleMongoDBStorageStrategy

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

the class PipelineQueryIT method testTriplePipeline.

@Test
public void testTriplePipeline() throws Exception {
    URI alice = VF.createURI("urn:Alice");
    URI bob = VF.createURI("urn:Bob");
    URI eve = VF.createURI("urn:Eve");
    URI friend = VF.createURI("urn:friend");
    URI knows = VF.createURI("urn:knows");
    URI year = VF.createURI("urn:year");
    Literal yearLiteral = VF.createLiteral("2017", XMLSchema.GYEAR);
    final String query = "CONSTRUCT {\n" + "    ?x <urn:knows> ?y .\n" + "    ?x <urn:year> \"2017\"^^<" + XMLSchema.GYEAR + "> .\n" + "} WHERE { ?x <urn:friend> ?y }";
    insert(alice, friend, bob);
    insert(bob, knows, eve);
    insert(eve, knows, alice);
    // Prepare query and convert to pipeline
    QueryRoot queryTree = new QueryRoot(PARSER.parseQuery(query, null).getTupleExpr());
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(getRyaCollection());
    queryTree.visit(visitor);
    // Get pipeline, add triple conversion, and verify that the result is a
    // properly serialized statement
    Assert.assertTrue(queryTree.getArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) queryTree.getArg();
    List<Bson> triplePipeline = pipelineNode.getTriplePipeline(System.currentTimeMillis(), false);
    SimpleMongoDBStorageStrategy strategy = new SimpleMongoDBStorageStrategy();
    List<Statement> results = new LinkedList<>();
    for (Document doc : getRyaCollection().aggregate(triplePipeline)) {
        final DBObject dbo = (DBObject) JSON.parse(doc.toJson());
        RyaStatement rstmt = strategy.deserializeDBObject(dbo);
        Statement stmt = RyaToRdfConversions.convertStatement(rstmt);
        results.add(stmt);
    }
    Assert.assertEquals(2, results.size());
    Assert.assertTrue(results.contains(VF.createStatement(alice, knows, bob)));
    Assert.assertTrue(results.contains(VF.createStatement(alice, year, yearLiteral)));
}
Also used : Statement(org.openrdf.model.Statement) RyaStatement(org.apache.rya.api.domain.RyaStatement) RyaStatement(org.apache.rya.api.domain.RyaStatement) Document(org.bson.Document) URI(org.openrdf.model.URI) DBObject(com.mongodb.DBObject) LinkedList(java.util.LinkedList) Bson(org.bson.conversions.Bson) QueryRoot(org.openrdf.query.algebra.QueryRoot) Literal(org.openrdf.model.Literal) SimpleMongoDBStorageStrategy(org.apache.rya.mongodb.dao.SimpleMongoDBStorageStrategy) Test(org.junit.Test)

Example 5 with SimpleMongoDBStorageStrategy

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

the class MongoDBRyaDAO method init.

@Override
public void init() throws RyaDAOException {
    if (isInitialized.get()) {
        return;
    }
    secondaryIndexers = conf.getAdditionalIndexers();
    for (final MongoSecondaryIndex index : secondaryIndexers) {
        index.setConf(conf);
    }
    db = mongoClient.getDB(conf.get(MongoDBRdfConfiguration.MONGO_DB_NAME));
    coll = db.getCollection(conf.getTriplesCollectionName());
    nameSpaceManager = new SimpleMongoDBNamespaceManager(db.getCollection(conf.getNameSpacesCollectionName()));
    queryEngine = new MongoDBQueryEngine();
    queryEngine.setConf(conf);
    storageStrategy = new SimpleMongoDBStorageStrategy();
    storageStrategy.createIndices(coll);
    for (final MongoSecondaryIndex index : secondaryIndexers) {
        index.init();
    }
    final MongoDbBatchWriterConfig mongoDbBatchWriterConfig = MongoDbBatchWriterUtils.getMongoDbBatchWriterConfig(conf);
    mongoDbBatchWriter = new MongoDbBatchWriter<>(new DbCollectionType(coll), mongoDbBatchWriterConfig);
    try {
        mongoDbBatchWriter.start();
    } catch (final MongoDbBatchWriterException e) {
        throw new RyaDAOException("Error starting MongoDB batch writer", e);
    }
    isInitialized.set(true);
}
Also used : SimpleMongoDBNamespaceManager(org.apache.rya.mongodb.dao.SimpleMongoDBNamespaceManager) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) MongoDbBatchWriterConfig(org.apache.rya.mongodb.batch.MongoDbBatchWriterConfig) DbCollectionType(org.apache.rya.mongodb.batch.collection.DbCollectionType) SimpleMongoDBStorageStrategy(org.apache.rya.mongodb.dao.SimpleMongoDBStorageStrategy) MongoDbBatchWriterException(org.apache.rya.mongodb.batch.MongoDbBatchWriterException)

Aggregations

SimpleMongoDBStorageStrategy (org.apache.rya.mongodb.dao.SimpleMongoDBStorageStrategy)5 RyaStatement (org.apache.rya.api.domain.RyaStatement)4 DBObject (com.mongodb.DBObject)3 MongoDbBatchWriterConfig (org.apache.rya.mongodb.batch.MongoDbBatchWriterConfig)3 Test (org.junit.Test)3 MongoDbBatchWriter (org.apache.rya.mongodb.batch.MongoDbBatchWriter)2 DbCollectionType (org.apache.rya.mongodb.batch.collection.DbCollectionType)2 Document (org.bson.Document)2 LinkedList (java.util.LinkedList)1 RyaStatementBuilder (org.apache.rya.api.domain.RyaStatement.RyaStatementBuilder)1 RyaDAOException (org.apache.rya.api.persist.RyaDAOException)1 MongoDbBatchWriterException (org.apache.rya.mongodb.batch.MongoDbBatchWriterException)1 MongoCollectionType (org.apache.rya.mongodb.batch.collection.MongoCollectionType)1 SimpleMongoDBNamespaceManager (org.apache.rya.mongodb.dao.SimpleMongoDBNamespaceManager)1 Bson (org.bson.conversions.Bson)1 Literal (org.openrdf.model.Literal)1 Statement (org.openrdf.model.Statement)1 URI (org.openrdf.model.URI)1 QueryRoot (org.openrdf.query.algebra.QueryRoot)1