Desktop version

Home arrow Computer Science arrow Designing Data-Intensive Applications. The Big Ideas Behind Reliable, Scalable and Maintainable Systems

The SPARQL query language

SPARQL is a query language for triple-stores using the RDF data model [43]. (It is an acronym for SPARQL Protocol and RDF Query Language, pronounced “sparkle.”) It predates Cypher, and since Cypher’s pattern matching is borrowed from SPARQL, they look quite similar [37].

The same query as before—finding people who have moved from the US to Europe— is even more concise in SPARQL than it is in Cypher (see Example 2-9).

Example 2-9. The same query as Example 2-4, expressed in SPARQL


SELECT ?personName WHERE {

  • ?person :name ?personName.
  • ?person :bornIn / :within* / :name "United States".
  • ?person :tivesIn / :within* / :name "Europe".


The structure is very similar. The following two expressions are equivalent (variables start with a question mark in SPARQL):

  • (person) -[:BORN_IN]-> () -[:WITHIN*0..]-> (location) # Cypher
  • ?person :bornIn / :within* ?location. # SPARQL

Because RDF doesn’t distinguish between properties and edges but just uses predicates for both, you can use the same syntax for matching properties. In the following expression, the variable usa is bound to any vertex that has a name property whose value is the string "United States":

(usa {name:'United States'}) # Cypher ?usa :name "United States". # SPARQL

SPARQL is a nice query language—even if the semantic web never happens, it can be a powerful tool for applications to use internally.

< Prev   CONTENTS   Source   Next >

Related topics