DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

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.

Arthur Keen user avatar by
Arthur Keen
·
Dec. 19, 18 · Tutorial
Like (8)
Save
Tweet
Share
17.07K Views

Join the DZone community and get the full member experience.

Join For Free

Two graphs one world

This 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 .

Emil knows Arthur since 2013

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.

Quick Reference

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.

Cypher CREATE, SPARQL INSERT

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

Matching

Creating Nodes and Relationships

Both Cypher and SPARQL can create many nodes and relationships at once.

Creating nodes and relationships

Pattern Matching

Describe what to find in the graph, for example, a pattern can be used to find Emil's friends:

Image title

• 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:

Recommendation

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

The Movie Graph

Finding Things in the Movie Graph

Find the actor named "Tom Hanks"

Find Tom Hanks

Find the movie with the title "Cloud Atlas"

Find the movie with the title &quot;Cloud Atlas&quot;

Find 10 people

Find 10 people

Find movies released in the 1990s

Find movies released in the 1990&apos;s

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...

List all Tom Hanks movies

Who directed "Cloud Atlas"?

Who directed Cloud Atlas

Tom Hanks' co-actors

Tom Hanks&apos; co-actors

How people are related to "Cloud Atlas"

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.

SPARQL

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Cloud-Based Transportation Management System
  • Memory Debugging: A Deep Level of Insight
  • Apache Kafka vs. Memphis.dev
  • Explainer: Building High Performing Data Product Platform

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: