neo4j 三元组数据存储问题的解决方案

来源:互联网 发布:怎么检查dns linux 编辑:程序博客网 时间:2024/06/01 10:12

    图数据库的扩展性,灵活性非常好,适合用于复杂关系管理和关系查询推理,社交关系应用就是一个可选的应用场景。语义网和Ontology的数据结构(三元组)就是图结构数据,而基于RDF构建的语义网,过于复杂,效率很低,实际上不如基于图数据库的构建方便好用,而且Neo4J支持RDF,SPARQL等扩展。目前neo4j支持了linked data数据存储和有效数据查询。

   早期 Neo4j-rdf-sail 组件用来将rdf数据批量存储到neo4j中,但是由于这个项目不在活跃,因此本次介绍了 Tinkerpop Sail 实现。Tinkerpop是一组针对图形数据库一系列工具,其中blueprints相当于图形库的jdbc引擎,通过提供了一系列的接口来开发基于图形数据库的应用,而不是依赖于任何一种具体的图形数据库实现。

  •    首先我们将所有的依赖通过sbt进行编译和下载

name := "hello"version := "1.0"scalaVersion := "2.10.3"site.settingslibraryDependencies += "com.tinkerpop.blueprints" % "blueprints-core" % "2.4.0"libraryDependencies += "com.tinkerpop.blueprints" % "blueprints-graph-sail" % "2.4.0"libraryDependencies += "com.tinkerpop.blueprints" % "blueprints-neo4j-graph" % "2.4.0"libraryDependencies += "org.openrdf.sesame" % "sesame-queryparser-sparql" % "2.6.10"val sampleStringTask = taskKey[String]("A sample string task.")val sampleIntTask = taskKey[Int]("A sample int task.")sampleStringTask := System.getProperty("user.home")sampleIntTask := {  val sum = 1 + 2  println("sum: " + sum)  sum}

  • 运行sbt eclipse, 开始构建工程并且下载相关的依赖
  • 最后看一下测试函数,能够做到存储rdf数据和支持sparql查询语言

package sail.test;import info.aduna.iteration.CloseableIteration;import org.openrdf.model.Statement;import org.openrdf.model.ValueFactory;import org.openrdf.query.BindingSet;import org.openrdf.query.QueryEvaluationException;import org.openrdf.query.TupleQuery;import org.openrdf.query.impl.EmptyBindingSet;import org.openrdf.query.parser.ParsedQuery;import org.openrdf.query.parser.serql.SeRQLParser;import org.openrdf.query.parser.sparql.SPARQLParser;import org.openrdf.repository.sparql.SPARQLRepository;import org.openrdf.repository.sparql.query.SPARQLGraphQuery;import org.openrdf.repository.sparql.query.SPARQLQuery;import org.openrdf.sail.Sail;import org.openrdf.sail.SailConnection;import org.openrdf.sail.SailException;import com.tinkerpop.blueprints.Edge;import com.tinkerpop.blueprints.Graph;import com.tinkerpop.blueprints.TransactionalGraph;import com.tinkerpop.blueprints.Vertex;import com.tinkerpop.blueprints.impls.neo4j.Neo4jGraph;import com.tinkerpop.blueprints.impls.tg.TinkerGraph;import com.tinkerpop.blueprints.oupls.sail.GraphSail;public class Test {/** * @param args * @throws Exception  */public static void main(String[] args) throws Exception {//Neo4jGraph graph = new Neo4jGraph("D:/webapp/neo");//graph.setCheckElementsInTransaction(true);//Sail sail = new GraphSail(graph);//sail.initialize();Neo4jGraph graph = new Neo4jGraph("D:/webapp/neo2");graph.setCheckElementsInTransaction(true);Sail sail = new GraphSail(graph);sail.initialize();SailConnection sc = sail.getConnection();ValueFactory vf = sail.getValueFactory();sc.addStatement(vf.createURI("http://tinkerpop.com#1"), vf.createURI("http://tinkerpop.com#knows"), vf.createURI("http://tinkerpop.com#3"), vf.createURI("http://tinkerpop.com"));sc.addStatement(vf.createURI("http://tinkerpop.com#1"), vf.createURI("http://tinkerpop.com#name"), vf.createLiteral("marko"), vf.createURI("http://tinkerpop.com"));sc.addStatement(vf.createURI("http://tinkerpop.com#3"), vf.createURI("http://tinkerpop.com#name"), vf.createLiteral("josh"), vf.createURI("http://tinkerpop.com"));System.out.println("get statements: ?s ?p ?o ?g");CloseableIteration<? extends Statement, SailException> results = sc.getStatements(null, null, null, false);while(results.hasNext()) {    System.out.println(results.next());}System.out.println("\nget statements: http://tinkerpop.com#3 ?p ?o ?g");results = sc.getStatements(vf.createURI("http://tinkerpop.com#3"), null, null, false);while(results.hasNext()) {    System.out.println(results.next());}SPARQLParser parser = new SPARQLParser();CloseableIteration<? extends BindingSet, QueryEvaluationException> sparqlResults;String queryString = "SELECT ?x ?y WHERE { ?x <http://tinkerpop.com#knows> ?y }";ParsedQuery query = parser.parseQuery(queryString, "http://tinkerPop.com");System.out.println("\nSPARQL: " + queryString);sparqlResults = sc.evaluate(query.getTupleExpr(), query.getDataset(), new EmptyBindingSet(), false);while (sparqlResults.hasNext()) {    System.out.println("this is test : " + sparqlResults.next());}sc.close();graph.shutdown();sail.shutDown();}}

0 0
原创粉丝点击