My previous blog post discussed the use of Neo4J as a RDF triple store. Michael Hunger however informed me that the neo-rdf-sail component is no longer under active development and advised me to have a look at Tinkerpop’s Sail implementation.
As mentioned in my previous blog post, I recently got asked to implement a storage and querying platform for biological RDF (Resource Description Framework) data. Traditional RDF stores are not really an option as my solution should also provide the ability to calculate shortest paths between random subjects. Calculating shortest path is however one of the strong selling points of Graph Databases and more specifically Neo4J. Unfortunately, the neo-rdf-sail component, which suits my requirements perfectly, is no longer under active development. Tinkerpop’s Sail implementation however, fills the void with an even better alternative!
1. What is Tinkerpop?
Tinkerpop is an open source project that provides an entire stack of technologies within the Graph Database space. At the core of this stack is the Blueprints framework. Blueprints can be considered as the JDBC of Graph Databases. By providing a collection of generic interfaces, it allows to develop graph-based applications, without introducing explicit dependencies on concrete Graph Database implementations. Additionally, Blueprints provides concrete bindings for the Neo4J, OrientDB and DexGraph Databases. On top of Blueprints, the Tinkerpop team developed an entire range of graph technologies, including Gremlin, a powerful, domain-specific language designed for traversing graphs. Hence, once a Blueprints binding is available for a particular Graph Database, an entire range of technologies can be leveraged.
2. Tinkerpop and Sail
Last time, I talked about exposing a Neo4J Graph Database (containing RDF triples) through the Sailinterface, which is part of the openrdf.org project. By doing so, we can reuse an entire range of RDF utilities (parsers and query evaluators) that are part of the openrdf.org project. The Blueprints framework provides us with a similar ability: each Graph Database binding that implements the Tinkerpop TransactionalGraph and IndexableGraph interfaces can be exposed as a GraphSail, which is Tinkerpop’s implementation of the Sail interface. Once you have your Sail available, storing and querying RDF is analogous to the piece of code shown in my previous blog article.
01.
// Create the sail graph database
02.
graph = new MyNeo4jGraph(
"var/flights"
, 100000);
03.
graph.setTransactionMode(TransactionalGraph.Mode.MANUAL);
04.
sail = new GraphSail(graph);
05.
06.
// Initialize the sail store
07.
sail.initialize();
08.
09.
// Get the sail repository connection
10.
connection = new SailRepository(sail).getConnection();
11.
12.
// Import the data
13.
connection.add(getResource(
"sneeair.rdf"
), null, RDFFormat.RDFXML);
14.
15.
// Execute SPARQL query
16.
TupleQuery durationquery = connection.prepareTupleQuery(QueryLanguage.SPARQL,
17.
"PREFIX io: <http://www.daml.org/2001/06/itinerary/itinerary-ont#> "
+
18.
"PREFIX fl: <http://www.snee.com/ns/flights#> "
+
19.
"SELECT ?number ?departure ?destination "
+
20.
"WHERE { "
+
21.
"?flight io:flight ?number . "
+
22.
"?flight fl:flightFromCityName ?departure . "
+
23.
"?flight fl:flightToCityName ?destination . "
+
24.
"?flight io:duration \"1:35\" . "
+
25.
"}"
);
26.
TupleQueryResult result = durationquery.evaluate();
The two first lines of code require some more clarification. A TransactionalGraph can be run inMANUAL or AUTOMATIC transaction mode. In AUTOMATIC mode, transactions are basically ignored, in the sense that each item that gets created is immediately persisted in the underlying Graph Database. Although this fits my needs, AUTOMATIC mode is extremely slow in case of Neo4J because of the continuous IO access. MANUAL mode on the other hand is very fast; a new transaction is created at the moment the import of the RDF data file starts and is only committed to the Neo4J data store once all RDF triples are parsed and created. Unfortunately, MANUAL mode does not scale either in my specific situation; as some of my RDF data files contain over 50 million RDF triples, they can not fit into memory (i.e. Java heap space error). Requiring fast imports, I extended the default Neo4J Blueprints binding to support intermediate commits. I based my implementation on Neo4J’s best practices for big transactions. The idea is rather simple: you specify the maximum number of items that can be kept in memory, before they should be committed to the Neo4J data store. Once this number is reached, the current transaction is committed and a new one is automatically started. Simple, but very effective!
01.
public
class
MyNeo4jGraph
extends
Neo4jGraph {
02.
03.
private
long
numberOfItems =
0
;
04.
private
long
maxNumberOfItems =
1
;
05.
06.
public
MyNeo4jGraph(
final
String directory,
long
maxNumberOfItems) {
07.
super
(directory,
null
);
08.
this
.maxNumberOfItems = maxNumberOfItems;
09.
}
10.
11.
public
MyNeo4jGraph(
final
String directory,
final
Map<String, String> configuration,
long
maxNumberOfItems) {
12.
super
(directory, configuration);
13.
this
.maxNumberOfItems = maxNumberOfItems;
14.
}
15.
16.
public
Vertex addVertex(
final
Object id) {
17.
Vertex vertex =
super
.addVertex(id);
18.
commitIfRequired();
19.
return
vertex;
20.
}
21.
22.
public
Edge addEdge(
final
Object id,
final
Vertex outVertex,
final
Vertex inVertex,
final
String label) {
23.
Edge edge =
super
.addEdge(id, outVertex, inVertex, label);
24.
commitIfRequired();
25.
return
edge;
26.
}
27.
28.
private
void
commitIfRequired() {
29.
// Check whether commit should be executed
30.
if
(++numberOfItems % maxNumberOfItems ==
0
) {
31.
// Stop the transaction
32.
stopTransaction(Conclusion.SUCCESS);
33.
// Immediately start a new one
34.
startTransaction();
35.
}
36.
}
37.
38.
}
3. Shortest path calculation
Although Blueprints allows you to abstract away the Neo4J implementation details, it still provides you with access to the raw Neo4J data store if needed. Hence, one can still use the graph algorithms provided in the neo4j-graph-algo component to calculate shortest paths between random subjects. The complete source code can be found on the Datablend public GitHub repository.
地址:http://java.dzone.com/news/rdf-data-neo4j-tinkerpop-story