我在使用 Neo4j 的 Neomodel 和 py2neo 客户端时遇到了一些问题。我已经在单独的 anaconda 虚拟环境中安装了 Neomodel 和 py2neo,并分别进行了测试。Neo4j 使用 docker 安装/停靠。
新模式
编码
from neomodel import (config, StructuredNode, StringProperty, IntegerProperty,UniqueIdProperty, RelationshipTo, RelationshipFrom)
config.DATABASE_URL = 'bolt://neo4j:password@localhost:7687'
class Country(StructuredNode):
code = StringProperty(unique_index=True, required=True)
# traverse incoming IS_FROM relation, inflate to Person objects
inhabitant = RelationshipFrom('Person', 'IS_FROM')
class Person(StructuredNode):
uid = UniqueIdProperty()
name = StringProperty(unique_index=True)
age = IntegerProperty(index=True, default=0)
# traverse outgoing IS_FROM relations, inflate to Country objects
country = RelationshipTo(Country, 'IS_FROM')
jim = Person(name='Jim', age=3).save()
jim.age = 4
jim.save() # validation happens here
jim.delete()
jim.refresh() # reload properties from neo
jim.id # neo4j internal id
Neomodel 生成在 neo4j webapp 上查看的节点。创建的节点是年龄 = 3 的 Jim,即它似乎没有记录 Jim 年龄从 3 -> 4 变化的事实。另外,我假设 jim.delete() 会删除它没有的节点. 最后,它会提示以下错误(下面是错误最后几行的片段)。
错误
...
File "/Users/sjamal/.conda/envs/tneo/lib/python3.6/site-
packages/neomodel/core.py", line 452, in inflate
if db_property in node.properties:
AttributeError: 'Node' object has no attribute 'properties'
现在我确实找到了这篇帖子,其中用户“Jack Daniel”提到 neomodel 不支持 neo4j 3。所以我尝试对接 Neo4j v.2.3 图像,但随后收到以下错误(请注意,它是最后几行的片段的错误)
对接图像 Neo4j 2.3 时出错
File "/Users/sjamal/.conda/envs/tneo/lib/python3.6/ssl.py", line 817, in __init__
self.do_handshake()
File "/Users/sjamal/.conda/envs/tneo/lib/python3.6/ssl.py", line 1077, in do_handshake
self._sslobj.do_handshake()
File "/Users/sjamal/.conda/envs/tneo/lib/python3.6/ssl.py", line 689, in do_handshake
self._sslobj.do_handshake()
OSError: [Errno 0] Error
Py2neo
由于 Neomodel 的问题,我开始考虑使用 p2neo,但我似乎无法正确配置。
编码
from py2neo import Node, Relationship, Graph
graph = Graph("localhost", user='neo4j', password='password', bolt=None)
alice = Node("Person", name="Alice")
bob = Node("Person", name="Bob")
alice_knows_bob = Relationship(alice, "KNOWS", bob)
graph.create(alice_knows_bob)
错误
File "/Users/sjamal/.conda/envs/py2neo_test/lib/python3.6/site-packages/neo4j/bolt/connection.py", line 459, in acquire
connection = self.connector(address)
File "/Users/sjamal/.conda/envs/py2neo_test/lib/python3.6/site-packages/neo4j/v1/bolt.py", line 46, in <lambda>
pool = ConnectionPool(lambda a: connect(a, security_plan.ssl_context, **config))
File "/Users/sjamal/.conda/envs/py2neo_test/lib/python3.6/site-packages/neo4j/bolt/connection.py", line 601, in connect
raise ProtocolError("Connection to %r closed without handshake response" % (address,))
neo4j.bolt.connection.ProtocolError: Connection to ('localhost', 7687) closed without handshake response
感谢任何对此进行调查的人。我很乐意收到有关如何设置 Py2neo 的任何建议或解释,无论我是否让 Neomodel 工作。