Java tutorial
/******************************************************************************* * Copyright (c) 2010-2014, Daniel Stein, Gabor Szarnyas, Istvan Rath and Daniel Varro * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Gabor Szarnyas - initial API and implementation for 4store * Daniel Stein - implementation for Mongo *******************************************************************************/ package eu.mondo.driver.mongo; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.UnknownHostException; import org.jongo.Jongo; import org.jongo.MongoCollection; import org.jongo.MongoCursor; import org.jongo.marshall.jackson.JacksonMapper; import org.openrdf.model.Statement; import org.openrdf.query.parser.sparql.ast.ParseException; import org.openrdf.rio.RDFFormat; import org.openrdf.rio.RDFHandler; import org.openrdf.rio.RDFHandlerException; import org.openrdf.rio.RDFParseException; import org.openrdf.rio.RDFParser; import org.openrdf.rio.Rio; import com.fasterxml.jackson.databind.MapperFeature; import com.mongodb.DB; import com.mongodb.MongoClient; import eu.mondo.driver.mongo.util.MStatement; import eu.mondo.driver.mongo.util.Node; import eu.mondo.driver.mongo.util.StatementModule; /** * * @author Dniel Stein * @author Gbor Szrnyas * */ public class MongoGraphClient { DB db; Jongo jongo; MongoCollection collection; MongoCollection nodes; protected final String dbName; protected final String collectionName; protected boolean showCommandOutput = false; protected boolean showUpdateCommands = false; // the default RDF format is Turtle protected RDFFormat rdfFormat = RDFFormat.TURTLE; public MongoGraphClient(final String connectionString) throws UnknownHostException, ParseException { String[] split = connectionString.split("/"); if (split.length != 2) { throw new ParseException( "Connection String not in the correct format ([dbName]/[collectionName]): " + connectionString); } this.dbName = split[0]; this.collectionName = split[1]; db = new MongoClient().getDB(dbName); jongo = new Jongo(db, new JacksonMapper.Builder().registerModule(new StatementModule()) .enable(MapperFeature.AUTO_DETECT_GETTERS).build()); collection = jongo.getCollection(collectionName); nodes = jongo.getCollection(collectionName + "_nodes"); collection.ensureIndex("{ \"subject\": 1 }", "{ \"background\": true, \"unique\": false }"); collection.ensureIndex("{ \"predicate\": 1 }", "{ \"background\": true, \"unique\": false }"); collection.ensureIndex("{ \"object\": 1 }", "{ \"background\": true, \"unique\": false }"); collection.ensureIndex("{ \"subject\": 1, \"predicate\": 1, \"object\": 1 }", "{ \"background\": true, \"unique\": false }"); collection.ensureIndex("{ \"subjectLong\": 1 }", "{ \"background\": true, \"unique\": false }"); collection.ensureIndex("{ \"predicateLong\": 1 }", "{ \"background\": true, \"unique\": false }"); collection.ensureIndex("{ \"objectLong\": 1 }", "{ \"background\": true, \"unique\": false }"); collection.ensureIndex("{ \"subjectLong\": 1, \"predicateLong\": 1, \"objectLong\": 1 }", "{ \"background\": true, \"unique\": false }"); nodes.ensureIndex("{ \"URI\": 1 }", "{ \"background\": true, \"unique\": true }"); } public void start(final boolean cluster) throws IOException { // final String script = "4s-start-" + (cluster ? "cluster" : "single") + ".sh"; // invokeScript(script); } public void destroy() throws IOException { collection.drop(); nodes.drop(); } public void load(final String modelPath, String ontologyIRI) throws IOException { // System.out.println(System.currentTimeMillis()); if (!new File(modelPath).exists()) { throw new FileNotFoundException(modelPath); } RDFParser rdfParser = Rio.createParser(RDFFormat.TURTLE); rdfParser.setRDFHandler(new RDFHandler() { @Override public void startRDF() throws RDFHandlerException { } @Override public void handleStatement(Statement st) throws RDFHandlerException { collection.insert(st); } @Override public void handleNamespace(String arg0, String arg1) throws RDFHandlerException { } @Override public void handleComment(String arg0) throws RDFHandlerException { } @Override public void endRDF() throws RDFHandlerException { } }); // System.out.println(System.currentTimeMillis()); try { rdfParser.parse(new FileInputStream(modelPath), ontologyIRI); MongoCursor<MStatement> nulls = collection.find("{ \"subjectBI\": \"\" }").as(MStatement.class); while (nulls.hasNext()) { MStatement next = nulls.next(); String subjectBI = getBISForURI(next.getSubject().toString()); String predicateBI = getBISForURI(next.getPredicate().toString()); String objectBI = getBISForURI(next.getObject().toString()); collection .update("{ \"subject\": #, \"predicate\": #, \"object\": # }", next.getSubject().toString(), next.getPredicate().toString(), next.getObject().toString()) .with("{ $set: { \"subjectBI\": #, \"predicateBI\": #, \"objectBI\": # } }", subjectBI, predicateBI, objectBI); } } catch (IOException e) { e.printStackTrace(); } catch (RDFParseException e) { e.printStackTrace(); } catch (RDFHandlerException e) { e.printStackTrace(); } // System.out.println(System.currentTimeMillis()); } public String getBISForURI(String URI) { return nodes.findAndModify("{ \"URI\": # }", URI).with("{ $set: { \"URI\": # } }", URI) .sort("{ \"URI\": 1 }").upsert().returnNew().as(Node.class).get_id(); } public void invokeScript(final String script) throws IOException { // final String command = UnixUtils.createTempFileFromScript(script); // UnixUtils.run(command, showCommandOutput, environment); } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public void setRdfFormat(final RDFFormat rdfFormat) { this.rdfFormat = rdfFormat; } public boolean isShowCommandOutput() { return showCommandOutput; } public void setShowCommandOutput(final boolean showCommandOutput) { this.showCommandOutput = showCommandOutput; } public boolean isShowUpdateCommands() { return showUpdateCommands; } public void setShowUpdateCommands(final boolean showUpdateCommands) { this.showUpdateCommands = showUpdateCommands; } }