赞
踩
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")
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)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。