Python连接Neo4j(py2neo)并创建4万节点电影演员关系图

目录

一、启动neo4j

二、Py2neo

三、创建豆瓣电影图数据库

四、总结


一、启动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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小楼一夜听春雨258

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值