時間:2023-05-30 11:00:01 | 來源:網(wǎng)站運(yùn)營
時間:2023-05-30 11:00:01 來源:網(wǎng)站運(yùn)營
【超詳細(xì)】手把手教你ElasticSearch集群搭建:tar -xvf elasticsearch-7.10.2-linux-x86_64.tar.gz
1. groupadd elsearch2. useradd elsearch -g elsearch -p elasticsearch3. chown -R elsearch:elsearch /usr/local/elasticsearch-7.10.2
執(zhí)行以上命令,創(chuàng)建一個名為elsearch用戶, 并賦予目錄權(quán)限。# 外網(wǎng)訪問地址network.host: 0.0.0.0
systemctl stop firewalld.servicesystemctl disable firewalld.service
JAVA_HOME=/usr/local/jdk-11.0.11# now set the path to javaif [ ! -z "$JAVA_HOME" ]; then JAVA="$JAVA_HOME/bin/java"else if [ "$(uname -s)" = "Darwin" ]; then # OSX has a different structure JAVA="$ES_HOME/jdk/Contents/Home/bin/java" else JAVA="$ES_HOME/jdk/bin/java" fifi
* soft nofile 65536* hard nofile 131072* soft nproc 2048* hard nproc 4096elsearch soft nproc 125535elsearch hard nproc 125535
重新切換用戶即可:tar -xvf kibana-7.10.2-linux-x86_64.tar.gz
chown -R elsearch:elsearch kibana-7.10.2-linux-x86_64
# 服務(wù)端口server.port: 5601# 服務(wù)地址server.host: "0.0.0.0"# elasticsearch服務(wù)地址elasticsearch.hosts: ["http://192.168.116.140:9200"]
./kibana -q
看到以下日志, 代表啟動正常log [01:40:00.143] [info][listening] Server running at http://0.0.0.0:5601
如果出現(xiàn)啟動失敗的情況, 要檢查集群各節(jié)點(diǎn)的日志, 確保服務(wù)正常運(yùn)行狀態(tài)。PUT orders{ "settings": { "index": { "number_of_shards": 2, "number_of_replicas": 2 } }}
查看索引信息, 會出現(xiàn)yellow提示:PUT orders{ "settings": { "index": { "number_of_shards": 2, "number_of_replicas": 0 } }}
將分片數(shù)設(shè)為0, 再次查看, 則顯示正常:## 創(chuàng)建索引PUT orders
3.2 查詢索引orders## 查詢索引GET orders
"number_of_shards" : "1", ## 主分片數(shù) "number_of_replicas" : "1", ## 副分片數(shù)
3.3 刪除索引 ## 刪除索引 DELETE orders
3.4 索引的設(shè)置 ## 設(shè)置索引 PUT orders { "settings": { "index": { "number_of_shards": 1, "number_of_replicas": 0 } } }
## 創(chuàng)建文檔,生成默認(rèn)的文檔idPOST orders/_doc{ "name": "襪子1雙", "price": "200", "count": 1, "address": "杭州市"}## 創(chuàng)建文檔,生成自定義文檔idPOST orders/_doc/1{ "name": "襪子1雙", "price": "2", "count": 1, "address": "杭州市"}
4.2 查詢文檔## 根據(jù)指定的id查詢GET orders/_doc/1## 根據(jù)指定條件查詢文檔GET orders/_search{ "query": { "match": { "address": "杭州市" } }}## 查詢?nèi)课臋nGET orders/_search
4.3 更新文檔## 更新文檔POST orders/_doc/1{ "price": "200"}## 更新文檔POST orders/_update/1{ "doc": { "price": "200" }}
4.4 刪除文檔## 刪除文檔DELETE orders/_doc/1
## 設(shè)置mapping信息PUT orders/_mappings{ "properties":{ "price": { "type": "long" } }}## 設(shè)置分片和映射PUT orders{ "settings": { "index": { "number_of_shards": 1, "number_of_replicas": 0 } }, "mappings": { "properties": { "name": { "type": "text" }, "price": { "type": "long" }, "count": { "type": "long" }, "address": { "type": "text" } } }}
PUT my_date_index/_doc/1{ "date": "2021-01-01" } PUT my_date_index/_doc/2{ "date": "2021-01-01T12:10:30Z" } PUT my_date_index/_doc/3{ "date": 1520071600001 }
 ES的Date類型允許可以使用的格式有: > yyyy-MM-dd HH:mm:ss > yyyy-MM-dd > epoch_millis(毫秒值)3. 復(fù)合類型 復(fù)雜類型主要有三種: Array、object、nested。 + Array類型: 在Elasticsearch中,數(shù)組不需要聲明專用的字段數(shù)據(jù)類型。但是,在數(shù)組中的所有值都必須具有相同的數(shù)據(jù)類型。舉例: ```sh POST orders/_doc/1 { "goodsName":["足球","籃球","兵乓球", 3] } POST orders/_doc/1 { "goodsName":["足球","籃球","兵乓球"] } ``` + object類型: 用于存儲單個JSON對象, 類似于JAVA中的對象類型, 可以有多個值, 比如LIST<object>,可以包含多個對象。 但是LIST<object>只能作為整體, 不能獨(dú)立的索引查詢。舉例: ```sh # 新增第一組數(shù)據(jù), 組別為美國,兩個人。 POST my_index/_doc/1 { "group" : "america", "users" : [ { "name" : "John", "age" : "22" }, { "name" : "Alice", "age" : "21" } ] } # 新增第二組數(shù)據(jù), 組別為英國, 兩個人。 POST my_index/_doc/2 { "group" : "england", "users" : [ { "name" : "lucy", "age" : "21" }, { "name" : "John", "age" : "32" } ] } ``` 這兩組數(shù)據(jù)都包含了name為John,age為21的數(shù)據(jù), 采用這個搜索條件, 實際結(jié)果: ```sh GET my_index/_search { "query": { "bool": { "must": [ { "match": { "users.name": "John" } }, { "match": { "users.age": "21" } } ] } } } ``` 結(jié)果可以看到, 這兩組數(shù)據(jù)都能找出,因為每一組數(shù)據(jù)都是作為一個整體進(jìn)行搜索匹配, 而非具體某一條數(shù)據(jù)。 + Nested類型 用于存儲多個JSON對象組成的數(shù)組,`nested` 類型是 `object` 類型中的一個特例,可以讓對象數(shù)組獨(dú)立索引和查詢。 舉例: 創(chuàng)建nested類型的索引: ```sh PUT my_index { "mappings": { "properties": { "users": { "type": "nested" } } } } ``` 發(fā)出查詢請求: ```sh GET my_index/_search { "query": { "bool": { "must": [ { "nested": { "path": "users", "query": { "bool": { "must": [ { "match": { "users.name": "John" } }, { "match": { "users.age": "21" } } ] } } } } ] } } } ``` 采用以前的條件, 這個時候查不到任何結(jié)果, 將年齡改成22, 就可以找出對應(yīng)的數(shù)據(jù): ```sh "hits" : [ { "_index" : "my_index", "_type" : "_doc", "_id" : "1", "_score" : 1.89712, "_source" : { "group" : "america", "users" : [ { "name" : "John", "age" : "22" }, { "name" : "Alice", "age" : "21" } ] } } ] ``` 4. GEO地理位置類型 現(xiàn)在大部分APP都有基于位置搜索的功能, 比如交友、購物應(yīng)用等。這些功能是基于GEO搜索實現(xiàn)的。 對于GEO地理位置類型,分為地理坐標(biāo)類型:Geo-point, 和形狀:Geo-shape 兩種類型。 | 經(jīng)緯度 | 英文 | 簡寫 | 正數(shù) | 負(fù)數(shù) | | :----- | :-------- | :------- | :--- | ---- | | 維度 | latitude | lat | 北緯 | 南緯 | | 經(jīng)度 | longitude | lon或lng | 東經(jīng) | 西經(jīng) | 創(chuàng)建地理位置索引: ```sh PUT my_locations { "mappings": { "properties": { "location": { "type": "geo_point" } } } } ``` 添加地理位置數(shù)據(jù): ```sh # 采用object對象類型 PUT my_locations/_doc/1 { "user": "張三", "text": "Geo-point as an object", "location": { "lat": 41.12, "lon": -71.34 } } # 采用string類型 PUT my_locations/_doc/2 { "user": "李四", "text": "Geo-point as a string", "location": "45.12,-75.34" } # 采用geohash類型(geohash算法可以將多維數(shù)據(jù)映射為一串字符) PUT my_locations/_doc/3 { "user": "王二麻子", "text": "Geo-point as a geohash", "location": "drm3btev3e86" } # 采用array數(shù)組類型 PUT my_locations/_doc/4 { "user": "木頭老七", "text": "Geo-point as an array", "location": [ -80.34, 51.12 ] } ``` 需求:搜索出距離我{"lat" : 40,"lon" : -70} 200km范圍內(nèi)的人: ```sh GET my_locations/_search { "query": { "bool": { "must": { "match_all": {} }, "filter": { "geo_distance": { "distance": "200km", "location": { "lat": 40, "lon": -70 } } } } } } ```## 2. ES高可用集群配置### 2.1 ElasticSearch集群介紹+ **主節(jié)點(diǎn)(或候選主節(jié)點(diǎn))** 主節(jié)點(diǎn)負(fù)責(zé)創(chuàng)建索引、刪除索引、分配分片、追蹤集群中的節(jié)點(diǎn)狀態(tài)等工作, 主節(jié)點(diǎn)負(fù)荷相對較輕, 客戶端請求可以直接發(fā)往任何節(jié)點(diǎn), 由對應(yīng)節(jié)點(diǎn)負(fù)責(zé)分發(fā)和返回處理結(jié)果。 一個節(jié)點(diǎn)啟動之后, 采用 Zen Discovery機(jī)制去尋找集群中的其他節(jié)點(diǎn), 并與之建立連接, 集群會從候選主節(jié)點(diǎn)中選舉出一個主節(jié)點(diǎn), 并且一個集群只能選舉一個主節(jié)點(diǎn), 在某些情況下, 由于網(wǎng)絡(luò)通信丟包等問題, 一個集群可能會出現(xiàn)多個主節(jié)點(diǎn), 稱為“腦裂現(xiàn)象”, 腦裂會存在丟失數(shù)據(jù)的可能, 因為主節(jié)點(diǎn)擁有最高權(quán)限, 它決定了什么時候可以創(chuàng)建索引, 分片如何移動等, 如果存在多個主節(jié)點(diǎn), 就會產(chǎn)生沖突, 容易產(chǎn)生數(shù)據(jù)丟失。要盡量避免這個問題, 可以通過 discovery.zen.minimum_master_nodes 來設(shè)置最少可工作的候選主節(jié)點(diǎn)個數(shù)。 建議設(shè)置為(候選主節(jié)點(diǎn)/2) + 1 比如三個候選主節(jié)點(diǎn),該配置項為 (3/2)+1 ,來保證集群中有半數(shù)以上的候選主節(jié)點(diǎn), 沒有足夠的master候選節(jié)點(diǎn), 就不會進(jìn)行master節(jié)點(diǎn)選舉,減少腦裂的可能。 主節(jié)點(diǎn)的參數(shù)設(shè)置: ```sh node.master = true node.data = false
node.master = falsenode.data = true
node.master = falsenode.data = false
node.ingest = true
PUT orders{ "settings":{ "number_of_shards":2, ## 主分片 2 "number_of_replicas":2 ## 副分片 4 }}
mkdir /usr/local/clustercd /usr/local/clustertar -xvf elasticsearch-7.10.2-linux-x86_64.tar.gz
將安裝包解壓至/usr/local/cluster目錄。vi /usr/local/cluster/elasticsearch-7.10.2-node1/config/elasticsearch.yml
192.168.116.140, 第一臺節(jié)點(diǎn)配置內(nèi)容:# 集群名稱cluster.name: my-application#節(jié)點(diǎn)名稱node.name: node-1# 綁定IP地址network.host: 192.168.116.140# 指定服務(wù)訪問端口http.port: 9200# 指定API端戶端調(diào)用端口transport.tcp.port: 9300#集群通訊地址discovery.seed_hosts: ["192.168.116.140:9300", "192.168.116.140:9301","192.168.116.140:9302"]#集群初始化能夠參選的節(jié)點(diǎn)信息cluster.initial_master_nodes: ["192.168.116.140:9300", "192.168.116.140:9301","192.168.116.140:9302"]#開啟跨域訪問支持,默認(rèn)為falsehttp.cors.enabled: true##跨域訪問允許的域名, 允許所有域名http.cors.allow-origin: "*"
修改目錄權(quán)限:chown -R elsearch:elsearch /usr/local/cluster/elasticsearch-7.10.2-node1
cd /usr/local/clustercp -r elasticsearch-7.10.2-node1 elasticsearch-7.10.2-node2cp -r elasticsearch-7.10.2-node1 elasticsearch-7.10.2-node3
# 集群名稱cluster.name: my-application#節(jié)點(diǎn)名稱node.name: node-2# 綁定IP地址network.host: 192.168.116.140# 指定服務(wù)訪問端口http.port: 9201# 指定API端戶端調(diào)用端口transport.tcp.port: 9301#集群通訊地址discovery.seed_hosts: ["192.168.116.140:9300", "192.168.116.140:9301","192.168.116.140:9302"]#集群初始化能夠參選的節(jié)點(diǎn)信息cluster.initial_master_nodes: ["192.168.116.140:9300", "192.168.116.140:9301","192.168.116.140:9302"]#開啟跨域訪問支持,默認(rèn)為falsehttp.cors.enabled: true##跨域訪問允許的域名, 允許所有域名http.cors.allow-origin: "*"
192.168.116.140 第三臺節(jié)點(diǎn)配置內(nèi)容:# 集群名稱cluster.name: my-application#節(jié)點(diǎn)名稱node.name: node-3# 綁定IP地址network.host: 192.168.116.140# 指定服務(wù)訪問端口http.port: 9202# 指定API端戶端調(diào)用端口transport.tcp.port: 9302#集群通訊地址discovery.seed_hosts: ["192.168.116.140:9300", "192.168.116.140:9301","192.168.116.140:9302"]#集群初始化能夠參選的節(jié)點(diǎn)信息cluster.initial_master_nodes: ["192.168.116.140:9300", "192.168.116.140:9301","192.168.116.140:9302"]#開啟跨域訪問支持,默認(rèn)為falsehttp.cors.enabled: true##跨域訪問允許的域名, 允許所有域名http.cors.allow-origin: "*"
su elsearch/usr/local/cluster/elasticsearch-7.10.2-node1/bin/elasticsearch -d/usr/local/cluster/elasticsearch-7.10.2-node2/bin/elasticsearch -d/usr/local/cluster/elasticsearch-7.10.2-node3/bin/elasticsearch -d
注意: 如果啟動出現(xiàn)錯誤, 將各節(jié)點(diǎn)的data目錄清空, 再重啟服務(wù)。elasticsearch.hosts: ["http://192.168.116.140:9200","http://192.168.116.140:9201","http://192.168.116.140:9202"]
重啟kibana服務(wù), 進(jìn)入控制臺:PUT orders{ "settings": { "index": { "number_of_shards": 2, "number_of_replicas": 2 } }}
可以看到, 這次結(jié)果是正常:PUT orders{ "settings": { "index": { "number_of_shards": 2, "number_of_replicas": 5 } }}
可以看到出現(xiàn)了yellow警告錯誤:本文由傳智教育博學(xué)谷 - 狂野架構(gòu)師教研團(tuán)隊發(fā)布如果本文對您有幫助,歡迎關(guān)注和點(diǎn)贊;如果您有任何建議也可留言評論或私信,您的支持是我堅持創(chuàng)作的動力轉(zhuǎn)載請注明出處!
關(guān)鍵詞:詳細(xì),把手
客戶&案例
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。