• Neo4j学习笔记1:增删改查


    安装

    这边直接下载了Neo4j Desktop+安装py2neo,安装后就可直接使用

    pip install py2neo
    
    • 1

    练习节点和关系结构

    类型字段1字段2字段3字段4
    nodeidnametypeindustry
    linksourcerelationtarget-

    连接数据库

    Desktop直接打开,用Neo4j Browser打开就行,很方便
    下面这个是使用Py2neo库链接数据库

    from py2neo import Graph, Node, Relationship, NodeMatcher
    
    # 连接到Neo4j
    graph = Graph("bolt://localhost:7687", auth=("neo4j", "password"))
    
    • 1
    • 2
    • 3
    • 4

    添加节点

    1. Cypher

    在Neo4j Browser中可以直接输入命令

    CREATE (n:Node {id:1, name:'Alice', type:'Person', industry:'software'})
    
    • 1

    2. Py2neo

    # 创建节点
    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
    • 2
    • 3
    • 4
    • 5

    添加关系

    1. Cypher

    MATCH (a:Node),(b:Node)
    WHERE a.name = 'Alice' AND b.name = 'Bom'
    CREATE (a)-[r:KNOWS] -> (b)
    RETURN r
    
    • 1
    • 2
    • 3
    • 4

    2. Py2neo

    这里的a,b是承接上一部分的添加节点的代码

    # 创建关系
    rel = Relationship(a, "KNOWS", b)
    graph.create(rel)
    
    • 1
    • 2
    • 3

    批量添加

    本人测试下来,这两种方法,后者速度更快

    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});
    
    
    • 1
    • 2
    • 3

    relation

    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);
    
    
    • 1
    • 2
    • 3
    • 4

    2. Py2neo

    node

    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    relation

    # 读取关系CSV文件
    df_relations = pd.read_csv("path/to/your/relations.csv")
    matcher = NodeMatcher(graph)
    
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    查询

    1. Cypher

    MATCH (n:Node) 
    WHERE n.industry = 'software' 
    RETURN n
    
    • 1
    • 2
    • 3

    2. Py2neo

    # 查询所有类型为"Person"的节点
    query = "MATCH (n:Node) WHERE n.industry = 'software' RETURN n"
    nodes = graph.run(query)
    for node in nodes:
        print(node)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    更新

    1. Cypher

    MATCH (n:Node {name: 'Alice'})
    SET n.industry = 'IT'
    RETURN n
    
    • 1
    • 2
    • 3

    2. Py2neo

    # 查找名为Alice的节点并更新其行业
    alice = matcher.match("Node", name="Alice").first()
    if alice:
        alice['industry'] = 'IT'
        graph.push(alice)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    删除

    1. Cypher

    MATCH (n:Node)-[r:KNOWS]->()
    DELETE r
    
    • 1
    • 2

    2. Py2neo

    方法一:查询节点后删除

    # 删除名为Bom的节点
    bom = graph.nodes.match("Node", name="Bom").first()
    graph.delete(bom)
    
    • 1
    • 2
    • 3

    方法二:运行CYPHER语句

    #删除特定属性的节点,例如删除所有industry为"hardware"的节点
    query = "MATCH (n:Node {industry: 'hardware'}) DELETE n"
    graph.run(query)
    
    # 删除特定类型的关系,例如删除所有KNOWS关系
    query = "MATCH ()-[r:KNOWS]-() DELETE r"
    graph.run(query)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    如果有帮助到你,能点个赞嘛!!谢谢!!!

  • 相关阅读:
    爬虫工程师基本功,前端基础
    像追女神一样学好java~
    【c语言进阶】深入挖掘数据在内存中的存储
    Unity学习shader笔记[一百]简单焦散Caustic效果
    File的常见成员方法
    面试其他注意事项
    Spring Tool Suite(STS)初始化配置记录
    File类和IO流的相关面试(二)
    FreeRTOS 任务任务同步与数据传递总结
    块级元素设置背景颜色后,背景颜色直接占据一整行的处理
  • 原文地址:https://blog.csdn.net/qq_40044912/article/details/136259064