目录
- 安装
- 练习节点和关系结构
- 连接数据库
- 添加节点
-
- [1. Cypher](#1. Cypher)
- [2. Py2neo](#2. Py2neo)
- 添加关系
-
- [1. Cypher](#1. Cypher)
- [2. Py2neo](#2. Py2neo)
- 批量添加
-
- [1. Cypher](#1. Cypher)
- [2. Py2neo](#2. Py2neo)
- 查询
-
- [1. Cypher](#1. Cypher)
- [2. Py2neo](#2. Py2neo)
- 更新
-
- [1. Cypher](#1. Cypher)
- [2. Py2neo](#2. Py2neo)
- 删除
-
- [1. Cypher](#1. Cypher)
- [2. Py2neo](#2. Py2neo)
安装
这边直接下载了Neo4j Desktop+安装py2neo,安装后就可直接使用
python
pip install py2neo
练习节点和关系结构
类型 | 字段1 | 字段2 | 字段3 | 字段4 |
---|---|---|---|---|
node | id | name | type | industry |
link | source | relation | target | - |
连接数据库
Desktop直接打开,用Neo4j Browser打开就行,很方便
下面这个是使用Py2neo库链接数据库
python
from py2neo import Graph, Node, Relationship
# 连接到Neo4j
graph = Graph("bolt://localhost:7687", auth=("neo4j", "password"))
添加节点
1. Cypher
在Neo4j Browser中可以直接输入命令
bash
CREATE (n:Node {id:1, name:'Alice', type:'Person', industry:'software'})
2. Py2neo
python
# 创建节点
a = Node("Node", id=1, name="Alice", type="Person", industry="software")
graph.create(a)
b = Node("Node", id=2, name="Bom", type="Person", industry="hardware")
graph.create(b)
添加关系
1. Cypher
bash
MATCH (a:Node),(b:Node)
WHERE a.name = 'Alice' AND b.name = 'Bom'
CREATE (a)-[r:KNOWS] -> (b)
RETURN r
2. Py2neo
这里的a,b是承接上一部分的添加节点的代码
python
# 创建关系
rel = Relationship(a, "KNOWS", b)
graph.create(rel)
批量添加
本人测试下来,这两种方法,后者速度更快
1. Cypher
这边的文件得放在neo4j文件夹下的import文件夹里
node
LOAD CSV WITH HEADERS FROM 'file:///nodes.csv' AS row
CREATE (n:Node {id: row.id, name: row.name, type: row.type, industry: row.industry});
relation
python
LOAD CSV WITH HEADERS FROM 'file:///links.csv' AS row
MATCH (source:Node {id: row.source}), (target:Node {id: row.target})
MERGE (source)-[r:RELATIONSHIP {type: row.relation}]->(target);
2. Py2neo
node
python
import pandas as pd
from tqdm import tqdm
# 读取CSV文件
df = pd.read_csv("path/to/your/nodes.csv")
# 使用tqdm显示进度条
for index, row in tqdm(df.iterrows(), total=df.shape[0]):
# 创建节点,这里假设CSV有'name', 'type', 'industry'列
node = Node("Node", id=row['id'], name=row['name'], type=row['type'], industry=row['industry'])
# 将节点添加到图中
graph.create(node)
relation
python
# 读取关系CSV文件
df_relations = pd.read_csv("path/to/your/relations.csv")
for index, row in df_relations.iterrows():
source_node = matcher.match("Node", id=row['source']).first()
target_node = matcher.match("Node", id=row['target']).first()
if source_node and target_node:
rel = Relationship(source_node, row['relation'], target_node)
graph.create(rel)
查询
1. Cypher
python
MATCH (n:Node)
WHERE n.industry = 'software'
RETURN n
2. Py2neo
python
# 查询所有类型为"Person"的节点
query = "MATCH (n:Node) WHERE n.industry = 'software' RETURN n"
nodes = graph.run(query)
for node in nodes:
print(node)
更新
1. Cypher
python
MATCH (n:Node {name: 'Alice'})
SET n.industry = 'IT'
RETURN n
2. Py2neo
python
# 查找名为Alice的节点并更新其行业
alice = matcher.match("Node", name="Alice").first()
if alice:
alice['industry'] = 'IT'
graph.push(alice)
删除
1. Cypher
python
MATCH (n:Node)-[r:KNOWS]->()
DELETE r
2. Py2neo
方法一:查询节点后删除
python
# 删除名为Bom的节点
bom = graph.nodes.match("Node", name="Bom").first()
graph.delete(bom)
方法二:运行CYPHER语句
python
#删除特定属性的节点,例如删除所有industry为"hardware"的节点
query = "MATCH (n:Node {industry: 'hardware'}) DELETE n"
graph.run(query)
# 删除特定类型的关系,例如删除所有KNOWS关系
query = "MATCH ()-[r:KNOWS]-() DELETE r"
graph.run(query)
如果有帮助到你,能点个赞嘛!!谢谢!!!