Java rest 弹性搜索 json 文档

雨果

我正在尝试使用 java rest 将 json 文档发送到 elasticsearch。

我只需要知道如何初始化变量“entities[i]”并将json文档放入其中。我尝试了很多方法,但仍然没有得到一些有效的方法。

这是来自elasticsearch网站的代码:https ://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_example_requests.html

int numRequests = 10;
final CountDownLatch latch = new CountDownLatch(numRequests);

for (int i = 0; i < numRequests; i++) {
    restClient.performRequestAsync(
        "PUT",
        "/twitter/tweet/" + i,
        Collections.<String, String>emptyMap(),
        //assume that the documents are stored in an entities array
        entities[i],
        new ResponseListener() {
            @Override
            public void onSuccess(Response response) {
                System.out.println(response);
                latch.countDown();
            }

            @Override
            public void onFailure(Exception exception) {
                latch.countDown();
            }
        }
    );
}

//wait for all requests to be completed
latch.await();

谢谢

阿比拉什
    int numRequests = 1;
    final CountDownLatch latch = new CountDownLatch(numRequests);

    HttpEntity entity = new NStringEntity(
            "{\n" +
                    "    \"user\" : \"kimchy\",\n" +
                    "    \"post_date\" : \"2009-11-15T14:12:12\",\n" +
                    "    \"message\" : \"trying out Elasticsearch\"\n" +
                    "}", ContentType.APPLICATION_JSON);

    List<HttpEntity> entities = asList(entity);

    for (int i = 0; i < numRequests; i++) {
        restClient.performRequestAsync(
                "PUT",
                "/twitter/tweet/" + i,
                Collections.<String, String>emptyMap(),
                entities.get(i),
                new ResponseListener() {
                    @Override
                    public void onSuccess(Response response) {
                        System.out.println(response);
                        latch.countDown();
                    }

                    @Override
                    public void onFailure(Exception exception) {
                        latch.countDown();
                    }
                }
        );
    }

    latch.await();

该实体是一个类型 HttpEntity。您需要在列表中创建 HttpEntity 对象列表并使用它们。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章