簡體   English   中英

Elasticsearch 中 Java Rest Client 中的批量操作

[英]Bulk operations in Java Rest Client in Elasticsearch

我正在使用 Java Rest 客戶端進行彈性搜索https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/index.html但找不到進行批量插入或更新的方法。 我怎樣才能批量操作這個客戶端?

Elasticsearch 的 5.2 Java Rest 客戶端是基於字符串的,很快就會變得混亂。 對於批量操作尤其如此,因為它們是通過鏈接 JSON 對象構造的。

如果您想/必須通過 REST 客戶端連接到您的 Elasticsearch 集群,我建議改用JEST 客戶端

以下是關於如何使用 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);

如果您使用 Java 來處理 Elasticsearch 服務器,我建議您改用 Java API。 您可以在這里使用它: https : //www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html

您可以在 Document API/Bulk API 中找到如何進行批量操作。

如果您出於某種原因仍需要使用 Java Rest 客戶端,則需要以 Elasticsearch 的 Bulk 請求格式構建有效負載才能執行請求。

請在此處了解如何構建批量請求格式: https : //www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html (基本上,它是從 json 對象列表構建的)

我正在使用.json文件中的數據。

彈性搜索版本: 6.8.0

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>`

爪哇

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM