无法在浏览器中通过Cypher查询查看使用py2neo创建的节点
我正在使用py2neo创建节点,代码如下:
from py2neo import neo4j
graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data")
print graph_db.neo4j_version
graph_db.clear()
if not graph_db.get_index(neo4j.Node, "Kiran"):
from py2neo import node,rel
trial = graph_db.create(
node(name="Kiran"),
node(name="teja"),
rel(0, "Brother", 1),
)
#else:
details = graph_db.get_index(neo4j.Node, "Kiran")
print details
get_index这个函数给我返回了一些数据,比如:
Index(Node, u'http://localhost:7474/db/data/index/node/Kiran')
但是当我在浏览器中搜索这个节点时,却什么都找不到……我是不是哪里做错了?
另外,我还想发布一些网络信息,代码如下:
from py2neo import neo4j
from py2neo import node,rel
graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data")
def nodepublish(dpid, port, mac):
if graph_db.get_index( neo4j.Node, dpid ) == None:
create = graph_db.create({"DPID": dpid})
print "switch "+str(dpid)+" added to graph"
if graph_db.get_index( neo4j.Node, mac ) == None:
query = neo4j.CypherQuery(graph_db, "MATCH (sw) WHERE sw.DPID = "+str(dpid)+" CREATE (nd {MAC: "+str(mac)+"}) CREATE (sw)-[:connected {PORT: "+str(port)+"}]->(nd)")
print "node "+str(mac)+" added to graph"
当我调用nodepublish()这个函数时,
nodepublish(1,1,"aa:aa:aa:aa:aa:aa")
它每次都会创建一个新的节点,dpid总是1,而不是在get_index返回不为空的时候跳过这个if语句。
有人能帮我解决这个问题吗?
谢谢!
1 个回答
2
第一点:确保你的GraphDatabaseService的URI后面有一个斜杠。如果没有,可能会得到错误的结果。
第二点:你在这里使用的是旧版索引。要清楚你使用的是哪种类型的索引,可以阅读这篇文章。
我觉得你可能搞混了索引和索引条目。一个索引(在这个例子中可能叫做People
)指向一组条目,每个条目都有一个键值对来标识。在每个条目处,你可以引用一个或多个节点。想了解更多关于旧版索引的内容,可以查看这里。
你可能希望你的代码看起来更像这样:
from py2neo import neo4j
graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")
# Create a "People" index if one doesn't already exist
people = graph_db.get_or_create_index(neo4j.Node, "People"):
# Create two people nodes if they don't already exist
kiran = people.get_or_create("name", "Kiran", {"name": "Kiran"})
teja = people.get_or_create("name", "Teja", {"name": "Teja"})
# Relate the two
brothers, = graph_db.create((kiran, "BROTHER", teja))
print kiran
print teja
print brothers
这篇页面可能会帮助你了解代码中的一些细节,因为它描述了你在这里需要的旧版索引功能。