Ever since Facebook promoted its “graph search” methodology, lots of people in our industry have been waking up to the fact that graphs are über-cool. Thanks to the powerful query possibilities, people like Facebook, Twitter, LinkedIn, and let us not forget, Google have been providing us with some of the most amazing technologies. Specifically, the power of the “social network” is tempting many people to get their feet wet, and to start using graph technology. And they should: graphs are fantastic at storing, querying and exploiting social structures, stored in a graph database.
So how would that really work? I am a curious, “want to know” but “not very technical” kind of guy, and I decided to get my hands dirty (again), and try some of this out by storing my own little part of Facebook – in neo4j. Without programming any kind of production-ready system – because I don’t know how – but with enough real world data to make us see what it would be like.
Give me my data!
The first step to take was to get access to my own facebook data. Obviously there is the facebook graph api, but I hope that by now you would realise that is just not my cup of tea. Sounded to exhausting 🙂 … So: I found myself a little tool that would allow me to download the data from facebook, in a workable format. Give me my data provides a number of different export options, but I chose the “Mutual Friends Network Graph – as it would give me most info with regards to my actual social network.
After a few seconds, the app presents me with a csv document that I could copy and paste into a text file:
and in the spreadsheet I then used some spreadsheet wizardry to generate the Cypher statements that would allow us to import the data.
=”create (n{name:'”&E2&”‘, type:’Facebook’});”
create (n{name:’Rik Van Bruggen’, type:’Facebook’});
=”start n1=node:node_auto_index(name='”&A2&”‘),
n2=node:node_auto_index(name='”&B2&”‘)
CREATE n1-[:IS_A_FRIEND_OF]->n2;”
start n1=node:node_auto_index(name=’Pieter-Jan Boone’),
n2=node:node_auto_index(name=’Jason Goode’)
CREATE n1-[:IS_A_FRIEND_OF]->n2;
So now we have the cypher statements. All we now need to do is execute them in a transaction (wrap them with begin / commit) to make creating the graph really fast and easy. Before you do that, please make two important notes:
-
don’t forget to configure your “auto-indexing” in neo4j before you execute the statements – otherwise your nodes/relations will not be indexed properly.
-
And that’s about it. Now you can just surf to the webadmin and start exploring: https://localhost:7474/webadmin/ … Of course there are better visualisations possible – just look at neo4j.org for more info.
Just to show you how easy it is, I have also created a little video that explains the process for you – you will see that it’s dead easy.
Hope this is useful, and that we will be seeing many more neo4j based social network application in the near future.
Want to learn more about graph databases? Click below to get your free copy of O’Reilly’s Graph Databases ebook and discover how to use graph technologies for your application today.
Download My Ebook