neo4j数据可视化
Graph databases are powerful structures to visualize connections between entities that miss a structured format in them. This tutorial is a step by step approach for generating simple graphs using Neo4j.
图形数据库是强大的结构,可可视化实体中缺少结构化格式的实体之间的连接。 本教程是使用Neo4j生成简单图形的分步方法。
目标 (Goal)
We will see how to use Neo4j to set up a quick query engine for generating graphs from a JSON document and also capture a structured format from the generated graph.
我们将看到如何使用Neo4j设置快速查询引擎,以从JSON文档生成图,以及如何从生成的图捕获结构化格式。
数据 (Data)
The data is going to be a custom JSON file that contains information about the characters (heroes, villains, anti-heroes) from the DC and Marvel Universe.
数据将是一个自定义JSON文件,其中包含有关DC和Marvel Universe中角色(英雄,小人,反英雄)的信息。
先决条件 (Prerequisite)
You should have installed the latest version of the Neo4j Desktop in your machine. If not, you can install it from here (depends on the type of OS you’re using).
您应该已经在计算机中安装了Neo4j Desktop的最新版本。 如果没有,您可以从这里安装(取决于您所使用的操作系统类型)。
I will be using the Neo4j Desktop v1.3.4.
我将使用Neo4j Desktop v1.3.4。
STEP 0: Upon successful installation, you will see the following window when you start Neo4j Desktop.
步骤0 :成功安装后,启动Neo4j Desktop时将显示以下窗口。
Step 0: Setting the Database Connection and Plugin Setup 步骤0:设置数据库连接和插件设置STEP 1: Select the +Add Database option. Create a new local database. Give your Graph database a name, and a password that you will use for connecting with the database later. Click Create.
步骤1 :选择+添加数据库选项。 创建一个新的本地数据库。 为您的Graph数据库命名,并提供一个密码,以后将用于与数据库连接。 单击创建。
STEP 1: Creating your first Graph Database :) 步骤1:创建第一个图形数据库:)STEP 2: Hit the ▶️ Start button. This will generate an empty graph with zero nodes and zero relationships. It is an empty graph upon which we will be building soon.
步骤2 :点击▶️开始按钮。 这将生成一个零图和零关系的空图。 这是一个空图,我们将在此图上进行构建。
STEP 3: You will see three dots, on the Database tile. You should click the Manage gear icon ⚙️ and this will take you to the monitoring page of the graph database.
步骤3 :在“数据库”磁贴上,您将看到三个点。 您应该单击管理齿轮图标⚙️,这将带您到图形数据库的监视页面。
STEP 3: Click on the Manage Gear Icon 步骤3:点击管理齿轮图标STEP 4: Navigate to the Plugins tab, and hit the install button on the APOC (Awesome Procedures On Cypher) library section. This library is used to import different file formats to Neo4j and leverage that to a graph representation.
步骤4 :导航到“插件”选项卡,然后单击“ APOC (Cypher上的超赞过程)”库部分上的安装按钮。 该库用于将不同的文件格式导入Neo4j,并将其用于图形表示。
STEP 5: Navigate to the Settings tab, you will find a script file in which you need to add two permission statements:
步骤5 :导航到“设置”选项卡,您将找到一个脚本文件,在其中需要添加两个权限声明:
apoc.import.file.enabled=true
apoc.export.file.enabled=true
STEP 5: Activating the Import/Export capability of APOC package 步骤5:激活APOC软件包的导入/导出功能
Click the Apply button. This completes the setup, now its time to code. Before that, you deserve a cookie !
单击应用按钮。 这样就完成了设置,现在可以进行编码了。 在此之前,您应该得到一个饼干!
做得好! 让我们开始 :) (Good job! Let’s begin :))
数据 (Data)
We will use a JSON file to generate the nodes and relationships we need for the superheroes and villains from Marvel and DC. You can copy the below content and paste it in your code editor:
我们将使用JSON文件生成Marvel和DC的超级英雄和恶棍所需的节点和关系。 您可以复制以下内容并将其粘贴到代码编辑器中:
Data for the Graph Database 图形数据库的数据To place the file, go to the Manage tab from the home page (Step 5) and select the Open folder - Import option and this will open the import directory where you can place the JSON file for this tutorial. After all the configuration setup, you can start the database and open the Neo4j browser.
要放置文件,请从主页转到“管理”选项卡(第5步),然后选择“打开文件夹-导入”选项,这将打开导入目录,您可以在其中放置本教程的JSON文件。 完成所有配置设置后,您可以启动数据库并打开Neo4j浏览器。
A quick note about the data, it contains the squad/anti-squad of heroes and anti-heroes from the Marvel and DC Universe with suitable properties assigned to each character. We can create a mapping between them as who all acted in the same movie to start with as a query in Neo4j.
关于数据的快速注释,其中包含来自漫威和DC宇宙的英雄和反英雄小队/反小队,并为每个角色分配了适当的属性。 我们可以在他们之间创建一个映射,就像谁都扮演同一部电影一样,从Neo4j中的查询开始。
让我们编码 (Let’s Code )
1.创建一个简单图 (1. Creating a Simple Graph)
Graphs, in general, have two main characteristics: Nodes, and Relationships.
通常,图具有两个主要特征:节点和关系。
Nodes can be considered as the basic entity like an object with a label and some properties (metadata) attached to it. Multiple nodes are connected with others by a relationship with a label and properties (metadata) too. Multiple nodes and relationships together make the graph database.
节点可以被视为基本实体,就像带有标签和附加了一些属性(元数据)的对象一样。 多个节点也通过与标签和属性(元数据)之间的关系相互连接。 多个节点和关系共同构成了图形数据库。
The relationship can have a sense of direction indicated by an arrow. For example, we can denote that Iron Man hated Thanos using the following code
该关系可以具有箭头指示的方向感。 例如,我们可以使用以下代码来表示钢铁侠讨厌Thanos
Simple CREATE query in Cypher 在Cypher中进行简单的CREATE查询To run the code, you can use Ctrl+Enter or Cmd+Enter.
要运行代码,可以使用Ctrl + Enter或Cmd + Enter。
The general syntax for creating a node-to-node relation is as follows:
创建节点到节点关系的一般语法如下:
(node1:Label{key:value,..})-[relationship:Label]->(node2:Label{key:value,..})
(node1:Label {key:value,..})-[relationship:Label]->(node2:Label {key:value,..})
To visualize a graph run: (remember this command)
可视化图形运行:(请记住此命令)
MATCH (n) RETURN (n)
Result of a simple CREATE query 简单的CREATE查询的结果
Following this, we can add more nodes and relationships to the existing graph to make an exciting graph from it. Let’s use the JSON data for this.
接下来,我们可以向现有图形添加更多的节点和关系,从而从中制作出令人兴奋的图形。 让我们为此使用JSON数据。
2.从JSON文件生成图 (2. Generating Graph from a JSON file)
The filedata.json
can be read using the following Cypher query which is the standard querying language in Neo4j.
可以使用以下Cypher查询(Neo4j中的标准查询语言)读取文件data.json
。
Once we are able to get the JSON file to the Graph database, we can now convert that to a tabular structure, create relationships between various levels of the structure in the data.
一旦能够将JSON文件获取到Graph数据库,我们现在就可以将其转换为表格结构,在数据中结构的各个级别之间创建关系。
Query to create the Nodes 查询以创建节点Lines 1 and 2 are used to load the JSON data into Neo4j and store it in a variable value
.This is then unwrapped using the clauseUNWIND
in Line 3. So, we have the entire Marvel child-dictionary in the variable v
. We then store the information of Heroes&Villains and Anti-Heroes in two variables, hv
and ahv
respectively in Line 4.
第1行和第2行用于将JSON数据加载到Neo4j中并将其存储在变量value
。然后使用第UNWIND
行中的UNWIND
子句将其解包。因此,变量v
有整个Marvel子词典。 然后,我们将第4行中的变量hv
和ahv
分别ahv
&Villains和Anti-Heroes的信息。
Lines 5 through 7 are used to parse through each list of characters and create three classes of nodes {HERO, VILLAIN, ANTIHERO} which have an attached property to them like name, superpower, otherside, quotes, and the universe.
第5至7行用于解析每个字符列表,并创建三类节点{HERO,VILLAIN,ANTIHERO},它们具有附加的属性,例如name , superpower , otherside , quotes和Universe。
The FOREACH
clause is used to get each item from the list of characters and generate a Node. The | character is used to pass the result from one clause to the next, here the result from FOREACH
is fed to MERGE
clause. This clause will create the nodes just like the combination of CREATE
clause and MATCH
clause. It will avoid the creation of duplicate nodes by checking the database for nodes with the same label. If it exists it will not create the duplicate one.
FOREACH
子句用于从字符列表中获取每个项目并生成一个Node 。 | 字符用于将结果从一个子句传递到下一个子句,此处将FOREACH
的结果馈送到MERGE
子句。 该子句将创建节点,就像CREATE
子句和MATCH
子句的组合一样。 通过检查数据库中具有相同标签的节点,将避免创建重复的节点。 如果存在,将不会创建重复的副本。
Running this command we can get the graph generated for the heroes, villains, and anti-heroes.
运行此命令,我们可以获得为英雄,恶棍和反英雄生成的图形。
MATCH (h:HERO), (v:VILLAIN), (ahv:ANTIHERO)
RETURN h, v, ahv
When we visualize the nodes, we get a simple graph
当我们可视化节点时,我们得到一个简单的图
Graph of Marvel Characters 奇迹人物图3.在角色之间添加关系(3. Adding Relationships between the Characters)
Now, we have done the crucial part of creating the Nodes in the database, we should connect each of them with a relationship. This can only be a directed relationship. Neo4j does not support removing directions while creating a relationship, although we can avoid it while querying.
现在,我们已经完成了在数据库中创建节点的关键部分,我们应该将每个节点与一个关系连接起来。 这只能是直接关系。 Neo4j不支持在创建关系时删除路线,尽管我们可以在查询时避免这样做。
This is done by using the MATCH and CREATE clauses. The direction is shown from left to right in Line 11 which can be read as
这是通过使用MATCH和CREATE子句完成的。 方向在第11行中从左到右显示,可以理解为
Spiderman is the god-son of Ironman.
蜘蛛侠是钢铁侠的神父。
Line 12, we create a right to left arrow from Ironman to Spiderman which is the equivalent of
第12行,我们创建了从Ironman到Spiderman的向右箭头,等效于
Ironman is the god-father of Spiderman.
钢铁侠是蜘蛛侠的神父。
We can also create relationships on a node that points to itself like in Line 7 and Line 13.
我们还可以在指向自身的节点上创建关系,如第7行和第13行。
Query to generate the relationships 查询以生成关系 Final Graph with Relationships (Heroes vs. Villains) 具有关系的最终图形(英雄与恶棍)If you notice, we have left the DC universe from this graph, let this be an exercise for you to work on a simple graph using the data.json
file and create interesting relationships from scratch. You can try building something similar or a more complicated graph using what we have seen in this tutorial.
如果您注意到了,我们已经从该图中离开了DC Universe ,让我们练习一下如何使用data.json
文件在一个简单的图中工作,并从头开始创建有趣的关系。 您可以尝试使用本教程中看到的内容来构建相似或更复杂的图形。
结论 (Conclusion)
Congratulations! You have learned how to use the basic Cypher query clauses to create graphs from a JSON file. I hope you had fun programming using Neo4j.
恭喜你! 您已经了解了如何使用基本的Cypher查询子句从JSON文件创建图。 希望您使用Neo4j玩得开心。
As food for thought. One way, this can be leveraged is by mining API responses in the form of a JSON and converting them to nodes and relationships.
令人深思。 一种可以利用的方式是通过以JSON形式挖掘API响应并将其转换为节点和关系。
The data and queries used in this tutorial can be found in this repository.
在本资料库中可以找到本教程中使用的数据和查询。
Feel free to experiment with the data, add more nodes, relations, and extend the tree!
随意尝试数据,添加更多节点,关系并扩展树!
I am planning to write more articles in the coming days about general programming concepts, data engineering, machine learning, basic data science concepts, and other analytic methodologies.
我计划在未来几天写更多有关通用编程概念,数据工程,机器学习,基本数据科学概念和其他分析方法的文章。
Feel free to connect with me on LinkedIn and GitHub.
随时在LinkedIn和GitHub上与我联系。
This is my first article on Medium, your constructive comments and thoughts will help me in writing better articles. Feel free to reach out to me!
这是我的第一篇有关中型的文章,您的建设性评论和想法将有助于我撰写更好的文章。 欢迎与我联系!
翻译自: https://medium.com/swlh/how-i-used-neo4j-to-visualize-connections-from-semi-structured-data-1dc841c44f20
neo4j数据可视化