简体   繁体   English

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

[英]cannot reproduce bulbs/py2neo in python to create edges (relationships)

I am trying to sequentially insert vertices and edges in neo4j using python. 我试图使用python顺序插入neo4j中的顶点和边。 The existing nodes aren't recognised as such when I add edges. 添加边时,现有节点不会被识别。 Whether I use py2neo or bulbs I got a similar error message. 无论我使用py2neo还是灯泡,我都收到了类似的错误消息。

Note I am working with: linux64 python2.7 bulbs0.3 py2neo1.5 neo4j-community1.8.2 注意我正在使用:linux64 python2.7 bulbs0.3 py2neo1.5 neo4j-community1.8.2

With bulbs: 用灯泡:

>>> 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

With py2neo 用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

Moreover I got the same error with bulbs if I use rexster instead of neo4j , ie 此外,如果我使用rexster而不是neo4j ,我得到了与bulbs相同的错误,即

>>> 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

What's wrong here? 这有什么不对?

Thanks 谢谢

Your application variables james and julie won't automatically be created simply by creating nodes with a similar name property. 您的应用程序变量jamesjulie不会通过创建具有类似name属性的节点自动创建。 You haven't shared any of your py2neo code and I'm not familiar with bulbs but within py2neo you will need to do something like: 您还没有共享任何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))

You could of course instead create both nodes and relationship in the same statement (and batch) if you preferred: 当然,如果您愿意,您可以在同一语句(和批处理)中创建节点和关系:

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

You're not setting the james or julie vars on your create statements. 您没有在创建语句中设置jamesjulie变量。

Here's the proper code: 这是正确的代码:

>>> 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)

See the Bulbs Quickstart for more examples. 有关更多示例,请参阅灯泡快速入门

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM