使用python包(Neomodel和py2neo)与Neo4j的问题

问题描述 投票:0回答:1

我在与Neo4j使用Neomodel和py2neo客户端时遇到了一些问题。我在单独的anaconda虚拟环境中安装了Neomodel和py2neo,并分别进行了测试。 Neo4j使用docker安装/停靠。

Neomodel

代码

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上查看的节点。创建的节点是Jim,年龄= 3,即它似乎没有记录Jims年龄从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'

现在我确实找到了这个帖子,其中用户“杰克丹尼尔”提到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工作。

neo4j py2neo neomodel
1个回答
0
投票

所以我设法用Py2neo解决了我的问题,但没有解决我与Neomodel的问题。如果我确实找到了让Neomodel工作的方法,我会发布它并链接到这篇帖子或在这个帖子中发表评论。

P2neo解决方案与p2neo hf.0和neochy uz.o相关

我尝试了各种组合,从neo4j 2.3开始,连同不同版本的py2neo,如3.1.2,然后用neo4j v3.0做同样的事情。

我正在发布我用于创建节点和图形连接的脚本,因为我在试图弄清楚我是否设置配置不佳或包装,驱动程序等中存在错误时会发疯。

Py2neo脚本

from py2neo import Node, Relationship, Graph                                       

graph = Graph('http://localhost:7474/db/data',user='neo4j',pass word='password1234')
tx = graph.begin()                                                                 
a = Node(label='hero',name='Sabri')                                                
tx.create(a)                                                                       
tx.commit()  

过时的驱动程序py2neo v3.1.2与Neo4j v3.4串联

正如本Github问题报告https://github.com/neo4j/neo4j-python-driver/issues/252中所讨论的,报告该问题的用户正在使用py2neo 3.1.2和Neo4jv3.4。怀疑是由于py2neo 3.1.2附带的过时驱动程序(v1.1)。 Neo4j v3.4的新发行版似乎附带了新的驱动程序1.6。

将py2neo升级到v4.0并坚持使用最新版本的Neo4j服务器,即v3.4

这样做时我遇到了一个不同的错误

File "/Users/sjamal/.conda/envs/py2neo.v4/lib/python3.6/site-packages/py2neo/internal/http.py", line 26, in <module>
from neo4j.addressing import SocketAddress
ModuleNotFoundError: No module named 'neo4j.addressing'

在这个stackoverflow线程(ModuleNotFoundError: No module named 'neo4j.addressing' and ModuleNotFoundError: No module named 'neo4j')中讨论过,问题可能是驱动程序1.6驱动程序可能必须通过pip手动安装,我这样做了。

pip install neo4j-driver==1.6.2

我现在收到一个新错误,其中在调用map对象时捕获了TypeError。

File "/Users/sjamal/.conda/envs/py2neo.v4/lib/python3.6/site-packages/py2neo/internal/http.py", line 74, in fix_parameters
raise TypeError("Parameters of type {} are not supported".format(type(value).__name__))

TypeError:不支持类型为map的参数

我发现speters-cmri https://github.com/technige/py2neo/issues/688发布了这个github问题,它包含以下github提交(https://github.com/technige/py2neo/compare/v4...CMRI-ProCan:v4),通过修改py2neo包中的json.py脚本来解决问题

我再次运行我的脚本来添加测试节点,它运行没有任何问题。

如果你太懒或太沮丧而无法通过长篇解释这里是一个总结

1. Make sure neo4j v3.0+ is installed. I suggest you look into docker to install neo4j using a docker image

2. pip install py2neo==v4.0
3. pip install neo4j-driver==1.6.2

4. Modify json.py file as described here https://github.com/technige/py2neo/compare/v4...CMRI-ProCan:v4

5. Run py2neo script outlined above
© www.soinside.com 2019 - 2024. All rights reserved.