我正在使用基于Nodejs的脚本将大型csv文件导入ibm cloudant数据库。我对该脚本的处理方式如下:
到目前为止有效的方法:
面临的问题:
确切错误:
注意:
有人可以提出建议的最佳方法来解决此要求,而不会遇到如上所述的问题吗?也欢迎任何其他可解决此要求的npm软件包。也欢迎任何关于改变我的开发方法的建议。
假设您的CSV行按ID排序,则可以先进行批量获取:
请求:
POST /db/_all_docs HTTP/1.1
Accept: application/json
Content-Length: 70
Content-Type: application/json
Host: localhost:5984
{
"keys" : [
"a_id123",
"b_id123",
"c_id123",
"d_id123",
]
}
然后您将获得以下内容:响应
{
"total_rows" : 2666,
"rows" : [
{
"value" : {
"rev" : "1-a3544d296de19e6f5b932ea77d886942"
},
"id" : "a_id123",
"key" : "a_id123"
},
{
"value" : {
"rev" : "1-91635098bfe7d40197a1b98d7ee085fc"
},
"id" : "b_id123",
"key" : "b_id123"
}
],
"offset" : 0
}
现在您可以遍历响应,查看哪些文档存在而哪些不存在。
此后,您可以批量添加不存在的文档:
请求:
POST /db/_bulk_docs HTTP/1.1
Accept: application/json
Content-Length: 109
Content-Type:application/json
Host: localhost:5984
{
"docs": [
{
"_id": "c_id123",
"foo": "bar",
},
{
"_id": "d_id123",
"foo": "bar c",
},
{
"_id": "a_id123",
"_rev": "1-a3544d296de19e6f5b932ea77d886942",
"foo": "updated",
}
]
}
批量获取文件:https://docs.couchdb.org/en/stable/api/database/bulk-api.html#post--db-_all_docs
用于批量创建/更新的文档:https://docs.couchdb.org/en/stable/api/database/bulk-api.html#db-bulk-docs