Migrating from the Java High Lev

2021-12-07  本文已影响0人  i_cyy

Preface

Deprecated in 7.0.0.

The TransportClient is deprecated in favour of the Java High Level REST Client and will be removed in Elasticsearch 8.0. The migration guide describes all the steps needed to migrate.

This section describes the Java Transport Client that Elasticsearch provides. All Elasticsearch operations are executed using a Client object. All operations are completely asynchronous in nature (either accepts a listener, or returns a future).

Additionally, operations on a client may be accumulated and executed in Bulk.

Note, all the APIs are exposed through the Java API (actually, the Java API is used internally to execute them).

Migrating from the High Level Rest Client

The Elasticsearch Java API Client is an entirely new client library that has no relation to the older High Level Rest Client (HLRC). This was a deliberate choice to provide a library that is independent from the Elasticsearch server code and that provides a very consistent and easier to use API for all Elasticsearch features.

Migrating from the HLRC therefore requires some code rewrite in your application. This transition can however happen progressively as the two client libraries can coexist in a single application with no operational overhead.

Transition strategies

There are different ways you can start transitioning away from the HLRC in your application code.

For example:

Using the same transport with the HLRC and the Java API Client

To avoid any operational overhead during the transition phase where an application would use both the HLRC and the new Java API Client, both clients can share the same Low Level Rest Client, which is the network layer that manages all connections, round-robin strategies, node sniffing, and so on.

The code below shows how to initialize both clients with the same HTTP client:

// Create the low-level client
RestClientBuilder httpClientBuilder = RestClient.builder(
   new HttpHost("localhost", 9200)
);

// Create the HLRC
RestHighLevelClient hlrc = new RestHighLevelClient(httpClientBuilder);

// Create the new Java Client with the same low level client
Transport transport = new RestClientTransport(
   hlrc.getLowLevelClient(),
   new JacksonJsonpMapper()
);

ElasticsearchClient esClient = new ElasticsearchClient(transport);

// hlrc and esClient share the same httpClient

Javadoc

The javadoc for the transport client can be found at https://artifacts.elastic.co/javadoc/org/elasticsearch/client/transport/7.15.2/index.html.

Maven Repository

Elasticsearch is hosted on Maven Central.

For example, you can define the latest version in your pom.xml file:

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>7.15.2</version>
</dependency>

Jave Api Detail Can Refer : Java-Api

上一篇下一篇

猜你喜欢

热点阅读