一、elasticsearch版本與jdk版本對應關系
2020年3月27日更新
二、Transport Client和Rest Client對比
所有Elasticsearch操作都是使用Client對象執行的。Client 定義的所有API都是異步執行的。(要么使用事件監聽器回調或者使用Future模式)
Elasticsearch計划在Elasticsearch 7.0中棄用TransportClient,在8.0中完全刪除它。故在實際使用過程中建議您使用Java高級REST client。Rest client執行HTTP請求來執行操作,無需再序列化的Java請求。
按照官方的意思,以后ElasticSearch應該不會再為某一種具體語言單獨提供客戶端API,而是使用通用rest請求(http)來ElasticSearch服務器進行交互。
接下來我們會從java api開始進入ElasticSearch API的學習。
ElasticSearch Client按照編程語言提供如下實現:
官方文檔鏈接:https://www.elastic.co/guide/en/elasticsearch/client/index.html
接下來將重點分析JAVA Client與Java REST Client。
2、TransportClient詳解
2.1 TransportClient概述
TransportClient 是ElasticSearch(java)客戶端封裝對象,使用transport模塊遠程連接到Elasticsearch集群,該transport node並不會加入集群,而是簡單的向ElasticSearch集群上的節點發送請求。transport node使用輪詢機制進行集群內的節點進行負載均衡,盡管大多數操作(請求)可能是“兩跳操作”。(圖片來源於Elasticsearch權威指南)
正如上述圖所述,以一個新建操作為例,第一個請求首先發送到NODE1,然后會根據ID進行路由計算(hashcode(id)%主分片個數),例如使用p0(第一個主分片),此時NODE1會將請求轉發到Node3,然后客戶端發送第二個請求,會發送到NODE2上(上文中的輪詢機制)。
默認構建TransportClient的方法如下:
// on startup TransportClient client = new PreBuiltTransportClient(Settings.EMPTY) // @1 .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.10"), 9300)) // @2 .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.11"), 9300)); // on shutdown client.close();
代碼@1:使用空配置。
代碼@2:連接ElasticSearch 節點,可以通過addTransportAddress方法連接多個Node節點,這樣請求會輪流發送到這些節點上,實現集群節點在接受請求時的負載均衡。
TransportClient級別的主要參數如下:
接下來重點描述一下client.transport.sniff參數,集群群嗅探機制。
在創建TransportClient時可以通過addTransportAddress來靜態的增加ElasticSearch集群中的節點,如果開啟集群群嗅探機制,即開啟節點動態發現機制,允許動態添加和刪除節點。當啟用嗅探功能時,首先客戶端會連接addTransportAddress中的節點上。在此之后,客戶端將調用這些節點上的內部集群狀態API來發現可用的數據節點。客戶端的內部節點列表將僅被發現的數據數據節點替換。默認情況下,這個列表每5秒刷新一次。也就意味着如果該節點不是數據節點,則列表可能不包括它連接的原始節點。例如,如果您最初連接到一個主節點,在嗅探之后,如果發現了有其對應的數據節點,則不會再向該主節點發出請求,而是向任何數據節點發出請求。傳輸客戶端排除非數據節點的原因是為了避免只向主節點發送搜索流量。
使用配置構建Settings構建TransportClient對象代碼如下:
Settings settings = Settings.builder() .put("cluster.name", "myClusterName") .put("client.transport.sniff", "true").build(); TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.10"), 9300)) ; //Add transport addresses and do something with the client...
2.2、TransportClient API
TransportClient 的核心類繼承圖如下:
上述API的設計要點:
整個客戶端API提供兩個最底層的方法,execute,其關鍵特征如下:
ActionFuture execute(Action<Request, Response, RequestBuilder> action, Request request);
返回ActionFuture,根據名稱即可知道,該方法是典型的異步調用,Future模式。
void execute(Action<Request, Response, RequestBuilder> action, Request request, ActionListener listener);
無返回值,但需要傳入ActionListener listener,同樣根據名稱即可知道,該參數的作用是事件監聽器(回調方法),也就是收到服務端響應后,調用回調函數,進行結果處理。
注意:ElasticSearch Client API 其本質是使用異步請求模式。
prepare 開頭的方法,例如IndexRequestBuilder prepareIndex()
這類API的設計是使用Build模式,先通過build構建請求參數,最終會通過調用get()方法完成接口調用。
TransportClient Api就先解釋到這里了,后續會詳細對上述API進行分類詳解。
2.3 Maven依懶
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>6.4.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.11.1</version> </dependency>
3、Java Rest Client詳解
Java REST客戶端有兩種風格:
Java Low Level REST Client:elasticsearch client 低級別客戶端。它允許通過http請求與Elasticsearch集群進行通信。API本身不負責數據的編碼解碼,由用戶去編碼解碼。它與所有的ElasticSearch版本兼容。
Java High Level REST Client:Elasticsearch client官方高級客戶端。基於低級客戶端,它定義的API,已經對請求與響應數據包進行編碼解碼。
3.1 Java High Level REST Client
3.1.1 初始化
RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("localhost", 9200, "http"), new HttpHost("localhost", 9201, "http"))); // close client.close();
new HttpHost(“localhost”, 9200, “http”)其機制與TransportClient的addTransportAddress的作用一致。
3.1.2 核心API依懶
RestHighLevelClient 的類圖 如下:
其API設計具有如下特征:
每個API提供同步與異步調用,方法名以async結尾的方法為異步調用,需要提供對應的ActionListener實現。
每個API都可以提供RequestOptions對象來定制請求選型。
本節將不會對上述API一一介紹,上述API會在后續文章中詳細解析。
3.1.3 Maven依懶
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>6.4.0</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>6.4.0</version> <type>pom</type> </dependency>
本文就先介紹到這里了,詳細分析介紹了Elasticsearch兩大客戶端 TransportClient與RestHighLevelClient ,后續文章會詳細介紹各個API,例如文檔的索引、更新、查詢、刪除、批量查詢,Search API等。
elasticsearch安裝 及 啟動異常解決
虛擬機使用net連接模式
1
Download and unzip the latest Elasticsearch distribution
2
Run bin/elasticsearch on Unix or bin\elasticsearch.bat on Windows
3
Run curl -X GET http://localhost:9200/
官網 :www.elastic.co
包含 安裝介質 和 權威指南
1下載
elasticsearch 下載地址: https://www.elastic.co/cn/downloads/elasticsearch
linux 對應版本 elasticsearch-7.0.0-rc2-linux-x86_64.tar.gz 大小330 MB
2建用戶
useradd es
passwd es
123456
es/123456
3上傳到虛擬機
put elasticsearch-7.0.0-rc2-linux-x86_64.tar.gz
4解壓
tar -zxvf elasticsearch-7.0.0-rc2-linux-x86_64.tar.gz
啟動
cd elasticsearch-7.0.0-rc2/bin
./elasticsearch
5警告跳過
unable to install syscall filter:
java.lang.UnsupportedOperationException: seccomp unavailable: requires kernel 3.5+ with CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER compiled in
6檢驗
http://localhost:9200/
{
"name" : "localhost.localdomain",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "WuVD9aKLTYyt_XbWbxZxAg",
"version" : {
"number" : "7.0.0-rc2",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "f076a79",
"build_date" : "2019-04-02T17:20:40.380908Z",
"build_snapshot" : false,
"lucene_version" : "8.0.0",
"minimum_wire_compatibility_version" : "6.7.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
遇到得問題
7 http://ip:9200/ 無法訪問
vim config/elasticsearch.yml
# 增加
network.host: 0.0.0.0
8重啟 es 異常
ERROR: [5] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
[2]: max number of threads [1024] for user [es] is too low, increase to at least [4096]
[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[4]: failed to install; check the logs and fix your configuration or disable system call filters at your own risk
[5]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
[2019-04-09T09:51:29,228][INFO ][o.e.n.Node ] [localhost.localdomain] stopping ...
[2019-04-09T09:51:29,264][INFO ][o.e.n.Node ] [localhost.localdomain] stopped
[2019-04-09T09:51:29,265][INFO ][o.e.n.Node ] [localhost.localdomain] closing ...
[2019-04-09T09:51:29,320][INFO ][o.e.n.Node ] [localhost.localdomain] closed
[2019-04-09T09:51:29,323][INFO ][o.e.x.m.p.NativeController] [localhost.localdomain] Native controller process has stopped - no new native processes can be started
切換root用戶
vi /etc/security/limits.conf
在倒數第二行
* soft nofile 65536
* hard nofile 65536
# End of file
vi /etc/sysctl.conf
添加
vm.max_map_count=655360
保存后執行
sysctl -p
重啟es 異常
ERROR: [3] bootstrap checks failed
[1]: max number of threads [1024] for user [es] is too low, increase to at least [4096]
[2]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
[3]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
vi /etc/security/limits.d/90-nproc.conf
修改
* soft nproc 1024
為
* soft nproc 4096
重啟es 異常
ERROR: [2] bootstrap checks failed
[1]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
[2]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
在 elasticsearch.yml中添加配置項:bootstrap.system_call_filter為false:
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
重啟es 異常
ERROR: [1] bootstrap checks failed
[1]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
修改
elasticsearch.yml
取消注釋保留一個節點
cluster.initial_master_nodes: ["node-1"]
這個的話,這里的node-1是上面一個默認的記得打開就可以了
重啟 正常
jvm內存分配的問題heap size [268435456] not equal to maximum heap size [2147483648],需要修改的jvm配置
*此操作需要root權限
[root@localhost ~]# sysctl -w vm.max_map_count=262144
查看修改
[root@localhost ~]# sysctl -a|grep vm.max_map_count
vm.max_map_count = 262144
或者永久性修改
[root@localhost ~]# cat /etc/sysctl.conf | grep -v "vm.max_map_count" > /tmp/system_sysctl.conf
[root@localhost ~]# echo "vm.max_map_count=262144" >> /tmp/system_sysctl.conf
[root@localhost ~]# mv /tmp/system_sysctl.conf /etc/sysctl.conf
mv:是否覆蓋"/etc/sysctl.conf"? y
[root@localhost ~]# cat /etc/sysctl.conf
# System default settings live in /usrb/sysctl.d/00-system.conf. # To override those settings, enter new settings here, or in an /etc/sysctl.d/<name>.conf file # # For more information, see sysctl.conf(5) and sysctl.d(5). vm.max_map_count=262144
[root@localhost ~]# sysctl -p
vm.max_map_count = 262144
————————————————
版權聲明:本文為CSDN博主「happyzxs」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/happyzxs/article/details/89156068
原文鏈接:https://blog.csdn.net/prestigeding/article/details/83188043