我正在将SQL数据库迁移到couchDB。当我发布多个文档时,我遇到了问题,比如大约8K个文档I。代码如下:
MyClass cl = new MyClass();
foreach (DataRow row in dteqEvent.Rows)
{
NewSnDocument pn = new NewSnDocument();
pn.id = row[1].ToString(); //this is the document id
pn.val = row[2].ToString();
string json = JsonConvert.SerializeObject(pn);
cl.PostToCouch(json); //method under MyClass to post documents
} 然后在MyClass下,我有如下方法:
public async void PostToCouch(string json)
{
using (var client = new MyCouchClient(HostServer, Database))
{
var resp = await client.Documents.PostAsync(json);
Console.WriteLine(resp.StatusCode);
}
}前两千个ids是成功的POSTed,然后它会给我一个错误。错误提示:“无法连接到远程服务器。”InnerException声明“无法建立连接,因为目标计算机主动拒绝了它。”这与我的couchDB配置有关吗?
有没有另一种方法来POSTing多个文档。我在MyCouch中看到了一个批量操作,但我不清楚:https://github.com/danielwertheim/mycouch/wiki/documentation#bulk-operations,提前谢谢!
更新:好的,我通过稍微调整代码来解决我的问题:
MyClass cl = new MyClass();
List<NewSnDocument> pnList = new List<NewSnDocument>();
foreach (DataRow row in dteqEvent.Rows)
{
NewSnDocument pn = new NewSnDocument();
pn.id = row[1].ToString(); //this is the document id
pn.val = row[2].ToString();
pnList.Add(pn);
}
cl.PostToCouch(pnList);然后是MyClass下的方法:
public async void PostToCouch(List<NewSnDocument> obj)
{
int r = obj.Count;
using (var client = new MyCouchClient(HostServer, Database))
{
for(int i=0; i<r; i++)
{
string json = JsonConvert.SerializeObject(obj[i]);
var resp = await client.Documents.PostAsync(json);
Console.WriteLine(resp.StatusCode);
}
}发布于 2018-04-07 20:20:49
我认为即使是你更新的代码看起来也不太对劲。我不确定,请看一下我在代码中所做的注释/修改:
MyClass cl = new MyClass();
List<NewSnDocument> pnList = new List<NewSnDocument>(); //List of documents
foreach (DataRow row in dteqEvent.Rows)
{
NewSnDocument pn = new NewSnDocument();
pn.id = row[1].ToString();
pn.val = row[2].ToString();
// cl.PostToCouch(pnList);
pnList.push(pn);//You need to push each document to the list of documents
//I'm not sure about C#, but there should some "push" method
//or something equivalent to "push"
}
cl.PostToCouch(pnList);//"pnList" contains a list of documents
//So it should be posted to CouchDB outside "foreach" loop
//After all documents have been pushed into ithttps://stackoverflow.com/questions/49703819
复制相似问题