Ho deciso di provarlo. Ho un database contenente documenti che descrivono una persona.
Qui è la mia classe Person
che si estende 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
}
L'algoritmo è semplice.
- Creare un array di tipo
Document
- Mettere i documenti nella matrice
- Creare una richiesta HTTP POST
- Mettere il JSON convertito array in corpo della richiesta
- Inviatela
Ecco approssimativamente come potrebbe apparire il codice.
Nota: try/catch omesso per brevità! Ovviamente ci si aspetta che li usi.
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();
}
descriverò le due parti degne di nota in questo esempio.
Il primo è la raccolta di documenti. In questo caso ho usato un array invece di un List
per l'esempio.
Document[] docs = new Document[2];
docs[0] = new Person("John", "Smith", 34);
docs[1] = new Person("Jane", "Smith", 30);
È possibile utilizzare un List
come bene e poi convertirlo in un array usando metodi di utilità di Java.
Il secondo è lo StringEntity
. Secondo la documentazione di CouchDB su HTTP Bulk Document API per modificare più documenti con una singola richiesta, la struttura JSON del corpo della richiesta dovrebbe apparire come questa.
{
"docs": [
DOCUMENT,
DOCUMENT,
DOCUMENT
]
}
Questa è la ragione per la definizione un po 'brutta di StringEntity
.
StringEntity data = new StringEntity("{ \"docs\": " + gson.toJson(docs) + "}");
Come risposta si otterrà una matrice JSON contenente oggetti i cui campi rappresentare l'* _id * e * * _rev del documento inserito insieme a un indicatore di stato della transazione.
Il salvataggio di massa non è supportato nell'API LightCouch, tuttavia è previsto nella prossima versione. – ahmedyha
se si tratta di uno show stopper è possibile effettuare una chiamata utilizzare qualcosa come Apache Httpclient e Jackson per rendere la vostra chiamata di massa al divano – skipy