flynn.gg

Christopher Flynn

Machine Learning
Systems Architect,
PhD Mathematician

Home
Projects
Open Source
Blog
Résumé

GitHub
LinkedIn

Blog


RedisGraph Elixir client

2019-09-30 Feed

Over the past few months, I’ve done research into a number of graph database products as database backend solutions for some core platform architecture. One of the products that I found was RedisGraph, a graph db module that runs inside of redis, an in-memory key-value database which is typically used for caching, pub/sub, and message queues.

RedisGraph implements openCypher’s Property Graph Model, which describes a directed, labeled multigraph in which both node and edge entities may have properties.

The query implementation is a subset of the Cypher query language, which is described as being expressive, efficient, and human-readable. Cypher was originally created at Neo4j.

Redis provides a number of client library implementations in a number of languages, including Python, Javascript, Go, Java, and Ruby.

I noticed also that there wasn’t an Elixir implementation, nor could I find one from a third party. Since I wanted to get better at the language, I figured I would write one myself.

I used the Redix library’s GenServer to communicate with redis. Most of the Elixir code I wrote is a port of the Python client implementation, with changes to suit the purely functional nature of Elixir.

To launch a redisgraph database instance locally, use

docker run -p 6379:6379 -it --rm redislabs/redisgraph

Here is a simple example on constructing a RedisGraph.Graph and fetching the results of a MATCH query:

alias RedisGraph.{Node, Edge, Graph, QueryResult}

# Create a connection using Redix
{:ok, conn} = Redix.start_link("redis://localhost:6379")

# Create a graph
graph = Graph.new(%{
  name: "social"
})

# Create a node
john = Node.new(%{
  label: "person",
  properties: %{
    name: "John Doe",
    age: 33,
    gender: "male",
    status: "single"
  }
})

# Add the node to the graph
# The graph and node are returned
# The node may be modified if no alias has been set
# For this reason, nodes should always be added to the graph
# before creating edges between them.
{graph, john} = Graph.add_node(graph, john)

# Create a second node
japan = Node.new(%{
  label: "country",
  properties: %{
    name: "Japan"
  }
})

# Add the second node
{graph, japan} = Graph.add_node(graph, japan)

# Create an edge connecting the two nodes
edge = Edge.new(%{
  src_node: john,
  dest_node: japan,
  relation: "visited"
})

# Add the edge to the graph
# If the nodes are not present, an {:error, error} is returned
{:ok, graph} = Graph.add_edge(graph, edge)

# Commit the graph to the database
{:ok, commit_result} = RedisGraph.commit(conn, graph)

# Print the transaction statistics
IO.inspect(commit_result.statistics)

# Create a query to fetch some data
query = "MATCH (p:person)-[v:visited]->(c:country) RETURN p.name, p.age, v.purpose, c.name"

# Execute the query
{:ok, query_result} = RedisGraph.query(conn, graph.name, query)

# Pretty print the results using the Scribe lib
IO.puts(QueryResult.pretty_print(query_result))

which gives the following results:

# Commit result statistics
%{
  "Labels added" => nil,
  "Nodes created" => "2",
  "Nodes deleted" => nil,
  "Properties set" => "5",
  "Query internal execution time" => "0.228669",
  "Relationships created" => "1",
  "Relationships deleted" => nil
}

# Query result pretty-printed
+----------------+-------------+-----------------+--------------+
| "p.name"       | "p.age"     | "v.purpose"     | "c.name"     |
+----------------+-------------+-----------------+--------------+
| "John Doe"     | 33          | nil             | "Japan"      |
+----------------+-------------+-----------------+--------------+

Further reading

RedisGraph Elixir

Redis

Graph

RedisGraph

Back to the posts.