繁体   English   中英

如何使用java中的lightcouch api在couchdb中保存批量文档

[英]How to save bulk documents in couchdb using lightcouch api in java

我正在使用lightcouch API通过Java连接到couchdb。 我可以使用dbclient.save(object)方法保存单个文档。 但是,我的要求是一次保存批量文档。 我无法找到任何与使用Lightcouch api保存批量文档相关的方法。 请建议任何可能的解决方案

提前致谢!

我决定试一试。 我有一个数据库,其中包含描述某人的文档。

这是我的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

}

算法很简单。

  1. 创建一个Document类型的数组
  2. 将文档放入数组中
  3. 创建HTTP POST请求
  4. 将JSON转换后的数组放入请求正文中
  5. 发送

这大致是代码的样子。

注意:为简洁起见,省略了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批量文档API的文档,您的请求正文的JSON结构应该如下所示。

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

这就是有点丑陋的StringEntity定义的原因。

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

作为响应,您将获得一个包含对象的JSON数组,这些对象的字段表示插入文档的* _id *和* _rev *以及事务状态指示符。

我做了同样的事情但是使用Spring Rest Template我创建了一个类,它将按照以下方式保存要更新的文档。

   public class BulkUpdateDocument {

    private List<Object> docs;
    }       

我的Rest代码看起来像这样。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM