Neo4j is a Graph Database. It uses Graph Data Model to represent the data, unlike other databases which use tables, documents, etc. Graph Data Model or Graph Data Structure is composed of Nodes and Relationships that connect nodes.
Neo4j Browser
A query workbench and a visualization interface.
To install Neo4j and run Neo4j Browser - you can head to https://neo4j.com/download/ and download a community or an enterprise edition based on your requirements. Based on your OS the installation steps may vary - and once you have a Neo4j application installed - you can now choose the location for the graph database on your computer and start the Neo4j server. Once the server starts - you are provided with an URL - which you can open in your browser. Once you are in you can access the Neo4j Browser using the default username and password - neo4j.
You can use Neo4j browser to not only browse your database, explore sample datasets and run queries - you can also use this tool to learn about graph databases and Neo4j.
Neo4j Database - Core Concepts:
- Nodes - Entity or a Data Record
- Properties - Data Values or Key Value Pairs.
- Relationship - connects nodes.
Node
A Node is a label/name for entities or data record in a graph - an example we can create a Node labeled/named Student, User or Person. Label do not have any properties just a name. A Node may have zero or more labels.
Data for a node is stored as Properties, also similar nodes can have different properties - say John plays Guitar and Jill plays Drums and Jack does not play any instruments. So John and Jill have a property ‘plays’ and a value Guitar and Drums respectively, but Jack won't have this property ‘plays’ - this is possible because Neo4j is schema-free.
Properties
Properties are simple name/value pairs. Properties can be Strings, Numbers or Booleans.
Relationships
To associate or connect nodes we need relationships which tells us how a data record i.e a node is connected to an another node. Example - Raman Knows Arun, Arun Knows Alan. Also, a relationship can have properties - example Raman Knows Arun Since 2011. Relationships will always have direction.
Cypher
To create nodes, relationships, and query Neo4j Database - we need to understand Cypher query language. Neo4j's Cypher is declarative and descriptive language built for working with graph data.
Query to CREATE a NODE
CREATE (ee:Student { name: "Alan", from: "USA" })
CREATE clause to create data
() to indicate a node
{} to add properties
ee:Student a variable 'ee' and label Student for the new node
Query to MATCH a NODE
MATCH (ee:Student) WHERE ee.name = "Alan" RETURN ee;
MATCH clause to specify a pattern of nodes and relationships
(ee:Student) a single node pattern with label 'Person' which will assign matches to the variable 'ee'
WHERE clause to constrain the results
ee.name = "Alan" compares name property to the value "Alan"
RETURN clause used to request particular results
Query to create a RELATIONSHIP
In the below query first, we find an existing node and create a new node for John and then establish a relationship between Alan and John - Alan knows John, since 2001 which is a property for this relationship.
MATCH (ee:Student) WHERE ee.name = "Alan"
CREATE (js:Student { name: "John" }),
(ee)-[:KNOWS {since: 2001}]->(js)
Pattern Matching
Find all students Alan knows.
MATCH (ee:Student)-[:KNOWS]-(friends)
WHERE ee.name = "Alan" RETURN ee, friends
Hopefully, this introductory article helps you in understanding core concepts for Neo4j Database if you are exploring this as a database option for complex and connected data, better data visualization and fast traversal.