0

我正在将 SQL 数据库迁移到 couchDB。当我发布多个文档时遇到问题,比如大约 8K 文档 ID。下面的代码:

 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);
   }
}

第一个 2K id 已成功发布,然后它给了我一个错误。错误提示:“无法连接到远程服务器。” InnerException 状态“无法建立连接,因为目标机器主动拒绝了它。” 这与我的 couchDB 配置有关吗?

是否有另一种发布多个文档的方法。我在 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);
       }
}
4

1 回答 1

0

我认为即使您更新的代码看起来也不正确。我不确定,请查看我在您的代码中所做的评论/修改:

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 it
于 2018-04-07T12:20:49.060 回答