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

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

Thanks in advance!

+6
source share
2 answers

I decided to leave. I have a database with documents that describe a person.

Here is my Person class that 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.

  • Create an array of type Document
  • Put your documents in an array
  • Create an HTTP POST request
  • Put the converted JSON array in the request body
  • Submit

Here is some code that might look like.

Note: try / catch is omitted for brevity! Of course you should 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 will describe two noteworthy parts in this example.

The first is a collection of documents. In this case, I used an array instead of List for an example.

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

You can also use List and then convert it to an array using Java utility methods.

The second is StringEntity . According to the CouchDB documentation in the HTTP Bulk Document API, for modifying 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 some ugly definition of StringEntity .

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

In response, you will receive a JSON array containing objects whose fields represent * * id and * _rev * of the inserted document along with a transaction status indicator.

+3
source

I did the same, but with spring Rest Template I created a class in which documents will be updated in the following order.

  public class BulkUpdateDocument { private List<Object> docs; } 

My breakpoint is as follows.

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

Source: https://habr.com/ru/post/914334/


All Articles