环境:neo4j-5.1.0、py2neo-2021.2.3、Neo4j Desktop-1.5.2、python3.9.13
1、创建neo4j链接,对数据库进行增删改的时候报错。
报错代码:
JSONDecodeError Traceback (most recent call last)
D:\anaconda3\lib\site-packages\py2neo\client\http.py in from_json(cls, status, data)
442 try:
--> 443 content = json_loads(data, object_hook=JSONHydrant.json_to_packstream)
444 except ValueError as error:
D:\anaconda3\lib\json\__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
358 kw['parse_constant'] = parse_constant
--> 359 return cls(**kw).decode(s)
D:\anaconda3\lib\json\decoder.py in decode(self, s, _w)
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
D:\anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
354 except StopIteration as err:
--> 355 raise JSONDecodeError("Expecting value", s, err.value) from None
356 return obj, end
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
The above exception was the direct cause of the following exception:
ProtocolError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_8660\3422543364.py in <module>
9 relation = Relationship(start_node,'hasConcept',end_node)
10 print(relation)
---> 11 g.merge(start_node,'article_id','article_id')
12 g.merge(end_node,'keyword','keyword')
13 g.merge(relation,'hasConcept','keyword')
D:\anaconda3\lib\site-packages\py2neo\database.py in merge(self, subgraph, label, *property_keys)
676 :param property_keys: property keys on which to match any existing nodes
677 """
--> 678 self.update(lambda tx: tx.merge(subgraph, label, *property_keys))
679
680 @property
D:\anaconda3\lib\site-packages\py2neo\database.py in update(self, cypher, parameters, timeout)
443 if callable(cypher):
444 if parameters is None:
--> 445 self._update(cypher, timeout=timeout)
446 elif (isinstance(parameters, tuple) and len(parameters) == 2 and
447 isinstance(parameters[0], Sequence) and isinstance(parameters[1], Mapping)):
D:\anaconda3\lib\site-packages\py2neo\database.py in _update(self, f, timeout)
465 tx = None
466 try:
--> 467 tx = self.begin(
468 # after=after, metadata=metadata, timeout=timeout
469 )
D:\anaconda3\lib\site-packages\py2neo\database.py in begin(self, readonly)
349 removed. Use the 'auto' method instead.*
350 """
--> 351 return Transaction(self, autocommit=False, readonly=readonly,
352 # after, metadata, timeout
353 )
D:\anaconda3\lib\site-packages\py2neo\database.py in __init__(self, graph, autocommit, readonly)
913 self._ref = None
914 else:
--> 915 self._ref = self._connector.begin(self.graph.name, readonly=readonly,
916 # after, metadata, timeout
917 )
D:\anaconda3\lib\site-packages\py2neo\client\__init__.py in begin(self, graph_name, readonly)
1357 cx = self._acquire(graph_name)
1358 try:
-> 1359 return cx.begin(graph_name, readonly=readonly,
1360 # after=after, metadata=metadata, timeout=timeout
1361 )
D:\anaconda3\lib\site-packages\py2neo\client\http.py in begin(self, graph_name, readonly)
197 # raise TypeError("Transaction timeouts are not supported over HTTP")
198 r = self._post(HTTPTransactionRef.begin_uri(graph_name))
--> 199 rs = HTTPResponse.from_json(r.status, r.data.decode("utf-8"))
200 location_path = urlsplit(r.headers["Location"]).path
201 tx = HTTPTransactionRef(graph_name, location_path.rpartition("/")[-1])
D:\anaconda3\lib\site-packages\py2neo\client\http.py in from_json(cls, status, data)
443 content = json_loads(data, object_hook=JSONHydrant.json_to_packstream)
444 except ValueError as error:
--> 445 raise_from(ProtocolError("Cannot decode response content as JSON"), error)
446 else:
447 return cls(status, content)
D:\anaconda3\lib\site-packages\six.py in raise_from(value, from_value)
ProtocolError: Cannot decode response content as JSON
奇怪的是,同样的代码、数据,用在python3.8.3的环境中没有报错,所以首先查看两个版本python的区别,发现json的__init__中有一些小区别,关于json解析的,手动给3.9.13的init进行修改,仍然报错,思考是否是别的问题。
2、寻找其他解决方案
参考了一个解决方案:https://blog.csdn.net/weixin_44155966/article/details/127704336
但是实际上没有解决问题,依然报错。
3、受到方案启发,认为是graph_name为空时的问题
看了上方的解决方案之后,感觉这个报错逻辑有些奇怪,实际上不是json的问题,还是连接neo4j时,指定的graph不够明确,所以在连接时指定name如下:
此时,再次执行控制neo4j的指令代码,就不再报错了。