- Neo4j Graph Data Modeling
- Mahesh Lal
- 420字
- 2021-07-16 13:31:17
Introduction to modeling nodes and relationships
We looked at the data models in Figure 2.2. Without delving into technical details, we can say that "A person can fly from New York to London on carrier X". With this statement, we can start exploring possibilities in which this data can be modelled. A preliminary approach would be to mark cities as nodes and flights as relationships, as shown in the following figure:

Figure 2.3: A preliminary model with flights and cities—the property graph
This might seem to be a fair model, however, there are a few problems with the approach. Relationships, in graphs, are used to model how the entities' nodes are related to each other. As discussed earlier, flights are one of the two core entities in our model. Flights don't relate cities to each other, instead, they are a means to get from one city to another. Modeling flights as relationships, can work out in presenting a flight plan, but if we have to allow flight bookings in the future, then we need to change flights to nodes. In general, it's always a good practice to model any entity in the domain as a node. Figure 2.4 shows us an alternative approach to model flights and cities.
Note
Modeling entities as relationships must be avoided. Relationships in Neo4j can't have other relationships linked to them. Relationships should depict semantically relevant connections between two entities, not entities themselves.

Figure 2.4: A model for flights and cities
The preceding data model has :Flight and :City as labels for the nodes for flights and cities, respectively. :HAS_FLIGHT and :FLYING_TO are relationships that link a flight to its origin city and destination city, respectively.
When we represent a part of a graph that contains all possible nodes and relationships, we lay down a specification for how data is connected and structured. Thus, we can describe graphs using specification by example.
This graph seems to be a good starting point for us to begin feeding the data into Neo4j.
Note
Before we move forward, it would be a good idea to install Neo4j so that we can work on the Cypher queries. Neo4j can be downloaded from http://neo4j.com/download/.
Once installed, we will use the neo4j-shell, a console tool for Neo4j to create nodes and relationships. Further in this book, we will make use of the Neo4j Browser tool, when we start dealing with traversal and exploring our graph. Before trying out any of the queries, ensure that the Neo4j server is started.
- Java應用與實戰
- React Native Cookbook
- MongoDB for Java Developers
- Python Data Analysis(Second Edition)
- 3D少兒游戲編程(原書第2版)
- Getting Started with Gulp
- Spring核心技術和案例實戰
- C語言程序設計實驗指導 (第2版)
- 零基礎Java學習筆記
- R語言數據可視化:科技圖表繪制
- 實戰Java高并發程序設計(第2版)
- jQuery for Designers Beginner's Guide Second Edition
- 嵌入式C編程實戰
- 用Python動手學統計學
- 算法訓練營:海量圖解+競賽刷題(入門篇)