Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save bulk documents in couchdb using lightcouch api in java

Tags:

java

couchdb

I am using the lightcouch API to connect to couchdb through Java. I am able to save a single document using dbclient.save(object) method. However, my requirement is to save bulk documents at a time. I am not able to find any methods related to saving bulk documents using the Lightcouch api. Please suggest any possible solution.

Thanks in advance!

like image 770
monil Avatar asked Apr 27 '12 12:04

monil


1 Answers

I decided to give it a go. I have a database holding documents that describe a person.

Here is my Person class which extends Document LightCouch:

public class Person extends Document {

    private String firstname = "";
    private String lastname = "";
    private int age = -1;

    public Person(String firstname, String lastname, int age) {
        super();
        this.setFirstname(firstname);
        this.setLastname(lastname);
        this.setAge(age);
    }

    // setters and getters omitted for brevity

}

The algorithm is simple.

  1. Create an array of type Document
  2. Put your documents into the array
  3. Create a HTTP POST request
  4. Put the JSON converted array into the request body
  5. Send it

Here is roughly what the code could look like.

Note: try/catch omitted for brevity! Of course you are expected to use them.

public static void main(String[] args) {

    // You could also use a List and then convert it to an array
    Document[] docs = new Document[2];
    docs[0] = new Person("John", "Smith", 34);
    docs[1] = new Person("Jane", "Smith", 30);

    DefaultHttpClient httpClient = new DefaultHttpClient();

    // Note the _bulk_docs
    HttpPost post = new HttpPost("http://127.0.0.1:5984/persons/_bulk_docs");

    Gson gson = new Gson();
    StringEntity data = 
        new StringEntity("{ \"docs\": " + gson.toJson(docs) + "}");
    data.setContentType("application/json");
    post.setEntity(data);

    HttpResponse response = httpClient.execute(post);

    if (response.getStatusLine().getStatusCode() != 201) {
        throw new RuntimeException("Failed. HTTP error code: "
            + response.getStatusLine().getStatusCode());
    }
    BufferedReader br = new BufferedReader(
        new InputStreamReader((response.getEntity().getContent())));
    String output;
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }
    httpClient.getConnectionManager().shutdown();
}

I'll describe the two noteworthy parts in this example.

First one is the collection of documents. In this case I used an array instead of a List for the example.

Document[] docs = new Document[2];
docs[0] = new Person("John", "Smith", 34);
docs[1] = new Person("Jane", "Smith", 30);

You could use a List as well and later convert it to an array using Java's utility methods.

Second one is the StringEntity. As per CouchDB's documentation on the HTTP Bulk Document API on modify multiple documents with a single request the JSON structure of your request body should look like this.

{
  "docs": [
    DOCUMENT,
    DOCUMENT,
    DOCUMENT
  ]
}

This is the reason for the somewhat ugly StringEntity definition.

StringEntity data = new StringEntity("{ \"docs\": " + gson.toJson(docs) + "}");

As a response you'll get a JSON array containing objects whose fields represent the *_id* and *_rev* of the inserted document along with a transaction status indicator.

like image 146
Octavian A. Damiean Avatar answered Oct 14 '22 15:10

Octavian A. Damiean