java - ElasticSearch 和 Apache HttpAsyncClient

标签 java http elasticsearch apache-httpclient-4.x apache-httpasyncclient

我正在尝试通过 Java 使用 ElasticSearch REST API Apache HttpAsyncClient图书馆。我想使用持久流水线连接。这是一些测试代码(输出在注释中):

@Test
public void testEsPipeliningClient() throws IOException, ExecutionException, InterruptedException
{
    testPost(HttpAsyncClients.createDefault());
    //201: {"_index":"test_index","_type":"test_type","_id":"AVIHYGnqdqqg_TAHm4ix","_version":1,"_shards":{"total":2,"successful":1,"failed":0},"created":true}
    testPost(HttpAsyncClients.createPipelining());
    //400: No handler found for uri [http://127.0.0.1:9200/test_index/test_type] and method [POST]
}

private void testPost(CloseableHttpAsyncClient client) throws ExecutionException, InterruptedException, IOException
{
    client.start();
    HttpPost request = new HttpPost("http://127.0.0.1:9200/test_index/test_type");
    request.setEntity(new StringEntity("{\"some_field\": \"some_value\"}"));
    Future<HttpResponse> responseFuture = client.execute(request, null);
    HttpResponse response = responseFuture.get();
    System.err.println(response.getStatusLine().getStatusCode() + ": " + EntityUtils.toString(response.getEntity()));
}

我不明白,为什么它与 HttpAsyncClients.createDefault() 配合得很好?客户端,但不适用于 HttpAsyncClients.createPipelining() .我也无法理解这两种创建方法之间的区别。

为什么我在使用 createPipelining() 时会收到错误响应?

我试着看看与 https://httpbin.org/post 的区别但它向我展示了两种选择的相同结果。我使用默认的 ElasticSearch 设置。

谢谢!


UPD1

我试过 PUT文档 ( PUT http://127.0.0.1/test_index/test_type/<doc id> ) 请求具有相同的结果 - 它适用于 createDefault()但是我在使用 createPipelining() 时遇到了类似的错误- 未找到处理程序 <...>。

但是当我尝试执行创建索引的请求(PUT http://127.0.0.1/<index name>)时,出现了另一个错误。请看下面的代码:

@Test
public void testEsPipeliningClient() throws IOException, ExecutionException, InterruptedException
{
    testCreateIndex(HttpAsyncClients.createDefault());
    //200: {"acknowledged":true}
    testCreateIndex(HttpAsyncClients.createPipelining());
    //400: {"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse, document is empty"}],"type":"mapper_parsing_exception","reason":"failed to parse, document is empty"},"status":400}
}

private void testCreateIndex(CloseableHttpAsyncClient client) throws ExecutionException, InterruptedException, IOException
{
    client.start();
    HttpPut request = new HttpPut("http://127.0.0.1:9200/" + RandomStringUtils.randomAlphabetic(8).toLowerCase());
    Future<HttpResponse> responseFuture = client.execute(request, null);
    HttpResponse response = responseFuture.get();
    System.err.println(response.getStatusLine().getStatusCode() + ": " + EntityUtils.toString(response.getEntity()));
}

正如我在 this documentation page 看到的那样ElasticSearch 默认支持 HTTP 管道。也许我需要在 ES 设置中更改什么?


UPD2

以下是 UPD1 部分中具有不同日志设置的代码的一些线路日志:

Dorg.apache.commons.logging.simplelog.log.org.apache.http=DEBUG -Dorg.apache.commons.logging.simplelog.log.org.apache.http.wire=INFO

http://pastebin.com/v29uvgbj

-Dorg.apache.commons.logging.simplelog.log.org.apache.http.impl.conn=DEBUG -Dorg.apache.commons.logging.simplelog.log.org.apache.http.impl.client=DEBUG -Dorg.apache.commons.logging.simplelog.log.org.apache.http.client=DEBUG -Dorg.apache.commons.logging.simplelog.log.org.apache.http.wire=DEBUG

http://pastebin.com/G9ij15d6


UPD3

我只是尝试用 createMinimal() 替换 createDefault(),它导致了与 createPipelining() 相同的错误。 MinimalHttpAsyncClient 中的任何想法可能会导致此问题?也许有一种方法可以让我手动创建流水线客户端(使用构建器类)而不会出现这个问题?

最佳答案

服务器必须阻塞在请求行中的绝对请求 URI

[DEBUG] wire - http-outgoing-1 >> "PUT http://127.0.0.1:9200/ydiwdsid HTTP/1.1[\r][\n]"

流水线模式下的 HttpAsyncClient 使用最小的协议(protocol)处理链。它不会尝试重写请求对象的请求 URI。

对于您的特定情况,请求流水线似乎没有多大意义。更不用说除非您批量提交请求,否则您甚至不会使用流水线执行。

关于java - ElasticSearch 和 Apache HttpAsyncClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34576416/

相关文章:

java - 如何使用 PDFReactor 生成不透明的 PDF?

java - 使用 WebSphere MQ v7.0 找不到 SYSTEM.BROKER 服务

http - 4D Web 服务器和 session 变量

elasticsearch - 如何将嵌套类型与 NEST 客户端一起用于 Elastic Search

在我自己的java文件中使用weka.jar文件时出现java.lang.NoClassDefFoundError

扫描目录时出现 java.lang.NullPointerException

http - 请求 http.GET 时发送的 Angular2 OPTIONS 方法

javascript - Express.js : Convert req. body 变成 POST 编码的字符串

python - Python Agent如何使用ElasticAPM跟踪各种计数器/值随时间的演变?

python - 如何使用 elasticsearch python api 获取所有快照的概览?