当前位置:   article > 正文

Neo4j报错:py2neo.errors.ProtocolError: Cannot decode response content as JSON 解决方案

py2neo.errors.protocolerror: cannot decode response content as json

环境:neo4j-5.1.0、py2neo-2021.2.3、Neo4j Desktop-1.5.2、python3.9.13

1、创建neo4j链接,对数据库进行增删改的时候报错。

报错代码:

  1. JSONDecodeError Traceback (most recent call last)
  2. D:\anaconda3\lib\site-packages\py2neo\client\http.py in from_json(cls, status, data)
  3. 442 try:
  4. --> 443 content = json_loads(data, object_hook=JSONHydrant.json_to_packstream)
  5. 444 except ValueError as error:
  6. D:\anaconda3\lib\json\__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
  7. 358 kw['parse_constant'] = parse_constant
  8. --> 359 return cls(**kw).decode(s)
  9. D:\anaconda3\lib\json\decoder.py in decode(self, s, _w)
  10. 336 """
  11. --> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  12. 338 end = _w(s, end).end()
  13. D:\anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
  14. 354 except StopIteration as err:
  15. --> 355 raise JSONDecodeError("Expecting value", s, err.value) from None
  16. 356 return obj, end
  17. JSONDecodeError: Expecting value: line 1 column 1 (char 0)
  18. The above exception was the direct cause of the following exception:
  19. ProtocolError Traceback (most recent call last)
  20. ~\AppData\Local\Temp\ipykernel_8660\3422543364.py in <module>
  21. 9 relation = Relationship(start_node,'hasConcept',end_node)
  22. 10 print(relation)
  23. ---> 11 g.merge(start_node,'article_id','article_id')
  24. 12 g.merge(end_node,'keyword','keyword')
  25. 13 g.merge(relation,'hasConcept','keyword')
  26. D:\anaconda3\lib\site-packages\py2neo\database.py in merge(self, subgraph, label, *property_keys)
  27. 676 :param property_keys: property keys on which to match any existing nodes
  28. 677 """
  29. --> 678 self.update(lambda tx: tx.merge(subgraph, label, *property_keys))
  30. 679
  31. 680 @property
  32. D:\anaconda3\lib\site-packages\py2neo\database.py in update(self, cypher, parameters, timeout)
  33. 443 if callable(cypher):
  34. 444 if parameters is None:
  35. --> 445 self._update(cypher, timeout=timeout)
  36. 446 elif (isinstance(parameters, tuple) and len(parameters) == 2 and
  37. 447 isinstance(parameters[0], Sequence) and isinstance(parameters[1], Mapping)):
  38. D:\anaconda3\lib\site-packages\py2neo\database.py in _update(self, f, timeout)
  39. 465 tx = None
  40. 466 try:
  41. --> 467 tx = self.begin(
  42. 468 # after=after, metadata=metadata, timeout=timeout
  43. 469 )
  44. D:\anaconda3\lib\site-packages\py2neo\database.py in begin(self, readonly)
  45. 349 removed. Use the 'auto' method instead.*
  46. 350 """
  47. --> 351 return Transaction(self, autocommit=False, readonly=readonly,
  48. 352 # after, metadata, timeout
  49. 353 )
  50. D:\anaconda3\lib\site-packages\py2neo\database.py in __init__(self, graph, autocommit, readonly)
  51. 913 self._ref = None
  52. 914 else:
  53. --> 915 self._ref = self._connector.begin(self.graph.name, readonly=readonly,
  54. 916 # after, metadata, timeout
  55. 917 )
  56. D:\anaconda3\lib\site-packages\py2neo\client\__init__.py in begin(self, graph_name, readonly)
  57. 1357 cx = self._acquire(graph_name)
  58. 1358 try:
  59. -> 1359 return cx.begin(graph_name, readonly=readonly,
  60. 1360 # after=after, metadata=metadata, timeout=timeout
  61. 1361 )
  62. D:\anaconda3\lib\site-packages\py2neo\client\http.py in begin(self, graph_name, readonly)
  63. 197 # raise TypeError("Transaction timeouts are not supported over HTTP")
  64. 198 r = self._post(HTTPTransactionRef.begin_uri(graph_name))
  65. --> 199 rs = HTTPResponse.from_json(r.status, r.data.decode("utf-8"))
  66. 200 location_path = urlsplit(r.headers["Location"]).path
  67. 201 tx = HTTPTransactionRef(graph_name, location_path.rpartition("/")[-1])
  68. D:\anaconda3\lib\site-packages\py2neo\client\http.py in from_json(cls, status, data)
  69. 443 content = json_loads(data, object_hook=JSONHydrant.json_to_packstream)
  70. 444 except ValueError as error:
  71. --> 445 raise_from(ProtocolError("Cannot decode response content as JSON"), error)
  72. 446 else:
  73. 447 return cls(status, content)
  74. D:\anaconda3\lib\site-packages\six.py in raise_from(value, from_value)
  75. 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的指令代码,就不再报错了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/392137
推荐阅读
相关标签
  

闽ICP备14008679号