简体   繁体   中英

Posting multiple documents to CouchDB using myCouch

I'm migrating an SQL database to couchDB. I'm having problem when I post multiple documents, say around 8K doc ids. 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:

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. Error says: "Unable to connect to the remote server." InnerException states "No connection could be made because the target machine actively refused it." Is this something to do with my couchDB configuration.

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!

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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