Neo4j: A High-Performance, NoSQL Graph Database
Neo4j is a high-performance, NoSQL graph database that stores structured data on a network rather than in tables. It is an embedded, disk-based, full-transactional Java persistence engine with the characteristics of a mature database. Programmers work in an object-oriented, flexible network structure rather than strict, static tables, enjoying the benefits of an enterprise-class database.
Thinking of Neo4j as a High-Performance Drawing Engine
Imagine Neo4j as a high-performance drawing engine, which has all the characteristics of a mature and robust database. Programmers work in an object-oriented, flexible network structure rather than strict, static tables, but they can enjoy the full benefits of an enterprise-class database.
Registering and Downloading Neo4j
To get started with Neo4j, you need to register and download the desktop version from the official website: Neo4j Desktop Download | Free Graph Database Download. After downloading, click install and specify your own installation directory.
Creating a Graph in Neo4j
We can create a graph in Neo4j based on a project and then click start. This will allow us to enter a browser and operate locally. We can write code statements at the top of the page, which will be explained in the following sections.
Creating Data with Cypher
We will first look at the data and see how to create a clause to add data. To add data, we only need to use known patterns. By providing a model, we can specify that we want to create a graphic structure, tags, and attributes as part of the pattern. The simplest term is called CREATE.
Example: Creating a Node
If we execute the following statement, Cypher returns the number of changes, in this case, add a node, a label, and two properties:
CREATE (: Movie {title: "The Matrix", released: 1997})
This will create a node with the label Movie and properties title and released.
Example: Creating Multiple Elements
We can separate elements with commas or use multiple CREATE statements to create multiple elements. For example:
CREATE (a: Person {name: "Keanu Reeves", born: 1964}) - [r: ACTED_IN {roles: ["Forrest"]}] -> (m: Movie {title: "Forrest Gump", released: 1994})
This will create a node with the label Person and properties name and born, a relationship with the label ACTED_IN and properties roles, and a node with the label Movie and properties title and released.
Finding Existing Data with MATCH
We can find existing data in Neo4j using the MATCH clause. This clause searches for a pattern and returns each successful pattern matching row. For example:
MATCH (m: Movie) RETURN m
This will return all nodes with the label Movie.
Example: Finding a Specific Person
We can find a specific person by specifying their name:
MATCH (p: Person {name: "Keanu Reeves"}) RETURN p
This will return the node with the label Person and name Keanu Reeves.
Connecting New Data to the Existing Structure
To connect new data to the existing structure, we need to know how to find existing models in the graph data. We can use the MATCH clause to find existing data and then use the CREATE clause to add new data.
Example: Creating a Relationship
We can create a relationship between two nodes using the CREATE clause:
CREATE (a: Person {name: "Tom Hanks"}) - [r: ACTED_IN {roles: ["Forrest"]}] -> (m: Movie {title: "Forrest Gump", released: 1994})
This will create a relationship between the node with the label Person and name Tom Hanks and the node with the label Movie and title Forrest Gump.
MERGE: Updating or Creating Data
We can use the MERGE clause to update or create data in Neo4j. This clause checks whether data exists before creating data. For example:
MERGE (m: Movie {title: "Cloud Atlas"}) ON CREATE SET m.released = 2012 RETURN m
This will create a node with the label Movie and title Cloud Atlas and set the released property to 2012.
Example: Creating a Tree Structure
We can create a tree structure using the MERGE clause:
CREATE (y: Year {year: 2014}) MERGE (y) <- [: IN_YEAR] - (m10: Month {month: 10}) MERGE (y) <- [: IN_YEAR] - (m11: Month {month: 11}) MERGE (y) <- [: IN_YEAR] - (m12: Month {month: 12}) RETURN y, m10, m11, m12
This will create a tree structure with nodes Year, Month, and relationships between them.
Conclusion
Neo4j is a high-performance, NoSQL graph database that stores structured data on a network rather than in tables. It is an embedded, disk-based, full-transactional Java persistence engine with the characteristics of a mature database. Programmers work in an object-oriented, flexible network structure rather than strict, static tables, enjoying the benefits of an enterprise-class database. We can create a graph in Neo4j based on a project and then click start. We can use the CREATE clause to add data, the MATCH clause to find existing data, and the MERGE clause to update or create data.