monil
monil

Reputation: 143

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

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!

Upvotes: 6

Views: 1962

Answers (2)

Amrut Prabhu
Amrut Prabhu

Reputation: 139

I did the same thing but with spring Rest Template I created a class which would hold the documents to be updated int he following way.

   public class BulkUpdateDocument {

    private List<Object> docs;
    }       

My Rest code looks like this.

    BulkUpdateDocument doc = new BulkUpdateDocument(ListOfObjects);

    Gson gson = new Gson();
    RestTemplate restTemplate = new RestTemplate();

    HttpHeaders header = new HttpHeaders();
    header.setContentType(MediaType.APPLICATION_JSON_UTF8);
    HttpEntity<?> requestObject = new HttpEntity<Object>(gson.toJson(doc), header);

    ResponseEntity<Object> postForEntity = restTemplate.postForEntity(path + "/_bulk_docs", requestObject, Object.class);

Upvotes: 0

Octavian Helm
Octavian Helm

Reputation: 39604

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.

Upvotes: 3

Related Questions