Now Reading
Experimenting with graph databases with Memgraph and Elixir

Experimenting with graph databases with Memgraph and Elixir

2023-05-21 04:48:47

I had a dialog with Marko Budiselić @mbudiselicbuda, the CTO at Memgraph, throughout this 12 months’s DORS/CLUC convention. We mentioned graph databases intimately and explored their capabilities and purposes. Since I had no prior expertise with graph databases, I discovered the dialogue fascinating. As an enthusiastic person of Elixir, I used to be particularly interested in the opportunity of integrating Elixir with Memgraph and utilizing its options throughout the Elixir ecosystem.

One of many largest challenges when working with demos is acquiring precise knowledge for the database. Thankfully, Memgraph affords incredible “datasets” that you need to use of their Memgraph Lab utility. I’ve chosen to make use of the “Europe street community” mannequin for this demo.

These datasets provided by Memgraph are particularly designed to showcase the ability and flexibility of their graph database answer. They’re rigorously curated collections of interconnected knowledge, meticulously crafted to symbolize real-world situations and domains. By using these datasets, we are able to get hands-on expertise with Memgraph’s options and functionalities.

The thought of this put up is to experiment with Memgraph and see (1) if we are able to use it with Elixir in any respect, and (2) the way it all works underneath the hood. As of the time of penning this I haven’t discovered any tutorials or guides on methods to use it with Elixir, and we clearly want to repair that ASAP ????.

Getting began

First we have to setup our demo setting. Here’s a guidelines of what we want earlier than we begin:

Organising Memgraph DB is tremendous straightforward:

docker run -d 
  -p 7687:7687 -p 7444:7444 
  -v mg_lib:/var/lib/memgraph 
  -e MEMGRAPH_USER=memgraph 
  -e MEMGRAPH_PASSWORD=memgraph 

And that ought to have our DB up and operating regionally. Memgraph people suggest a pleasant multi function package deal, however I haven’t been ready to determine methods to setup the person and password which is required for Neo4j adapter that we use in Elixir.

Subsequent up, you’ll be able to obtain and set up Memgraph Lab. Open it up and enter the connection particulars manually (username and password). From there you’ll be able to load your dataset.

Screen shot showing a Memgraph Lab application and a red arrow pointing to the check out existing datasets link

Choose the “Europe street community dataset” from the listing of all accessible datasets.

Screen shot showing a Memgraph Lab application and a red arrow pointing to the Europe road network dataset

Organising bolt_sips

We’re going to use the Neo4j adapter library known as bolt_sips to attach and question our database. The library readme accommodates some fundamental directions however I’m going to sum up right here what we have to do to stand up and operating as quickly as potential. You’ll want so as to add {:bolt_sips, "~> 2.0"} to your dependencies, or run this in your reside setting Combine.set up([{:bolt_sips, "~> 2.0"}]).

Subsequent up is connecting:

{:okay, _memgraph} = Bolt.Sips.start_link(url: "bolt://memgraph:memgraph@localhost:7687")
conn = Bolt.Sips.conn()

We will test if all the things works OK by operating:

|> Bolt.Sips.question!("return 1 as n")
|> Bolt.Sips.Response.first()

And if all the things is OK we get %{"n" => 1}. Now we are able to begin taking part in round.

See Also

Planning our street journey

Let’s say we wish to journey from Milan, Italy to Zagreb, Croatia. Additionally, we wish to relaxation each 150 km. Right here is the question that we want:

MATCH path = (:Metropolis { title: "Milan" })
             -[:Road * bfs (e, v | e.length <= 150)]->
             (:Metropolis { title: "Zagreb" })
RETURN path;

You’ll be able to run this by utilizing Bolt.Sips.question! perform. And in consequence you’ll get outcomes with all the nodes in between. If we take a look at the :data we are able to discover the nodes and the relationships we have now. On this brief demo we’re going to solely use the :nodes array:

  %Bolt.Sips.Types.Node{id: 557, properties: %{"name" => "Milan"}, labels: ["City"]},
  %Bolt.Sips.Sorts.Node{id: 121, properties: %{"title" => "Bergamo"}, labels: ["City"]},
  %Bolt.Sips.Sorts.Node{id: 945, properties: %{"title" => "Verona"}, labels: ["City"]},
  %Bolt.Sips.Sorts.Node{id: 551, properties: %{"title" => "Mestre"}, labels: ["City"]},
  %Bolt.Sips.Sorts.Node{id: 922, properties: %{"title" => "Udine"}, labels: ["City"]},
  %Bolt.Sips.Sorts.Node{id: 906, properties: %{"title" => "Trieste"}, labels: ["City"]},
  %Bolt.Sips.Sorts.Node{id: 503, properties: %{"title" => "Ljubljana"}, labels: ["City"]},
  %Bolt.Sips.Sorts.Node{id: 1010, properties: %{"title" => "Zagreb"}, labels: ["City"]}

With slightly mapping magic (accessible within the Livebook instance) we are able to have our itinerary deliberate out:

  {"Milan", "Bergamo"},
  {"Bergamo", "Verona"},
  {"Verona", "Mestre"},
  {"Mestre", "Udine"},
  {"Udine", "Trieste"},
  {"Trieste", "Ljubljana"},
  {"Ljubljana", "Zagreb"}

and mapped out utilizing Mermaid graphs!

Graph connecting nodes together, from Milan to Zagreb

The demo I shared was a easy introduction to Memgraph and graph databases, offering a basis for understanding their capabilities. Nonetheless, I’ve thrilling plans to increase my exploration by working with bigger datasets to delve into extra intriguing concepts. Though the demo was fundamental, it ignited my creativeness concerning the potential of Memgraph and graph databases in fixing numerous issues. One of many outstanding benefits of Memgraph and graph databases is their effectivity in managing intensive and interconnected datasets.

I’ve supplied a Livebook demo so that you can mess around.

Source Link

What's Your Reaction?
In Love
Not Sure
View Comments (0)

Leave a Reply

Your email address will not be published.

2022 Blinking Robots.
WordPress by Doejo

Scroll To Top