java 使用休息客户端进行 Elasticsearch 批量插入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43339120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Elasticsearch bulk insert using rest client
提问by Muatik
To improve performance, I want to send documents to Elasticsearch in bulk instead sending one by one. I've read about elastic bulk API at https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-bulk.html
为了提高性能,我想批量发送文档到 Elasticsearch,而不是一个一个发送。我在https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-bulk.html阅读了有关弹性批量 API 的信息
However, I am using Elasticsearch rest-client (https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/index.html) and couldn't find any example or documentation about how to make a bulk insert. All I could find was about bulk requests through transport client.
但是,我正在使用 Elasticsearch rest-client ( https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/index.html) 并且找不到任何关于如何使用的示例或文档进行批量插入。我所能找到的只是关于通过传输客户端的批量请求。
I guess I have to prepare request body as described here (https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html) and pass it to restclient's performRequest method? Is there another way, for instance, a builder mechanism in the ES java rest-client library, to make bulk insert using rest?
我想我必须按照此处的描述准备请求正文(https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html)并将其传递给 restclient 的 performRequest 方法?是否有另一种方法,例如 ES java rest-client 库中的构建器机制,可以使用 rest 进行批量插入?
回答by Val
Yes, that's correct, for now the REST client only allows to send raw REST queries to ES but nothing too sophisticated. Elastic is working on a high-level client next that will work on top of the REST client and allow you to send DSL queries, etc.
是的,这是正确的,现在 REST 客户端只允许向 ES 发送原始 REST 查询,但没有什么太复杂的。Elastic 接下来正在开发一个高级客户端,它将在 REST 客户端之上工作,并允许您发送 DSL 查询等。
For now, here is a sample code that you can use to send documents in bulk to your ES server:
现在,这里有一个示例代码,您可以使用它向 ES 服务器批量发送文档:
RestClient client = ...;
String actionMetaData = String.format("{ \"index\" : { \"_index\" : \"%s\", \"_type\" : \"%s\" } }%n", index, type);
List<String> bulkData = ...; // a list of your documents in JSON strings
StringBuilder bulkRequestBody = new StringBuilder();
for (String bulkItem : bulkData) {
bulkRequestBody.append(actionMetaData);
bulkRequestBody.append(bulkItem);
bulkRequestBody.append("\n");
}
HttpEntity entity = new NStringEntity(bulkRequestBody.toString(), ContentType.APPLICATION_JSON);
try {
Response response = client.performRequest("POST", "/your_index/your_type/_bulk", Collections.emptyMap(), entity);
return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
} catch (Exception e) {
// do something
}
回答by chomp
Another example in addition to Val answer: http://cscengineer.net/2016/10/22/elastic-search-bulk-api/
除了 Val 答案的另一个例子:http: //cscengineer.net/2016/10/22/elastic-search-bulk-api/
Just use POST instead of PUT (be careful with the .exchange in the use of rest template)
只需使用 POST 而不是 PUT (在使用 rest 模板时要小心 .exchange)