Database Reference
In-Depth Information
Figure 6.1. A social network graph to be queried
In chapter 4 , when we discussed the Neo4j Traversal API, we demonstrated how to find all
the movies a user has seen using Java code. As a reminder, the following listing shows the
Java code used.
Listing 6.1. Traversing the graph using Java API to find all movies the user has seen
try (Transaction tx = graphDb.beginTx()) {
Node userJohn = graphDb.getNodeById(JOHN_JOHNSON_NODE_ID);
Iterable<Relationship> allRelationships = userJohn.getRelationships();
Set<Node> moviesForJohn = new HashSet<Node>();
for(Relationship r : allRelationships){
if(r.getType().name().equalsIgnoreCase("HAS_SEEN")){
Node movieNode = r.getEndNode();
moviesForJohn.add(movieNode);
}
}
for(Node movie : moviesForJohn){
logger.info("User has seen movie: " + movie.getProperty("name"));
}
tx.success();
 
Search WWH ::




Custom Search