graph-query-socialGraph databases are rising in popularity among the ranks of NoSQL databases. They allow you to store data as entities (nodes) and relationship (edges), and allow you to query the data as a graph. Queries written against graph databases are closer to how the data is modeled than other query languages. A great advantage of graph queries is that they eliminate the need to join multiple tables to find those relationships between your data points because the relationships are embedded in the data itself.

Once you have your mind set on a particular graph database, the next question will be, “Which query language should I use?” Unlike SQL databases where you pretty much have only one choice, graph databases have numerous query languages, each of which is trying to solve a particular problem.

Here are a few samples of the most popular graph database query languages:

Cypher

Cypher is a very popular declarative query language that was invented by the folks at Neo4j. Its popularity stems from its resemblance to SQL, so those with SQL experience will feel right at home.

MATCH (n:Person)-[:FRIEND]-(f)
WITH count(f) as c, n
MATCH (n)-[:FRIEND]-()-[:FRIEND]-(fof)
RETURN n, c, fof

SPARQL

SPARQL is SQL-like declarative query language that was created by W3C to query RDF (Resource Description Framework) graphs.

PREFIX foaf: 
SELECT ?craft ?homepage
{
  ?craft foaf:name "Apollo 7" .
  ?craft foaf:homepage ?homepage
}

GraphQL

GraphQL (created by Facebook) is a query language for APIs that isn’t specific to graph databases. Users define the structure of the data they need and they get exactly what they asked for. GraphQL queries are organized as types and fields, and not as endpoints. By varying the request object, you can determine what gets returned by the server.

GraphQL Query
{
  hero {
    name
    mass
  }
}
Returns
{
  "hero": {
    "name": "Luke Skywalker",
    "mass": 77
  }
}

By adding more attributes to the query, you change what gets returned

{
  hero {
    name
    height
    mass
  }
}
Returns
{
  "hero": {
    "name": "Luke Skywalker",
    "height": 1.72
    "mass": 77
  }
}

Gremlin

gremlin-language-variants Gremlin was created in 2009 and is the query language for Apache TinkerPop™. It is a graph traversal DSL (Domain Specific Language) that can be either declarative or imperative. It can be used for OLTP and OLAP graph databases. Gremlin is Groovy-based, but has many language variants that allow developers to write Gremlin queries natively in many modern programming languages such as Java, JavaScript, Python, Scala, Clojure and Groovy.

The fact that Gremlin is Groovy and can be embedded within a lot of modern programming languages allows for endless possibilities to what you can do. It allows you to start your traversal at either the vertices or the edges and to express any arbitrary graph query.

The following query returns the full path from all Hip Hop and Techno bands and the venues in which they are performing.

def gt=graph.traversal();
gt.V()
.hasLabel("band")
.has("genre",within("indie","Hip Hop","Techno/EDM"))
.outE("performing_at")
.inV().path();

If you’re interested in learning more about Gremlin, including learning about the syntax and traversal steps, please join me for a webinar I’m presenting on January 19 called Gremlin 101: How to Query Your Graph Data Using Gremlin.
In this webinar you’ll learn:

  • What Gremlin is and why it’s important
  • The Gremlin virtual machine
  • The Gremlin query language syntax
  • The Gremlin traversal steps
  • Graph CRUD
  • Graph traversal
  • How to use the Gremlin console and IBM Graph to run Gremlin queries

Save

4 comments on"No more joins: An overview of Graph database query languages"

  1. Thank you for the overview. It would be nice to use the same graph example for all query languages to compare them side by side

    • Alaa Mahmoud January 17, 2017

      @Thorsten That’s a good suggestion, I will try to do that in a follow up post.

  2. Yes I want to second Thorstens comment. Same base example graph and then expressing the one basic and one more advanced query on top of it in the different languages.

  3. Alaa Mahmoud January 19, 2017

    @Michael. Thanks for the suggestion but the intention was to give a quick overview of the query languages and not necessarily provide a deep dive in each of them. We will be doing a more in-depth tutorials for Gremlin in the future so stay tuned.

Join The Discussion

Your email address will not be published. Required fields are marked *