NoSQL- Graph databases (Neo4J), or the graph model - English Version
Autor: Felipe Antunes | Publicado: 2012-06-06T16:36:00-07:00

Para versão em português, clique aqui!

/*&nbsp;Ladies And Gentlemen DBAS!!! 
&nbsp;&nbsp; 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In my first post in english (sorry in advance), I would like to show you one of most interesting database models. The Graph Database (or Property Graph data model).

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A graph&nbsp;is model to store data which can be naturaly&nbsp;structured in an graphic way. A social network is an&nbsp;typical example, we have&nbsp;users that make connections between itself in many diferent ways (friend, follower, etc..) and they also can&nbsp;get connected with a lot of other objects like ideas, products or blog posts (like, follow, etc..) 


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Another good&nbsp;examples&nbsp;are hierachy, corporate networks&nbsp;and semantic web applications





 
 
  
  
  
  
  
  
  
  
  
  
  
  
 
 
 


Fig.1
– Modeling my family



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Instead of use tables, columns and PK/FK relationships , an graph database uses vertices,
properties and edges. 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; A vertex is a object&nbsp;with properties. Example:&nbsp;In a book store, the vertices could be books, authors and publishers. Books could be described&nbsp;with properties like Title, Genre, Price, Total Pages, etc...&nbsp;authors receive properties like Name, Birthday, biography, etc..
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The
edges connects the nodes with labels like Writed_by, 
Published_by,
etc... Edges can also have properties 



Tools



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The tool that we are going to use in our "hands on"&nbsp;is Neo4J, that can be free download here.&nbsp;The Neo4J&nbsp;it is a BluePrints&nbsp;implementation and&nbsp;BluePrints&nbsp; is a generic API for graph models. Another BluePrints implementations are TinkerGraph, OrientDB, DEX, InfiniteGraph, Rexster, e SailRDFStores

&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;We&nbsp;will also&nbsp;use&nbsp;Gremlin&nbsp;language to manipulate the data. Gremlin&nbsp;is based on Groovy&nbsp;and Pipes.&nbsp;There is a lot of languages that we could use (REST, Cypher).&nbsp;The main point is always choose the right tool for the job.

Note:&nbsp;Excuse me for the&nbsp;"new names avalanche", so many options make any begginner confuse, especially when&nbsp;we came from the relational world where the products are more integrated and ready to use, but is important&nbsp;to have a overview&nbsp;of all tools&nbsp;involved&nbsp;in a&nbsp;graphDB ecosystem if&nbsp;you want to&nbsp;implement&nbsp;it on the real world.







Creating&nbsp;an graph 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If your&nbsp;OS&nbsp;is Windows, all you need to do is download the Neo4J packagen on website ,&nbsp;uncompressed it&nbsp;and&nbsp;execute Neo4J.bat file located in Bin folder

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The script will open a java windows (it is the service running)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The Neo4J provides a web 
interface web&nbsp;hosted on your machine. by default, you can access the web admin on&nbsp; http://localhost:7474/webadmin/







&nbsp;&nbsp;&nbsp;To start, click on Console tab and select the Gremlin button on rigth-upper corner.


Inserting vertex, properties and listing

&nbsp;&nbsp; 

&nbsp;&nbsp;&nbsp;Lets to create a basic social network,&nbsp;the vertex represents&nbsp;users and the unique&nbsp;edge type&nbsp;available will be KNOWS (CONHECE in portuguese).



&nbsp;&nbsp; You can see below the code to add some users (a litle tribute to some friends of mine)





&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Just after the ASCII Art,&nbsp;the webadmin instance the variable g that represents the graph.

After that, we use addVertex function to add our first vertex


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The vertex properties are passed as a parameter of the function, it is an JSON that basically have
this structure:


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [PropertyName:PropertyValue, ...]




As you can notice, one differece&nbsp;between a&nbsp;relational database and a noSQL is that noSQL is schemaless,so properties can varies in the vertices.
To list vertex, use g.V, where V is a collection of vertices inside the graph G. To list a specific property type the property name after V. Example: g.V.Name or g.V.any_other_property.







We notice that the&nbsp;Bruno Salim position ("profissão" in portuguese)&nbsp;was writed wrong.&nbsp;Lets update this ( the vertex ID of Bruno is&nbsp;28).






Very simple, as is&nbsp;simplier add new properties.&nbsp;For example,&nbsp;lets add a position to Gustavo vertex.





Or&nbsp;add a completely new property (this is schemaless!)






&nbsp;&nbsp;&nbsp;&nbsp; The map show all vertex properties (a JSON&nbsp;structure).


Connecting vertices.





&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Now, we are going to create connections between vertices through the edge type&nbsp;KNOWS 













First of all, we assign&nbsp;the vertices to friendly named&nbsp;variables , then we add the edges between them.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Finally, lets answer a few typical questions&nbsp;of social networks&nbsp;and figure the power of graph db.

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;Gremlin queries are divided by steps separated by dots. 



Object.step1.step2.stepN.property



A step use the result of&nbsp;the previous step and can&nbsp;&nbsp;transform, filter or insert&nbsp;some side effects&nbsp;in results. 







Who I know? 



&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The
Felipe object is me, we use the out step to return all vertices wich&nbsp;receive edges of the object.










Who are the friends of my friends? 



&nbsp;&nbsp;&nbsp;&nbsp; Re-applying
the out step, we list the vertices conected to the vertices returned by the first out.






&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;One&nbsp;way to avoid the use&nbsp;of&nbsp;the out step many times 
it is use the loop step,&nbsp;it repeat&nbsp;the previous step until&nbsp;the rule in brackets&nbsp;is not&nbsp;fulfilled (In our case, we limited the loop to twice times checking the it.loops property.)








Who can introduces Felipe to Gustavo?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The power of graph can be showed&nbsp;solving this question. In a relational database, the correspondent query will be&nbsp;heavy and unsuitable for&nbsp;applications with many users, but in a graph db regardless the quantity of users and iterations the&nbsp;result will keep the same performance.





&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp; The loop will iterate until it find the the gustavo vertex. The query list two possible paths.



&nbsp;&nbsp;&nbsp;&nbsp; Replacing the "out" steps&nbsp; for pairs of "outE" (outbound edges)&nbsp;and "inV" (to get the vertices) we will get how&nbsp;they relate. 





&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Lets delete the&nbsp;connection between Spigariol&nbsp;and Gustavo&nbsp;and try again.





&nbsp;&nbsp;&nbsp; In the first line, we use 'g.E' to list all&nbsp;edges,&nbsp;then&nbsp;'inV' to get the verices and&nbsp;'has' to filter that vertices with property Name equals "Gustavo". Finally,&nbsp;we use back(2),
to show the results of two steps behind.(returning to E).
&nbsp;&nbsp;&nbsp;&nbsp;The next command we remove the Edge, now we have only one possible path to Gustavo.



Conclusion

&nbsp;&nbsp;&nbsp;



&nbsp;&nbsp;&nbsp;&nbsp;That
was a little tour on GraphDB, what are the tools to access and a "hands
on" to create, erase and query the stored vertices. This post is not a
full tutorial, for a more complete information please see this great video of Andreas
Kollegger &lt;twitter&gt;
, he
explain the graph database concept and present an full demo of Neo4j.&nbsp; My favorite part of the video is when he said that a relational database&nbsp;is great to calculate the salary average of the attendees of the webcast, but the graph&nbsp;would be better to identify who would&nbsp;buy&nbsp;him a beer.
&nbsp;&nbsp; &nbsp;&nbsp; Thanks for your reading. If you have any questions, feel free to send me a email, or follow me on twitter, I´m always posting related content&nbsp;of&nbsp;the marvelous world of data!


Best Regards!




Felipe Antunes




email:fjantunes@gmail.com




twitter: @felipe_store