SPARQL and Cypher Cheat Sheet
This article is intended for SPARQL users who want to understand Cypher and Cypher users that want to understand SPARQL.
Join the DZone community and get the full member experience.
Join For FreeThis article is intended for SPARQL users who want to understand Cypher and Cypher users that want to understand SPARQL. Cypher and SPARQL have radically different syntaxes, however, they have roughly equivalent functional capabilities. Learning Cypher from a W3C semantic web SPARQL perspective, I found myself outlining the query logic in SPARQL and then transforming the SPARQL to Cypher and so I developed a cheat sheet and a variant of the Neo4j Movie tutorial to include SPARQL and Cypher equivalent queries side-by-side. The original version of this document was created in 2015 and is being updated to the latest Cypher constructs.
Other than syntax, the main functional differences between SPARQL and Cypher stem from the differences between the Linked Property Graph (LPG) and standard RDF graph data models. LPG allows properties on edge, whereas standard RDF does not. In this article, we will use the property graph extensions of RDF SPARQL known as SPARQL* (Pronounced SPARQL-Star) and RDF* (pronounced RDF-star) to directly model the equivalent property on edge. Readers can download the BlazeGraph or AnzoGraph triplestores, which implement this extension. The use of SPARQL* extensions are easily recognized by the "<<" and ">>" used to dereference the triple so that triples can be added to it, for example, "Emil knows Arthur since 2013."
<< <Emil> <knows> <Arthur> >> <since> "2013"^^xsd:year .
For those using classic RDF/SPARQL, you can use the convention that edges with properties in Neo4j can be represented as entities in RDF. The term "qualified relationship" is often used to refer to these entities in RDF. Properties on edge in LPG are often used to represent events, including transactions, measurements, roles, etc., so you often see time(s), location, probability, instrument, actor, amount, and actions on edges and these edges easily be represented as specializations of event in an event taxonomy.
The table below contains an informal mapping of the SPARQL and Cypher constructs.
The Movie Tutorial With Cypher and SPARQL
This section is a variant of the Neo4j Movies tutorial, which shows equivallent Cypher and SPARQL queries side by side to make it easy for the reader to compare and contrast the two graph query languages.
Readers might want to install Neo4j to try out the Cypher and a triplestore like AnzoGraph or Blazegraph to try out the SPARQL*.
To increase readability, we will use the convention of abbreviating RDF IRI’s to a local name in angle brackets, e.g., <john> <age> 11. The translation should also handle the namespace part to be valid IRI’s as in <http://neo.org/movies#john>.
Cypher CREATE and SPARQL INSERT
Create a person named Emil from Sweden with klout 99.
• CREATE clause to create data.
• () parenthesis to indicate a node.
• ee:Persona variable 'ee',
• label 'Person' is the type for the new node.
Using Cypher MATCH and SPARQL WHERE
Finding nodes find the node representing Emil:
• Use Cypher's MATCH and SPARQL's WHERE clause to specify a pattern of nodes and relationships
• Cypher's (ee:Person) a single node pattern with label 'Person' which assign matches to the variable 'ee'
• SPARQL's variable ?ee matches to nodes of type Person.
• Cypher's WHERE clause and SPARQL's FILTER are used to constrain the results.
• Cypher's WHERE ee.name = "Emil", compares name property to the value "Emil".
• SPARQL's FILTER (?name = "Emil"), compares the name property to the value "Emil".
• Cypher's RETURN clause and SPARQL's SELECT clause are used to request particular results
Creating Nodes and Relationships
Both Cypher and SPARQL can create many nodes and relationships at once.
Pattern Matching
Describe what to find in the graph, for example, a pattern can be used to find Emil's friends:
• MATCH clause to describe the pattern from known Nodes to found Nodes
• (ee)starts the pattern with a Person (qualified by WHERE)
• -[:KNOWS]-matches "KNOWS" relationships (in either direction)
• (friends)will be bound to Emil's friends
Recommendations
Pattern matching can be used to make recommendations. Johan is learning to surf, so he may want to find a new friend who already does:
Notes on the recommendation query:
• Cypher's () empty parenthesis to ignore these nodes
• SPARQL's <knows>/<knows> also ignores the intermediate friend node.
• DISTINCT is used because more than one path will match the pattern in Cypher and SPARQL
• surfer will contain Allison, a friend of a friend who surfs
Creating the Movie Graph
Finding Things in the Movie Graph
Find the actor named "Tom Hanks"
Find the movie with the title "Cloud Atlas"
Find 10 people
Find movies released in the 1990s
Finding Patterns Within the Movie Graph
1. Actors are people who acted in movies
2. Directors are people who directed a movie
3. What other relationships exist?
List all Tom Hanks movies...
Who directed "Cloud Atlas"?
Tom Hanks' co-actors
How people are related to "Cloud Atlas"
Conclusion
Cypher and SPARQL are really very similar, and the reader should be able to pick out the different patterns in the syntax. I hope that this article has helped to bring the SPARQL and Cypher communities a little closer together.
Opinions expressed are solely myown and do not express the views or opinions of my employer.
Opinions expressed by DZone contributors are their own.
Comments