【问题标题】:Connection reset by peer error in MongoDb on bulk insert批量插入时 MongoDb 中的对等错误重置连接
【发布时间】:2014-05-24 06:00:37
【问题描述】:

我试图通过在 pymongo 中进行批量插入来插入 500 个文档,但出现此错误

File "/usr/lib64/python2.6/site-packages/pymongo/collection.py", line 306, in insert
    continue_on_error, self.__uuid_subtype), safe)
  File "/usr/lib64/python2.6/site-packages/pymongo/connection.py", line 748, in _send_message
    raise AutoReconnect(str(e))
pymongo.errors.AutoReconnect: [Errno 104] Connection reset by peer

我环顾四周,发现 here 发生这种情况是因为插入的文档的大小超过 16 MB,因此根据 500 个文档的大小应该超过 16 MB。所以我像这样检查了 500 个文档(python 字典)的大小

size=0
for dict in dicts:
    size+=dict.__sizeof__()
print size

这给了我502920。这就像 500 KB。不到 16 MB。那我为什么会收到这个错误。 我知道我正在计算 python 字典而不是 BSON 文档的大小,而 MongoDB 接受 BSON 文档,但不能将 500KB 变成 16+ MB。此外,我不知道如何将 python dict 转换为 BSON 文档。

我的MongoDB版本是2.0.6,pymongo版本是2.2.1

编辑 我可以批量插入 150 个文档,这很好,但超过 150 个文档会出现此错误

【问题讨论】:

  • This bug 已修复。如果您仍然遇到问题,请更新 pymongo

标签: bulkinsert pymongo


【解决方案1】:

Bulk Inserts bug 已解决,但您可能需要更新您的 pymongo 版本:

pip install --upgrade pymongo

【讨论】:

    【解决方案2】:

    由于批量插入的文档有 总大小大于 16 MB

    My method of calculating the size of dictionaries was wrong.
    

    当我手动检查字典的每个键并发现 1 个键的大小为 300 KB 时。所以这确实使批量插入的文档的整体大小超过了 16 MB。 (500*(300+)KB) > 16 MB。但是我仍然不知道如何在不手动检查字典的情况下计算字典的大小。有人可以建议吗?

    【讨论】:

      【解决方案3】:

      刚刚遇到了同样的错误,并通过创建我自己的小批量来解决它:

      region_list = []
      region_counter = 0
      write_buffer = 1000
      # loop through regions
      for region in source_db.region.find({}, region_column):
          region_counter += 1 # up _counter
          region_list.append(region)
          # save bulk if we're at the write buffer
          if region_counter == write_buffer:
              result = user_db.region.insert(region_list)
              region_list = []
              region_counter = 0
      # if there is a rest, also save that
      if region_counter > 0:
          result = user_db.region.insert(region_list)
      

      希望对你有帮助

      注意:小更新,从 pymongo 2.6 开始,PyMongo 将根据最大传输大小自动拆分列表:“insert() 方法根据 max_message_size 自动将大批量文档拆分为多个插入消息”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-31
        • 2013-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多