我使用lightcouch API连接通过Java来保存的CouchDB CouchDB中批量文件。我可以使用dbclient.save(object)方法保存单个文档。但是,我的要求是一次保存批量文档。我无法找到任何与使用Lightcouch api保存批量文档相关的方法。请提出任何可能的解决方案如何使用lightcouch API在Java中
在此先感谢!
我使用lightcouch API连接通过Java来保存的CouchDB CouchDB中批量文件。我可以使用dbclient.save(object)方法保存单个文档。但是,我的要求是一次保存批量文档。我无法找到任何与使用Lightcouch api保存批量文档相关的方法。请提出任何可能的解决方案如何使用lightcouch API在Java中
在此先感谢!
我决定搏一搏。我有一个数据库来存放描述一个人的文档。
这里是我Person
类延伸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
}
该算法简单。
Document
下面大概是代码的样子。
注:try/catch语句不再赘述!当然,你有望使用它们。
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();
}
我将描述在这个例子中,两个值得注意的部分。
第一个是文件的集合。在这种情况下,我使用了一个数组而不是List
作为示例。
Document[] docs = new Document[2];
docs[0] = new Person("John", "Smith", 34);
docs[1] = new Person("Jane", "Smith", 30);
你可以使用一个List
以及后来使用Java的实用方法将其转换为一个数组。
第二个是StringEntity
。根据CouchDB有关HTTP Bulk Document API上关于使用单个请求修改多个文档的文档,请求主体的JSON结构应该如下所示。
{
"docs": [
DOCUMENT,
DOCUMENT,
DOCUMENT
]
}
这就是有点难看StringEntity
定义的原因。
StringEntity data = new StringEntity("{ \"docs\": " + gson.toJson(docs) + "}");
正如你将获得一个包含对象,其字段代表* _id *和插入的文件进行的交易状态指示灯沿* _rev * JSON数组的响应。
+1撰写论文。 – 2012-08-05 08:33:07
我也做了同样的事情,但有弹簧安置模板 我创建了将持有的文件下面的方式,他被更新INT一类。
public class BulkUpdateDocument {
private List<Object> docs;
}
我的休息代码看起来像这样。
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);
保存批量在LightCouch API中不受支持,但是它计划在下一版本中使用。 – ahmedyha 2012-05-05 18:12:39
如果这是一个显示塞,你可以拨打电话使用类似Apache Httpclient和Jackson的电话,使你的批量电话拨打沙发 – skipy 2012-06-24 08:13:53