简体   繁体   English

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

[英]Posting multiple documents to CouchDB using myCouch

I'm migrating an SQL database to couchDB. 我正在将SQL数据库迁移到bedDB。 I'm having problem when I post multiple documents, say around 8K doc ids. 发布多个文档(例如约8K文档ID)时出现问题。 Code below: 代码如下:

 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
 }    

Then under MyClass I have the method below: 然后在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);
   }
}

The first 2K ids are POSTed successfully then it gives me an error after that. 成功发布了前2K个ID,之后又给了我一个错误。 Error says: "Unable to connect to the remote server." 错误消息:“无法连接到远程服务器。” InnerException states "No connection could be made because the target machine actively refused it." InnerException状态:“由于目标计算机主动拒绝连接,因此无法建立连接。” Is this something to do with my couchDB configuration. 这与我的ouchDB配置有关。

Is there an alternative way of POSTing multiple documents. 有没有其他方法可以发布多个文档。 I saw a bulk operation in MyCouch but it is not clear to me: https://github.com/danielwertheim/mycouch/wiki/documentation#bulk-operations Thanks in advance! 我在MyCouch中看到了一个批量操作,但我不清楚: https : //github.com/danielwertheim/mycouch/wiki/documentation#bulk-operations预先感谢!

UPDATE: Alright I managed to solve my problem by tweaking the code a little bit: 更新:好的,我通过稍微调整代码来解决了我的问题:

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

Then method under MyClass: 然后是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);
       }
}

I think even your updated code doesn't look right. 我认为即使您更新的代码也不正确。 I'm not sure, please take a look at the comments/modifications I made in your code: 我不确定,请查看我在您的代码中所做的评论/修改:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM