Databases Reference
In-Depth Information
In the following code, we see a snippet of code borrowed from the Neo4j tutorial in
which we try to find human companions from the Doctor Who universe: 1
// Index lookup for the node representing the doctor is omitted for brevity
Iterable < Relationship > relationships =
doctor . getRelationships ( Direction . INCOMING , COMPANION_OF );
for ( Relationship rel : relationships )
{
Node companionNode = rel . getStartNode ();
if ( companionNode . hasRelationship ( Direction . OUTGOING , IS_A ) )
{
Relationship singleRelationship = companionNode
. getSingleRelationship ( IS_A ,
Direction . OUTGOING );
Node endNode = singleRelationship . getEndNode ();
if ( endNode . equals ( human ) )
{
// Found one!
}
}
}
This code is very imperative: we simply loop round the Doctor's companions and check
to see if any of the companion nodes have an IS_A relationship to the node representing
the human species. If the companion node is connected to the human species node, we
do something with it.
Because it is an imperative API, the Core API requires us to fine-tune it to the underlying
graph structure. This can be very fast; at the same time, however, it means we end up
baking knowledge of our specific domain structure into our code. Compared to the
higher-level APIs (particularly Cypher) more code is needed to achieve an equivalent
goal. Nonetheless, the affinity between the Core API and the underlying record store is
plain to see—the structures used at the store and cache level are exposed relatively
faithfully by the Core API to user code.
Traversal API
The Traversal API is a declarative Java API. It enables the user to specify a set of con‐
straints that limit the parts of the graph the traversal is allowed to visit. We can specify
which relationship types to follow, and in which direction (effectively specifying rela‐
tionship filters); we can indicate whether we want the traversal to be performed breadth-
first or depth-first; and we can specify a user-defined path evaluator that is triggered
with each node encountered. At each step of the traversal, this evaluator determines
1. Doctor Who is the world's longest-running science fiction show and a firm favorite of the Neo4j team.
Search WWH ::




Custom Search