9300是Tcp協議端口:經過tcp協議通信,ES集羣之間是經過9300進行通信,java客戶端(TransportClient)的方式是也是以tcp協議在9300 端口上與集羣進行通訊。html
9200是Http協議端口:主要用於外部通信,外部使用RESTful接口進行訪問。 以下圖:url地址上輸入對應的RESTful接口 就能夠訪問。java
( PS:所謂低級與高級,我以爲一個很形象的比喻是,面向過程編程與面向對象編程 )編程
在 Elasticsearch 7.0 中不建議使用TransportClient,而且在8.0中會徹底刪除TransportClient。所以,官方更建議咱們用Java High Level REST Client,它執行HTTP請求,而不是序列號的Java請求。既然如此,這裏就直接用高級了。json
高級客戶端要與Elasticsearch集羣進行通訊,主版本號須要一致,次版本號沒必要相同。api
本案例,es 使用6.8.0版本,而高級客戶端使用6.6.2是能夠的。例如:6.0客戶端可以與任何6.x Elasticsearch節點通訊,而6.1客戶端確定能 夠與6.1,6.2和任何後來的6.x版本進行通 信,但與舊版本的 Elasticsearch節點通訊時可能會存在不兼容的問題,例如6.1和6.0之間, 可能6.1客戶端支持elasticsearch 6.0還沒出來的API。 elasticsearch
1 <dependency> 2 <groupId>org.elasticsearch.client</groupId> 3 <artifactId>elasticsearch-rest-high-level-client</artifactId> 4 <version>6.6.2</version> 5 </dependency>
鏈接到es集羣 RestHighLevelClient實例須要低級客戶端構建器來構建,tcp
以下所示: RestHighLevelClient client = new RestHighLevelClient(ide
RestClient.builder(
new HttpHost("192.168.20.210", 9200, "http"))post
); fetch
高級客戶端將在內部建立低級客戶端,用來執行基於提供的構建器的請求,並 管理其生命週期。
當再也不須要時,須要關閉高級客戶端實例,以便它所使用的全部資源以及底層 的http客戶端實例及其線程獲得正確釋放。能夠經過close方法來完成,該方法將關閉內部的RestClient實例。
client.close();
/* 使用高級客戶端鏈接ES集羣 */ public class ElasticSearchClient { public static void main(String[] args){ RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost ("localhost", 9200, "http") ) ); GetIndexRequest request = new GetIndexRequest(); request.indices("test"); boolean exists = false; try { exists = client.indices().exists(request, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace (); } if(exists){ System.out.println("test索引庫存在"); }else{ System.out.println("test索引庫不存在"); } //關閉高級客戶端實例,以便它所使用的全部資源以及底層 的http客戶端實例及其線程獲得正確釋放 try { client.close(); } catch (IOException e) { e.printStackTrace (); } } }
索引index(四種json,map,XContentBuilder,object)
IndexResponse indexResponse = client.index(indexRequest,RequestOptions.DEFAULT);
1 /** 2 * index api 3 * @throws IOException 4 */ 5 @Test 6 public void test3() throws IOException { 7 XContentBuilder builder = XContentFactory.jsonBuilder(); 8 builder.startObject(); 9 { 10 builder.field("user", "kimchy"); 11 builder.timeField("postDate", new Date()); 12 builder.field("message", "trying out Elasticsearch"); 13 } 14 builder.endObject(); 15 IndexRequest indexRequest = new IndexRequest("posts", "doc", "1") 16 .source(builder); 17 18 IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT); 19 System.out.println(indexResponse.getId()); 20 client.close(); 21 } 22 23 /** 24 * index api 25 * @throws IOException 26 */ 27 @Test 28 public void test4() throws IOException { 29 IndexRequest indexRequest = new IndexRequest("posts", "doc", "1") 30 .source("user", "kimchy", 31 "postDate", new Date(), 32 "message", "trying out Elasticsearch"); 33 34 IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT); 35 System.out.println(indexResponse.getId()); 36 client.close(); 37 }
運行結果:如圖從head插件中能夠看書建立了‘posts’索引,和四個文檔。
官網代碼:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.8/java-rest-high-document-get.html
@Test public void test5() throws IOException { GetRequest getRequest = new GetRequest( "posts", "doc", "1"); GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT); System.out.println(getResponse.getSource()); client.close(); }
運行結果:
{postDate=2019-06-14T09:15:05.372Z, message=trying out Elasticsearch, user=kimchy}
/** * exist api * @throws IOException */ @Test public void test6() throws IOException { GetRequest getRequest = new GetRequest( "posts", "doc", "1"); getRequest.fetchSourceContext(new FetchSourceContext (false)); getRequest.storedFields("_none_"); boolean exists = client.exists(getRequest, RequestOptions.DEFAULT); if(exists){ System.out.println("數據存在"); }else{ System.out.println("數據不存在"); } client.close(); } /** * update api * @throws IOException */ @Test public void test7() throws IOException { UpdateRequest request = new UpdateRequest( "posts", "doc", "1"); String jsonString = "{" + "\"updated\":\"2017-01-01\"," + "\"reason\":\"daily update\"" + "}"; request.doc(jsonString, XContentType.JSON); UpdateResponse updateResponse = client.update( request, RequestOptions.DEFAULT); client.close(); } /** * delete api * @throws IOException */ @Test public void test8() throws IOException { DeleteRequest request = new DeleteRequest( "posts", "doc", "1"); DeleteResponse deleteResponse = client.delete( request, RequestOptions.DEFAULT); client.close(); } /** * bulk api * @throws IOException */ @Test public void test9() throws IOException { BulkRequest request = new BulkRequest(); request.add(new DeleteRequest("posts", "doc", "3")); request.add(new UpdateRequest("posts", "doc", "2") .doc(XContentType.JSON,"other", "test")); request.add(new IndexRequest("posts", "doc", "4") .source(XContentType.JSON,"field", "baz")); BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT); if (bulkResponse.hasFailures()) { for (BulkItemResponse bulkItemResponse : bulkResponse) { if (bulkItemResponse.isFailed()) { BulkItemResponse.Failure failure = bulkItemResponse.getFailure(); System.out.println(failure.getMessage()); } } } client.close(); } /** * multi get api * @throws IOException */ @Test public void test10() throws IOException { MultiGetRequest request = new MultiGetRequest(); request.add(new MultiGetRequest.Item( "test", "user", "2")); request.add(new MultiGetRequest.Item("test", "user", "4")); MultiGetResponse responses = client.mget(request, RequestOptions.DEFAULT); for (MultiGetItemResponse itemResponse : responses) { GetResponse response = itemResponse.getResponse(); if (response.isExists()) { String json = response.getSourceAsString(); System.out.println(json); } } client.close(); }