目录
一、启动neo4j
windows系统中,首先切换到 Neo4j bin目录,然后运行命令启动 Neo4j:neo4j.bat console。
cd /d D:\neo4j-community-3.5.5\bin
neo4j.bat console
二、Py2neo
Py2neo 是一个用于与 Neo4j 图数据库交互的 Python 客户端库和工具集。它为 Python 应用程序提供了与 Neo4j 图数据库交互的功能,支持 Bolt 和 HTTP 协议,并提供了一套高级 API、对象图映射(OGM)、管理工具、Cypher 词法分析器等。
基于Py2neo库可以对neo4j图数据库进行增删改查等操作,代码如下:
from typing import Union
from py2neo import Graph, Node, Relationship
class Neo4jDatabase:
def __init__(self, username, password, uri="bolt://localhost:7687"):
self.graph = Graph(uri, auth=(username, password))
def create_node(self, label: str, node_name: str, **properties) -> Node:
"""
创建一个节点,并可以输入节点的属性信息
"""
node = Node(label, name=node_name, **properties)
self.graph.create(node)
return node
def create_relationship(self,
start_node: Union[int, Node],
relationship_type: str,
end_node: Union[int, Node],
**properties) -> Relationship:
"""
创建两个节点之间的关系
输入节点ID或节点本身
"""
if isinstance(start_node, int):
start_node = self.graph.nodes.get(start_node)
if isinstance(end_node, int):
end_node = self.graph.nodes.get(end_node)
relationship = Relationship(start_node, relationship_type, end_node, **properties)
self.graph.create(relationship)
return relationship
def delete_node(self, node: U