Elasticsearch httpclient connection

Programmer light 2022-02-13 07:51:50 阅读数:675

elasticsearch httpclient connection
  • One can walk very fast , A group of people can walk far
  • give the thumbs-up Comment on Collection   Focus on == Develop habits ( Four in one
  • Welcome to study and discuss together ️ Progress together
  • The author's level is limited , You are welcome to point out , Learn from each other and make progress !

Catalog

One 、 About ES Java Some of API explain

1.1、 summary

Two 、 summary

3、 ... and 、 adopt HttpClient operation ES

3.1、 Introduce dependent packages

 3.2、 Build the client

3.3、 Execute the query

3.4、 Output results


One 、 About ES Java Some of API explain

operation ES Of Java API There are several kinds of :

TransportClient、NodeClient、XPackTransportClient、REST API( low-level 、 senior REST)、Java API Client

  1. TransportClient: As an external visitor , request ES The cluster of , For clusters , It is an external factor .
  2. TransportClient It is recommended to use Java senior REST client . however 7.16 after ,Java senior REST It is not recommended to use , It is recommended to use Java API Client.
  3. NodeClient: As ES A node of the cluster , It is ES A link in , Other nodes are aware of it .
  4. XPackTransportClient: Service installed x-pack plug-in unit
  5. Java API Client:7.15 Version is only available , It's still improving .

1.1、 summary

Even though ES Some of API Always changing , But generally, the higher version can support the lower version , This article is mainly about REST Explain , Official website for TransportClient、REST The interface has relatively perfect documents for learning . And the operation ES Mainly CRUD, So there is no need to pursue API The change of , Are essentially used HttpClient operation ES Of HTTP Interface .

Two 、 summary

Java operation ES Of REST There are two styles of clients :Java REST Client [7.15] | Elastic

  • Java Low Level REST Client:Elasticsearch The official low-level client . It allows access http And Elasticsearch Cluster communication . Ungroup requests and responses to users . It's related to all Elasticsearch Version compatibility .
  • Java senior REST client :Elasticsearch The official advanced client . Based on low-level client , It's public API Specific methods and handle request marshalling and response unmarshalling .

7.15 After that, it is not recommended to use Java senior REST The client !!! Another name Java API client !!!

however Java API Client Also from the 7.15 Just started pushing it out , It's also an experimental stage ....

3、 ... and 、 adopt HttpClient operation ES

3.1、 Introduce dependent packages

 <dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.15.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>

  From the list of dependent packages, you can see httpclient

 3.2、 Build the client

RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")).build();
restClient.close();

3.3、 Execute the query

 RestClient restClient = null;
@Before
public void before(){
restClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
}
@After
public void after() throws IOException {
restClient.close();
}
public void doExecute(String method,String endpoint){
try{
Request request = new Request(method,endpoint);
Response response = restClient.performRequest(request);
System.out.println(response.getEntity());
System.out.println(response.getEntity().getContent());
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println(responseBody);
}catch (Exception e){
throw new RuntimeException(e.getCause());
}
}
@Test
public void getAll() {
doExecute("get","/");
}

3.4、 Output results

{
"name" : "WIN-ACMIHAQUB5F",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "03fvqoNRRN66JihZBxGs9A",
"version" : {
"number" : "7.16.1",
"build_flavor" : "default",
"build_type" : "zip",
"build_hash" : "5b38441b16b1ebb16a27c107a4c3865776e20c53",
"build_date" : "2021-12-11T00:29:38.865893768Z",
"build_snapshot" : false,
"lucene_version" : "8.10.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}

copyright:author[Programmer light],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130751484791.html