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());
}
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());
}
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);
}
}
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)));
}
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);
}
Aggregations