如何在py2neo中正确创建一个节点?
How to create one node in py2neo correctly?
我刚刚测试了py2neo演示,但是失败了,代码是:
from py2neo.data import Node, Relationship
import py2neo
g = py2neo.Graph('http:/172.18.0.52:7474', user='neo4j',password='123')
a = Node("Person", name="Alice")
b = Node("Person", name="Bob")
ab = Relationship(a, "KNOWS", b)
print(a)
g.create(a)
输出是:(:Person {name: 'Alice'})
追溯是:
TypeError Traceback (most recent call
last)
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/internal/http.py
in fix_parameters(parameters)
70 try:
---> 71 dehydrated, = dehydrator.dehydrate([parameters])
72 except TypeError as error:
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/internal/json.py
in dehydrate(self, values)
121
--> 122 return tuple(map(dehydrate_, values))
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/internal/json.py
in dehydrate_(obj)
117 elif isinstance(obj, dict):
--> 118 return {key: dehydrate_(value) for key, value in obj.items()}
119 else:
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/internal/json.py
in (.0)
117 elif isinstance(obj, dict):
--> 118 return {key: dehydrate_(value) for key, value in obj.items()}
119 else:
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/internal/json.py
in dehydrate_(obj)
119 else:
--> 120 raise TypeError(obj)
121
TypeError:
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call
last) in ()
10
11 print(a)
---> 12 g.create(a)
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/database.py
in create(self, subgraph)
343 """
344 with self.begin() as tx:
--> 345 tx.create(subgraph)
346
347 def delete(self, subgraph):
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/database.py
in create(self, subgraph)
919 raise TypeError("No method defined to create object %r" % subgraph)
920 else:
--> 921 create(self)
922
923 def delete(self, subgraph):
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/data.py in
db_create(self, tx)
612
613 def db_create(self, tx):
--> 614 create_subgraph(tx, self)
615
616 def db_delete(self, tx):
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/internal/operations.py
in create_subgraph(tx, subgraph)
133 for labels, nodes in _node_create_dict(n for n in subgraph.nodes if n.graph is None).items():
134 identities = _create_nodes(tx, labels, map(dict, nodes))
--> 135 for i, identity in enumerate(identities):
136 node = nodes[i]
137 node.graph = graph
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/internal/operations.py
in _create_nodes(tx, labels, data)
84 label_string = "".join(":" + cypher_escape(label) for label in sorted(labels))
85 cypher = "UNWIND $x AS data CREATE (_%s) SET _ = data RETURN id(_)" % label_string
---> 86 for record in tx.run(cypher, x=data):
87 yield record[0]
88
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/database.py
in run(self, cypher, parameters, **kwparameters)
838 try:
839 if self.transaction:
--> 840 result = self.transaction.run(cypher, parameters, **kwparameters)
841 else:
842 result = self.session.run(cypher, parameters, **kwparameters)
~/miniconda2/envs/py35/lib/python3.5/site-packages/neo4j/v1/api.py in
run(self, statement, parameters, **kwparameters)
603 if self.closed():
604 raise TransactionError("Transaction closed")
--> 605 return self.session.run(statement, parameters, **kwparameters)
606
607 def sync(self):
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/internal/http.py
in run(self, statement, parameters, **kwparameters)
288 self._statements.append(OrderedDict([
289 ("statement", ustr(statement)),
--> 290 ("parameters", fix_parameters(dict(parameters or {}, **kwparameters))),
291 ("resultDataContents", ["REST"]),
292 ("includeStats", True),
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/internal/http.py
in fix_parameters(parameters)
72 except TypeError as error:
73 value = error.args[0]
---> 74 raise TypeError("Parameters of type {} are not supported".format(type(value).name))
75 else:
76 return dehydrated
TypeError: Parameters of type map are not supported
输出与演示不一致:'(alice:Person {name:"Alice"})'
您可能安装了最新版本(4.0.0)的py2neo。有些人遇到了和你一样的问题,他的解决方法你可以参考py2neo issue 678。
另一种解决方案是您可以安装以前的版本,例如pip install py2neo==3.1.2
,它适用于我。
我刚刚测试了py2neo演示,但是失败了,代码是:
from py2neo.data import Node, Relationship
import py2neo
g = py2neo.Graph('http:/172.18.0.52:7474', user='neo4j',password='123')
a = Node("Person", name="Alice")
b = Node("Person", name="Bob")
ab = Relationship(a, "KNOWS", b)
print(a)
g.create(a)
输出是:(:Person {name: 'Alice'})
追溯是:
TypeError Traceback (most recent call last) ~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/internal/http.py in fix_parameters(parameters) 70 try: ---> 71 dehydrated, = dehydrator.dehydrate([parameters]) 72 except TypeError as error:
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/internal/json.py in dehydrate(self, values) 121 --> 122 return tuple(map(dehydrate_, values))
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/internal/json.py in dehydrate_(obj) 117 elif isinstance(obj, dict): --> 118 return {key: dehydrate_(value) for key, value in obj.items()} 119 else:
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/internal/json.py in (.0) 117 elif isinstance(obj, dict): --> 118 return {key: dehydrate_(value) for key, value in obj.items()} 119 else:
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/internal/json.py in dehydrate_(obj) 119 else: --> 120 raise TypeError(obj) 121
TypeError:
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last) in () 10 11 print(a) ---> 12 g.create(a)
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/database.py in create(self, subgraph) 343 """ 344 with self.begin() as tx: --> 345 tx.create(subgraph) 346 347 def delete(self, subgraph):
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/database.py in create(self, subgraph) 919 raise TypeError("No method defined to create object %r" % subgraph) 920 else: --> 921 create(self) 922 923 def delete(self, subgraph):
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/data.py in db_create(self, tx) 612 613 def db_create(self, tx): --> 614 create_subgraph(tx, self) 615 616 def db_delete(self, tx):
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/internal/operations.py in create_subgraph(tx, subgraph) 133 for labels, nodes in _node_create_dict(n for n in subgraph.nodes if n.graph is None).items(): 134 identities = _create_nodes(tx, labels, map(dict, nodes)) --> 135 for i, identity in enumerate(identities): 136 node = nodes[i] 137 node.graph = graph
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/internal/operations.py in _create_nodes(tx, labels, data) 84 label_string = "".join(":" + cypher_escape(label) for label in sorted(labels)) 85 cypher = "UNWIND $x AS data CREATE (_%s) SET _ = data RETURN id(_)" % label_string ---> 86 for record in tx.run(cypher, x=data): 87 yield record[0] 88
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/database.py in run(self, cypher, parameters, **kwparameters) 838 try: 839 if self.transaction: --> 840 result = self.transaction.run(cypher, parameters, **kwparameters) 841 else: 842 result = self.session.run(cypher, parameters, **kwparameters)
~/miniconda2/envs/py35/lib/python3.5/site-packages/neo4j/v1/api.py in run(self, statement, parameters, **kwparameters) 603 if self.closed(): 604 raise TransactionError("Transaction closed") --> 605 return self.session.run(statement, parameters, **kwparameters) 606 607 def sync(self):
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/internal/http.py in run(self, statement, parameters, **kwparameters) 288 self._statements.append(OrderedDict([ 289 ("statement", ustr(statement)), --> 290 ("parameters", fix_parameters(dict(parameters or {}, **kwparameters))), 291 ("resultDataContents", ["REST"]), 292 ("includeStats", True),
~/miniconda2/envs/py35/lib/python3.5/site-packages/py2neo/internal/http.py in fix_parameters(parameters) 72 except TypeError as error: 73 value = error.args[0] ---> 74 raise TypeError("Parameters of type {} are not supported".format(type(value).name)) 75 else: 76 return dehydrated
TypeError: Parameters of type map are not supported
输出与演示不一致:'(alice:Person {name:"Alice"})'
您可能安装了最新版本(4.0.0)的py2neo。有些人遇到了和你一样的问题,他的解决方法你可以参考py2neo issue 678。
另一种解决方案是您可以安装以前的版本,例如pip install py2neo==3.1.2
,它适用于我。