semr / neo4jena

Neo4J-Jena Wrapper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Neo4Jena

Overview

For the integration of Jena and neo4j we have implemented Jena graph interfaces using neo4j native API. Neo4Jena is a property graph model interface. It provides the mapping of RDF to property graph (Neo4J) using Jena API. The main work focuses on how RDF triple is converted to Neo4j graph and vice versa. After the successful loading of RDF in graph we retrieve the data using SPARQL. We made the following contributions:

  • Firstly RDF triples (subject, predicate and object) are converted to Neo4j node and relationship.
  • After the conversion, it store triples in Neo4j graph.
  • After the successful loading of RDF triples in graph Neo4Jena retrieve the data using SPARQL. For this neo4j nodes and relationship is converted to RDF triples (subject, predicate and object).
  • Neo4Jena has full support of SPARQL 1.1.

Mapping RDF to Neo4j

RDF triple consist of subject, predicate and object.

  • Subjects in rdf triples are URIs or blank nodes.
  • All predicates are URIs.
  • Objects can be URIs, blank nodes or literal values.

When mapping RDF to Neo4j following points are taken in considertaion:

  • Each neo4j node has a label that is either uri , literal or bnode.
  • Nodes having label uri or bnode have one property
    • uri
  • Nodes having label literal have three properties
    • value
    • datatype
    • lang

Follwing figure demostrats how uri and literals are modeled in Neo4j.

alt tag

Example: RDF Statement

The following RDF statment is taken from RDF primer. This RDF statement have:

alt tag

The subject and object are resources so while mapping them neo4j node has label uri.The following figure shows how this statement is represented in Neo4j. (In mapping prefixes are used)

alt tag

Example: RDF Statement with Literal

This RDF statement has:

alt tag

The subject is a resource so while mapping them neo4j node has label uri.Literal values can have their datatype and language as well.

alt tag

Example: Multiple Statements including Literal values

Following figure have multiple statements about the same subject. The object can be a URI or literal value.

alt tag

Subjects and objects when mapped in neo4j node have label uri while literal values have label literal. The properties of uri and literals are different. !https://github.com/semr/neo4jena/raw/master/doc/image/example2.PNG!

Example: Statement with Blank node

Subject and object can also be a blank node (a resource having no URI)

alt tag

While mapping blank node the neo4j node has label bnode.

!(alt tag

How to use Neo4Jena?

Create a Jena model and read RDF file/triples in it.

Model model = ModelFactory.createDefaultModel();
InputStream in = FileManager.get().open( inputFileName );
model.read(in,"","TTL"); 

For initialization of NeoGraph there are two constructors.

  • public NeoGraph(final String directory)
  • public NeoGraph(final GraphDatabaseService graphdb)
GraphDatabaseService njgraph = new GraphDatabaseFactory().newEmbeddedDatabase(NEO_STORE);
NeoGraph graph = new NeoGraph(njgraph);

After initialization an instance of NeoGraph is created. Then create a Jena model for graph and pass NeoGraph instance as parameter.

Model njmodel = ModelFactory.createModelForGraph(graph);

Load triples from model into njmodel.

njmodel.add(model);

Bulk Load Example

Create a Jena model and read RDF file/triples in it.

Model model = ModelFactory.createDefaultModel();
InputStream in = FileManager.get().open( inputFileName );
model.read(in,"","TTL"); 

Create a bacth inserter (act as graph service)

BatchInserter db = BatchInserters.inserter(NEO_STORE);

Initialize a batch handler and register the model.

BatchHandler handler = new BatchHandler(inserter,500000,60);
model.register(handler);

Read the model and close the handler

model.read(in,"","TTL");	
handler.close();

Search RDF data with Sparql

String queryString = "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n" +
            	"PREFIX ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#>" +
                "SELECT ?X ?name "+
                "WHERE" +
                "{ ?X ub:name ?name ." +
                 "FILTER regex(?name,\"^Publication\") ."+
                "}"; 
       	          
        Query query = QueryFactory.create(queryString);
        QueryExecution qExe = QueryExecutionFactory.create(query, njmodel);
        ResultSet results = qExe.execSelect();

About

Neo4J-Jena Wrapper

License:Apache License 2.0


Languages

Language:Java 100.0%