【问题标题】:TypeError: not JSON serializable Py2neo Batch submitTypeError: not JSON serializable Py2neo Batch submit
【发布时间】:2014-07-10 13:50:39
【问题描述】:

我正在创建一个包含超过 140 万个节点和 1.6 亿个关系的庞大图形数据库。我的代码如下所示:

from py2neo import neo4j
# first we create all the nodes
batch = neo4j.WriteBatch(graph_db)
nodedata = []

for index, i in enumerate(words): # words is predefined
    batch.create({"term":i})
    if index%5000 == 0: #so as not to exceed the batch restrictions
        results = batch.submit()
        for x in results:
            nodedata.append(x)
        batch = neo4j.WriteBatch(graph_db)

results = batch.submit()
for x in results:
    nodedata.append(x)

#nodedata contains all the node instances now
#time to create relationships

batch = neo4j.WriteBatch(graph_db)
for iindex, i in enumerate(weightdata): #weightdata is predefined 
    batch.create((nodedata[iindex], "rel", nodedata[-iindex], {"weight": i})) #there is a different way how I decide the indexes of nodedata, but just as an example I put iindex and -iindex
    if iindex%5000 == 0: #again batch constraints
        batch.submit() #this is the line that shows error
        batch = neo4j.WriteBatch(graph_db)
batch.submit()

我收到以下错误:

Traceback (most recent call last):
  File "test.py", line 53, in <module>
    batch.submit()
  File "/usr/lib/python2.6/site-packages/py2neo/neo4j.py", line 2116, in submit
    for response in self._submit()
  File "/usr/lib/python2.6/site-packages/py2neo/neo4j.py", line 2085, in _submit
    for id_, request in enumerate(self.requests)
  File "/usr/lib/python2.6/site-packages/py2neo/rest.py", line 427, in _send
    return self._client().send(request)
  File "/usr/lib/python2.6/site-packages/py2neo/rest.py", line 351, in send
    rs = self._send_request(request.method, request.uri, request.body, request.$
  File "/usr/lib/python2.6/site-packages/py2neo/rest.py", line 326, in _send_re$
    data = json.dumps(data, separators=(",", ":"))
  File "/usr/lib64/python2.6/json/__init__.py", line 237, in dumps
    **kw).encode(obj)
  File "/usr/lib64/python2.6/json/encoder.py", line 367, in encode
    chunks = list(self.iterencode(o))
  File "/usr/lib64/python2.6/json/encoder.py", line 306, in _iterencode
    for chunk in self._iterencode_list(o, markers):
  File "/usr/lib64/python2.6/json/encoder.py", line 204, in _iterencode_list
    for chunk in self._iterencode(value, markers):
  File "/usr/lib64/python2.6/json/encoder.py", line 309, in _iterencode
    for chunk in self._iterencode_dict(o, markers):
  File "/usr/lib64/python2.6/json/encoder.py", line 275, in _iterencode_dict
    for chunk in self._iterencode(value, markers):
  File "/usr/lib64/python2.6/json/encoder.py", line 317, in _iterencode
    for chunk in self._iterencode_default(o, markers):
  File "/usr/lib64/python2.6/json/encoder.py", line 323, in _iterencode_default
    newobj = self.default(o)
  File "/usr/lib64/python2.6/json/encoder.py", line 344, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: 3448 is not JSON serializable

谁能告诉我这里到底发生了什么,我该如何克服它?任何形式的帮助将不胜感激。提前致谢! :)

【问题讨论】:

    标签: python json neo4j batch-processing py2neo


    【解决方案1】:

    如果不能使用相同的数据集运行您的代码,这很难说,但这很可能是由 weightdata 中的项目类型引起的。

    逐步检查您的代码或打印数据类型以确定i 的类型在关系描述符的{"weight": i} 部分中是什么类型。您可能会发现这不是 int - JSON 数字序列化需要它。如果这个理论是正确的,那么在将它用于属性集中之前,您需要找到一种方法将该属性值强制转换或以其他方式转换为 int。

    【讨论】:

      【解决方案2】:

      我从未使用过 p2neo,但如果我查看文档

      这个:

      batch.create((nodedata[iindex], "rel", nodedata[-iindex], {"weight": i}))
      

      缺少 rel() 部分:

      batch.create(rel(nodedata[iindex], "rel", nodedata[-iindex], {"weight": i}))
      

      【讨论】:

      猜你喜欢
      • 2021-09-19
      • 1970-01-01
      • 2013-11-25
      • 2019-05-06
      • 2019-10-09
      • 2021-03-02
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多