[英]Bulk operations in Java Rest Client in Elasticsearch
I am working with Java Rest client for elastic search https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/index.html but could not find a way to do bulk inserts or updates.我正在使用 Java Rest 客户端进行弹性搜索https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/index.html但找不到进行批量插入或更新的方法。 How can I bulk operate with this client?
我怎样才能批量操作这个客户端?
The 5.2 Java Rest client for Elasticsearch is String based and can become messy really quick. Elasticsearch 的 5.2 Java Rest 客户端是基于字符串的,很快就会变得混乱。 This is especially true for Bulk operations, since they are constructed from chaining JSON objects.
对于批量操作尤其如此,因为它们是通过链接 JSON 对象构造的。
If you want / have to connect to your Elasticsearch cluster via REST-client, I recommend to useJEST client instead.如果您想/必须通过 REST 客户端连接到您的 Elasticsearch 集群,我建议改用JEST 客户端。
Here is an example on how to use the JEST Client for Bulk requests:以下是关于如何使用 JEST 客户端进行批量请求的示例:
// Construct a new Jest client according to configuration via factory
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig
.Builder("http://localhost:9200")
.multiThreaded(true)
.build());
JestClient client = factory.getObject();
// Construct Bulk request from articles
Bulk bulk = new Bulk.Builder()
.defaultIndex("twitter")
.defaultType("tweet")
.addAction(Arrays.asList(
new Index.Builder(article1).build(),
new Index.Builder(article2).build()))
.build();
client.execute(bulk);
If you are using Java to work with your Elasticsearch Server, i would suggest you using Java API instead.如果您使用 Java 来处理 Elasticsearch 服务器,我建议您改用 Java API。 Here is where you can take it: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html
您可以在这里使用它: https : //www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html
You can find how to do the bulk operation in Document API/Bulk API.您可以在 Document API/Bulk API 中找到如何进行批量操作。
If you still need to use Java Rest client for some reason, you will need to build a payload in Elasticsearch's Bulk request format to be able to perform the request.如果您出于某种原因仍需要使用 Java Rest 客户端,则需要以 Elasticsearch 的 Bulk 请求格式构建有效负载才能执行请求。
Please find out how to build the Bulk request format here: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html (Basically, it's constructed from a list of json object)请在此处了解如何构建批量请求格式: https : //www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html (基本上,它是从 json 对象列表构建的)
I'm using data from .json
file.我正在使用
.json
文件中的数据。
Elasticsearch version: 6.8.0弹性搜索版本: 6.8.0
pom.xml: pom.xml:
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.8.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.8.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.1</version>
<scope>test</scope>
</dependency>
</dependencies>`
Java爪哇
String bulkContent = new String(Files.readAllBytes(new File(filePath).toPath())); HttpEntity entity = new NStringEntity(bulkContent, ContentType.APPLICATION_JSON); Request request = createRequest(indexName, indexType, httpMethod, entity); RestClient restClient = RestClient.builder(new HttpHost(hostname, port, scheme)).build(); Response response = restClient.performRequest(request);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.