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

时间:2012-04-27 12:45:34

标签: java couchdb

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

提前致谢!

2 个答案:

答案 0 :(得分:3)

我决定试一试。我有一个数据库,里面有一个描述一个人的文件。

以下是我的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. 将您的文档放入数组
  4. 创建HTTP POST请求
  5. 将JSON转换后的数组放入请求正文
  6. 发送
  7. 这大致是代码的样子。

    注意:为简洁起见,省略了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) + "}");
    

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

答案 1 :(得分:0)

我做了同样的事情,但春天休息模板 我创建了一个类,它将按照以下方式保存要更新的文档。

   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);
相关问题