当前位置:   article > 正文

Python 操作 Neo4j_neo4j 打印执行语句

neo4j 打印执行语句

1.neo4j模块:执行CQL ( cypher ) 语句

from neo4j import GraphDatabase


driver = GraphDatabase.driver("bolt://localhost:7687",auth=("root","root"))

def add_point(tx,name):
    tx.run("CREATE (A:Person{name:$name}) "
           , name=name)


def add_friend(tx,name,friend_name):
    tx.run("MERGE (A:Person{name:$name}) "
           "-[:KNOWS]->(friend:Person{name:$friend_name})",name=name,friend_name=friend_name)


def print_friends(tx, name):
    for record in tx.run("MATCH (a:Person)-[:KNOWS]->(friend) WHERE a.name = $name "
                         "RETURN friend.name ORDER BY friend.name", name=name):
        print(record["friend.name"])


with driver.session() as session:
    # session.write_transaction(add_point, "Arthur")
    # session.write_transaction(add_point, "Guinevere")
    # session.write_transaction(add_point, "Lancelot")
    # session.write_transaction(add_point, "Merlin")
    session.write_transaction(add_friend,"Arthur","Guinevere")
    session.write_transaction(add_friend, "Arthur", "Lancelot")
    session.write_transaction(add_friend, "Arthur", "Merlin")
    session.read_transaction(print_friends, "Arthur")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

2.py2neo模块:通过操作python变量,达到操作neo4j的目的

from py2neo import Node,Graph,Relationship,NodeMatch

#构建图
g = Graph()
g.delete_all()

#添加节点
a = Node("Person",name = "Alice")
g.create(a)

#添加节点
b = Node("Person",name = "Bob")
g.create(b)

#添加联系,边
ab = Relationship(a,"KNOWS",b)
g.create(ab)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号