DEV Community

Cover image for Getting started with Cypher and RedisGraph / Part III
Gianluca Fabrizi
Gianluca Fabrizi

Posted on

Getting started with Cypher and RedisGraph / Part III

Welcome to the last part of the "Getting started with Cypher and RedisGraph" series.
In the first post we looked at what a Graph DB is and why should you use one; the second post explained the basics of querying RedisGraph.

Now we will build a very small example to show everything we learned in the previous posts.

First of all, start redis Insight with docker compose up -d.

At this point you need python3 installed on your local machine; if you don't, check online the instructions for you OS.
You also need click and redis python extensions; you can install them with:

pip3 install click redis
Enter fullscreen mode Exit fullscreen mode

Now that you have everything, you can clone the demo repository

git clone https://github.com/gfabrizi/redisgraph-demo.git
Enter fullscreen mode Exit fullscreen mode

enter the cloned directory and launch the data import:

python3 bulk_insert.py Routes -n City.csv -r ROUTE.csv -h 127.0.0.1 -p 6379
Enter fullscreen mode Exit fullscreen mode

you should see a message like this:

b'City'  [####################################]  100%
19 nodes created with label 'b'City''
b'ROUTE'  [####################################]  100%
81 relations created for type 'b'ROUTE''
Construction of graph 'Routes' complete: 19 nodes created, 81 relations created in 0.006071 seconds
Enter fullscreen mode Exit fullscreen mode

What just happened?

we used a script (bulk_insert.py) from the awesome repository redis-dataset:
https://github.com/redis-developer/redis-datasets
The script is used to import nodes (-n parameter) and relationships (-r parameter). You can specify more than one nodes and relationships file.
We imported nodes representing cities (City.csv) and relations (ROUTE.csv); the import script uses the csv filenames to give name to nodes and relations.
So now we have our graph "Routes" filled with 19 cities and 1786 routes.

It's time to open RedisInsight UI in the browser and start querying!

Visualize data

Go to "Workbench" tab in RedisInsight and launch the generic query to show all nodes and relations:

GRAPH.QUERY Routes "MATCH (a) RETURN a"
Enter fullscreen mode Exit fullscreen mode

you should see a graph like this (remember to check the "All relationships" flag:

Showing all nodes and edges

Not really helpful, right? 😔
Well, informations are there, now you have to query for what you want to know.

A little background:
Years ago a company asked me to resolve a test for an interview as backend developer. I had 3-4 days to solve this: "Imagine you have 20 goods depots, connected through cargo truck routes (each depot is connected with a maximum of 3 nearby depots). Write a script that - given deposit A and deposit B - finds the best route between them".
I struggled for days with this test, trying to learn and implement Dijkstra algorithm... with no success at all! 🥲
Well, if I knew about GraphDBs at that time, I would have passed that test! 🔝

For example, if you want to know the shortest routes between Memphis and Phoenix, you could use this query:

GRAPH.QUERY Routes "MATCH (source:City {name: 'Memphis'}), (destination:City {name: 'Phoenix'}) with source, destination MATCH p=allShortestPaths((source)-[:ROUTE*]->(destination)) RETURN nodes(p) as cities"
Enter fullscreen mode Exit fullscreen mode

we wrote a first MATCH query, followeb by a second one; to use the matching nodes of the first MATCH in the second one, we need to specify them with the with keyword. This keyword is useful when you have to use one (or more) previously matched node in a following MATCH.
allShortestPaths is a function of MATCH that allows us to find every shortest paths between two nodes; if we choose to see results as text (and not the defaul graph) we could see that Redis found 2 paths:

allShortestPaths match

if we wanted only one shortest path, we could use the shortestPath function:

GRAPH.QUERY Routes "MATCH (source:City {name: 'Memphis'}), (destination:City {name: 'Phoenix'}) RETURN shortestPath((source)-[:ROUTE*]->(destination))"
Enter fullscreen mode Exit fullscreen mode

if you look at the textual output of the results, you'll notice that this time RedisGraph returned not only the nodes, but also the relationships between nodes.
If you only want the nodes involved in the shortestPath, change the query like this:

GRAPH.QUERY Routes "MATCH (source:City {name: 'Memphis'}), (destination:City {name: 'Phoenix'}) RETURN nodes(shortestPath((source)-[:ROUTE*]->(destination)))"
Enter fullscreen mode Exit fullscreen mode

Cool, right?

Conclusion

This series showed the basic of RedisGraph and the Cypher language.
There is much more to explore and learn; if we look just at the "path algorithms", the official documentation offers many functions to use:
https://redis.io/docs/stack/graph/path_algorithm/#find-all-paths-from-a-if-the-trip-is-limited-to-10-kilometers

I hope you have followed this series and enjoyed it as much as I did!😊🎉

Top comments (0)