{"id":36552,"date":"2016-06-24T17:24:30","date_gmt":"2016-06-24T11:54:30","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=36552"},"modified":"2016-06-27T15:01:25","modified_gmt":"2016-06-27T09:31:25","slug":"up-and-running-with-neo4j","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/up-and-running-with-neo4j\/","title":{"rendered":"Up and Running with Neo4J"},"content":{"rendered":"<p><strong>\u00a0<\/strong>Neo4J is a <a title=\"MongoDB Consulting\" href=\"http:\/\/www.tothenew.com\/mean-stack-web-development-consulting\">NoSQL database<\/a> that stores information in the form of a huge property graphs where tuples\/rows (nodes) are connected to each other with relationships (edges) both of which can have variable number of properties associated with them. Traversing data in the form of graphs implies that we can explore highly connected neighbouring data more efficiently and faster while leaving the data outside our search perimeter untouched. With just a starting point and a pattern to search, neo4j can execute complex queries without the overhead of evaluating relationships.<\/p>\n<p><span style=\"font-weight: 400;\">To make this a bit more comprehensive, first let&#8217;s jot down the primary differences and correspondences between a relational database and Neo4J.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><strong>Tables vs Labels<\/strong> :- In RDBMS tables are responsible for storing data or rows with similar properties, for eg, in a sample database all users will be stored in a single table \u2018USER\u2019 and so on. Contrary to this neo4j provides with a property called :label which are assigned to each node(row) that defines the type of the node, i.e, all nodes with user specific data will be assigned a :label \u2018USER\u2019. Also a node can be assigned more than one :label and yes it means what you are thinking, speaking in rdbms terms \u2018A row can be a part of two tables\u2019, may help in reducing complexity.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">for eg :-<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-36615 aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/TVSL.png\" alt=\"TVSL\" width=\"407\" height=\"215\" \/><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><strong>Rows vs Nodes<\/strong> :- Unlike <a href=\"http:\/\/www.tothenew.com\/blog\/overview-of-database-testing\/\">relational databases<\/a> which store records (tuples\/rows) of same type(entity) in large tables, neo4j stores them (tuple\/rows) in the form of nodes that are then assigned a type by providing them with one or more labels. This precisely means that nodes corresponds to tuples\/rows in a relational db and node-label corresponds to the table name in a relational database.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><strong>Columns vs Properties<\/strong> :- RDBMS uses columns to store information for a tuple whereas Neo4j uses properties that are assigned to nodes. Being a nosql database neo4j can have variable number of properties \u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><strong>Joins vs Edges(Relationships)<\/strong> :- RDBMS evaluate relationships between multiple tables with the help of joins which are calculated at runtime and thus has to go through huge dataset every time irrespective of the data returned by the join operation. Contrary to this we can say that neo4j stores each and every join that can exist between each and every row(node) at the time of node creation itself. Its documentation states that relationships are first class citizens in neo4j, i.e, all relationships are persisted in neo4j and is only traversed at the time of retrieval, hence providing constant time of access and allows you to quickly traverse millions of connections per second per core.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><strong>SQL vs CQL<\/strong> :- Structured query language(SQL) provides a declrative and easy way to query for data but it is not effecient in quering highly connected datasets that needs multiple joins where as Cypher query Language (CQL) uses symentic queries to select, insert, update, delete data from our graph data. Contrary to SQL where we need to define what to get and how to get it, CQL only wants us to define what to get leaving how to get on the graph engine itself. It uses a pattern matching approach to define the queries. <\/span><\/li>\n<\/ul>\n<p>Combining all of the above here is a single visual from <a href=\"https:\/\/neo4j.com\/\" target=\"_blank\">Neo4j<\/a> website itself<\/p>\n<p><strong><strong><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-36616 aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/graph.png\" alt=\"graph\" width=\"424\" height=\"294\" \/><br \/>\n<\/strong><\/strong><\/p>\n<p>Let\u2019s take an example for making above text much more clear :-<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-36617 aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/DB.png\" alt=\"DB\" width=\"767\" height=\"462\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Relational Model<\/span><\/p>\n<p><strong><strong><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-36618 aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/06\/DB_NEO.png\" alt=\"DB_NEO\" width=\"571\" height=\"452\" \/><\/strong><\/strong><\/p>\n<p><span style=\"font-weight: 400;\">Property Graph Model<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Getting started with CQL (Cypher query language) :- According to the <a title=\"Neo4j CQL\" href=\"https:\/\/neo4j.com\/developer\/cypher-query-language\/\" target=\"_blank\">documentation <\/a><\/span><i><span style=\"font-weight: 400;\">\u201cCypher is a declarative, SQL-inspired language for describing patterns in graphs visually using an ascii-art syntax. It allows us to state <\/span><\/i><b><i>what<\/i><\/b><i><span style=\"font-weight: 400;\"> we want to select, insert, update or delete from our graph data without requiring us to describe exactly <\/span><\/i><b><i>how<\/i><\/b><i><span style=\"font-weight: 400;\"> to do it.\u201d<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\">A sample query to find \u2018Name of Persons who have visited a specific city\u2019<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">(Consider above domain model)<\/span><\/i><\/p>\n<p>In SQL :-<\/p>\n<p style=\"text-align: center;\"><span style=\"font-weight: 400;\">SELECT name FROM Person LEFT JOIN VISITED_CITIES ON Person.Id = VISITED_CITIES.Person_Id LEFT JOIN City ON City.Id = VISITED_CITIES.City_Id WHERE CITY.name = \u201cNew Delhi\u201d<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In CQL :- <\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"font-weight: 400;\">MATCH (p:Person)-[:VISITED]-&gt;(c:City) WHERE c.name = \u201cNew Delhi\u201d RETURN p.name<\/span><\/p>\n<p><span style=\"font-weight: 400;\">You may have observed that in SQL we have to define all the process of how to join tables and evaluate relationships to fetch data but in CQL we just have to define what to fetch. Also the use of patterns makes it much more intuitive and different queries can be simplified in CQL because of the same.<\/span><\/p>\n<p>The above discussion is useful when you have decided to use Neo4J in your next BIG project, but what before that, one must decide why to prefer this over other NoSQL solutions. Below are some of my readings and findings about Neo4j, it brings the best of both relational and nosql databases.<\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Flexible Schema<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Property graph model stores data in its natural form i.e having properties for both the nodes and relationships that exists between them.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Change properties on the fly as and when your domain model evolves using CYPHER QUERY LANGUAGE<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">ACID :- While many NoSQL databases keep ACID out, Neo4j brings<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Scale and Performance :- It provides in-built replication and high availability (Enterprise edition). <\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Cypher Query Language :- Powerful SQL like language for traversing and updating graphs with the help of a pattern matching syntax.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Development Tools :- Built in browser for executing queries and visualization using web-browser<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">I hope the above points are enough to get you intrigued and start with the same, so go on and install Neo4j. Here are the steps to install neo4j, as per their website :-<\/span><\/p>\n<p>Download the archive from https:\/\/neo4j.com\/download\/<\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Open up your terminal\/shell.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Extract the contents of the archive, using:<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">tar -xf<\/span><span style=\"font-weight: 400;\"> &lt;filecode&gt;.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">For example,<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">tar -xf neo4j-enterprise-2.3.1-unix.tar.gz<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">the top level directory is referred to as NEO4J_HOME<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Run Neo4j using,<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">$NEO4J_HOME\/bin\/neo4j console<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Instead of \u2018neo4j console\u2019, you can use neo4j start to start the server process in the background.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Visit http:\/\/localhost:7474 in your web browser.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Change the password for the \u2018neo4j\u2019 account.<\/span><\/li>\n<\/ol>\n<p><strong><strong>\u00a0<\/strong><\/strong><\/p>\n<p><span style=\"font-weight: 400;\">Stay tuned for more on :- <\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Cypher Query Language<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Neo4j with Rest<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Spring Data with Neo4j<\/span><\/li>\n<\/ul>\n<p><strong><span style=\"font-weight: 400;\">Thanks for reading, see you next time.<\/span><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0Neo4J is a NoSQL database that stores information in the form of a huge property graphs where tuples\/rows (nodes) are connected to each other with relationships (edges) both of which can have variable number of properties associated with them. Traversing data in the form of graphs implies that we can explore highly connected neighbouring data [&hellip;]<\/p>\n","protected":false},"author":766,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[1],"tags":[4843,3609,3608,1252],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/36552"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/766"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=36552"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/36552\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=36552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=36552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=36552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}