简体   繁体   English

Elasticsearch 中 Java Rest Client 中的批量操作

[英]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.

相关问题 使用休息客户端进行 Elasticsearch 批量插入 - Elasticsearch bulk insert using rest client 在使用Java高级其余客户端批量API创建动态Elasticsearch索引时需要帮助 - Need help in creating dynamic elasticsearch index using Java high level rest client bulk API Java Elasticsearch 高级 REST AWS 托管客户端库 Z45E23A169654FAF95CE80DA8 - Java Elasticsearch Highlevel REST Client lib on AWS Managed Elasticsearch 来自 Spring-Boot 的高级 Rest 客户端 7.1.1:java.lang.NoSuchMethodError: org.elasticsearch.action.bulk.BulkRequest.pipeline()Ljava/lang/String; - High Level Rest client 7.1.1 from Spring-Boot : java.lang.NoSuchMethodError: org.elasticsearch.action.bulk.BulkRequest.pipeline()Ljava/lang/String; Java + Membase + spymemcached 批量操作 - Java + Membase + spymemcached bulk operations 如何使用批量处理器捕获批量响应?(Java 高级其余客户端) - How to catch bulk response with bulk processor?(Java High level rest client) 在 Elasticsearch 中以 Curl 格式启用 Java REST 客户端日志记录 - Enabling Java REST Client logging in Elasticsearch in Curl format 使用Java REST Client API在Elasticsearch 6.5.4中完成建议 - Completion Suggester in Elasticsearch 6.5.4 with Java REST Client API Elasticsearch高级Rest Client Java排序不正常 - Elasticsearch High Level Rest Client Java sorting not working properly Elasticsearch Java Rest Client:如何获取所有索引的列表 - Elasticsearch Java Rest Client: how to get the list of all indices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM