首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用myCouch将多个文档发布到CouchDB

使用myCouch将多个文档发布到CouchDB
EN

Stack Overflow用户
提问于 2018-04-07 12:24:25
回答 1查看 523关注 0票数 0

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

代码语言:javascript
复制
 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下,我有如下方法:

代码语言:javascript
复制
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,提前谢谢!

更新:好的,我通过稍微调整代码来解决我的问题:

代码语言:javascript
复制
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下的方法:

代码语言:javascript
复制
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);
       }
}
EN

回答 1

Stack Overflow用户

发布于 2018-04-07 20:20:49

我认为即使是你更新的代码看起来也不太对劲。我不确定,请看一下我在代码中所做的注释/修改:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49703819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档