Python 如何通过curl在couchDB中插入数百万个文档?
我必须在CouchDB localhost中插入1000万个文档。我使用python脚本创建了以下格式的随机数据:Python 如何通过curl在couchDB中插入数百万个文档?,python,json,couchdb,Python,Json,Couchdb,我必须在CouchDB localhost中插入1000万个文档。我使用python脚本创建了以下格式的随机数据: { "docs": [ {"_id": "0", "integer": 0, "string": "0"}, {"_id": "1", "integer": 1, "string": "1"}, {"_id": "2", "integer": 2, "string": "2"} ] } 文件的大小是1.5GB,因为我在每个文档中有10个键值对 我正在使用此命令加载
{
"docs": [
{"_id": "0", "integer": 0, "string": "0"},
{"_id": "1", "integer": 1, "string": "1"},
{"_id": "2", "integer": 2, "string": "2"}
]
}
文件的大小是1.5GB,因为我在每个文档中有10个键值对
我正在使用此命令加载json文件:
curl -d @db.json -H "Content-type: application/json" -X POST http://127.0.0.1:5984/new/_bulk_docs
对于100000个文档,它需要10-15秒才能加载,但对于10000000个文档,它甚至没有在12小时内加载
任何关于如何在couchDB中批量插入的帮助都将不胜感激
TIA我不熟悉CouchDB批量API,但是您提到了一个包含100000条记录的批量请求可以工作,所以我怀疑10000条记录对于一次尝试来说太多了 考虑将包含10'000'000条记录的大文件拆分为包含10'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个文件,每个文件都有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
)
谢谢你的回答,我已经把它分成了100*100000个文件。然而,它正在寻找一些更快的东西,比如mysqlbulkinsert。模仿MySQL风格的bulkinsert的唯一方法是删除所有视图(禁用索引更新),进行插入,然后重新创建视图。