【问题标题】:How to insert millions of documents in couchDB through curl?如何通过 curl 在 couchDB 中插入数百万个文档?
【发布时间】:2018-06-03 17:06:05
【问题描述】:

我必须在 CouchDB localhost 中插入 1000 万个文档。我使用 python 脚本以这种格式创建随机数据:

{
"docs": [
  {"_id": "0", "integer": 0, "string": "0"},
  {"_id": "1", "integer": 1, "string": "1"},
  {"_id": "2", "integer": 2, "string": "2"}
  ]
}

文件大小为 1.5 GB,因为我在每个文档中有 10 个键值对。

我正在使用这个命令来加载json文件:

curl -d @db.json -H "Content-type: application/json" -X POST http://127.0.0.1:5984/new/_bulk_docs

对于 100,000 个文档,它需要 10-15 秒才能加载,但对于 10,000,000 个文档,它甚至在 12 小时内都没有加载。

任何关于如何在 couchDB 中批量插入的帮助将不胜感激。

TIA

【问题讨论】:

    标签: python json couchdb


    【解决方案1】:

    最后,我把我的文件分成100个文件,每个文件有0.1M条记录,并通过这个命令上传到数据库。

    FOR /L %i IN (0,1,9) DO (
        curl -d @dbn%i.json -H "Content-type: application/json" -X POST http://127.0.0.1:5984/new4/_bulk_docs
    )
    

    【讨论】:

      【解决方案2】:

      我不熟悉 CouchDB 批量 API,但您提到具有 100'000 条记录的批量请求有效,所以我怀疑 10'000'000 一次太多了。

      考虑将包含 10'000'000 条记录的大文件拆分为包含 100'000 条记录的较小 JSON 文件,并使用单独的请求发布每个块/批次:

      import json
      
      # Batch function from: https://stackoverflow.com/a/8290508/7663649
      def batch(iterable, n=1):
          l = len(iterable)
          for ndx in range(0, l, n):
              yield iterable[ndx:min(ndx + n, l)]
      
      BATCH_SIZE = 100000
      with open("db.json") as input_file:
          for batch_index, batch_list in enumerate(
                  batch(json.load(input_file), BATCH_SIZE)):
              with open("chunk_{}.json".format(batch_index), "w") as chunk_file:
                  json.dump(batch_list, chunk_file)
      

      【讨论】:

      • 感谢您的回答,我已经通过拆分 100*100000 个文件来做到这一点。然而,它正在寻找更快的东西,比如 MySQL 中的批量插入
      • 模仿 MySQL 风格的批量插入的唯一方法是删除我们所有的视图(禁用索引更新),进行插入,然后重新创建视图。
      猜你喜欢
      • 1970-01-01
      • 2019-05-18
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多