无法在python中重现灯泡/ py2neo来创建边缘(关系)

时间:2013-04-27 10:23:27

标签: python neo4j graph-databases bulbs

我正在尝试使用python在neo4j中顺序插入顶点和边。添加边时,现有节点不会被识别。无论我使用py2neo还是灯泡,我都收到了类似的错误消息。

注意我正在使用: LINUX64 python2.7 bulbs0.3 py2neo1.5 的Neo4j-community1.8.2

用灯泡:

>>> from bulbs.neo4jserver import Graph
>>> g = Graph()
>>> g.vertices.create(name="James")
>>> g.vertices.create(name="Julie")
>>> g.edges.create(james, "knows", julie)

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-46-9ba24256218d> in <module>()
----> 1 g.edges.create(james, "knows", julie)

NameError: name 'james' is not defined

使用py2neo

from py2neo import neo4j
graph=neo4j.GraphDatabaseService()
node=graph.create({"name":'James'},{'name':'Julie'})
rel=graph.create((james,"knows",julie))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-591f826cfd05> in <module>()
      2 graph=neo4j.GraphDatabaseService()
      3 node=graph.create({"name":'James'},{'name':'Julie'})
----> 4 rel=graph.create((james,"knows",julie))

NameError: name 'james' is not defined

此外,如果我使用bulbs代替rexster,我的neo4j也会出现同样的错误,即

>>> from bulbs.rexster import Graph
>>> g = Graph()
>>> g.vertices.create(name="James")
>>> g.vertices.create(name="Julie")
>>> g.edges.create(james, "knows", julie)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-2cfb5faa42d1> in <module>()
      3 g.vertices.create(name="James")
      4 g.vertices.create(name="Julie")
----> 5 g.edges.create((james, "knows", julie))

NameError: name 'james' is not defined

这里有什么问题?

由于

2 个答案:

答案 0 :(得分:2)

您的应用程序变量jamesjulie不会仅通过创建具有类似name属性的节点自动创建。您还没有共享任何py2neo代码,我不熟悉灯泡,但在py2neo中,您需要执行以下操作:

from py2neo import neo4j
graph_db = neo4j.GraphDatabaseService()
james, julie = graph_db.create(node(name="James"), node(name="Julie"))
graph_db.create(rel(james, "KNOWS", julie))

如果您愿意,您当然可以在同一语句(和批处理)中创建节点和关系:

from py2neo import neo4j
graph_db = neo4j.GraphDatabaseService()
james, julie, friendship = graph_db.create(
    node(name="James"), node(name="Julie"), rel(0, "KNOWS", 1)
)

答案 1 :(得分:1)

您没有在创建语句中设置jamesjulie变量。

这是正确的代码:

>>> from bulbs.neo4jserver import Graph
>>> g = Graph()
>>> james = g.vertices.create(name="James")
>>> julie = g.vertices.create(name="Julie")
>>> g.edges.create(james, "knows", julie)

有关更多示例,请参阅Bulbs Quickstart