Elasticsearch介紹與部署
阿新 • • 發佈:2020-11-30
Elasticsearch 學習
一、生活中的資料
1.資料的分類
1)結構化資料
行資料,以二維表的形式儲存
2)非結構化資料
沒有具體的結構,視訊,文字,音樂檔案
3)半結構化資料
xml表格,word文件,PPT,html檔案
2.搜尋的種類
1)結構化資料搜尋
資料之間我們會對他建立一定的關係,生成二維表進行查詢
2)非結構化資料搜尋
1.順序掃描
2.全文檢索
二、ES介紹
1.什麼是ES
是一個高度可擴充套件的開源全文搜尋和分析引擎,它可實現資料的實時全文搜尋搜尋、 支援分散式可實現高可用、提供API介面,可以處理大規模日誌資料, 比如Nginx、Tomcat、系統日誌等功能。 ElasticSearch是一個基於Lucene的搜尋伺服器 1).ES是全文檢索的框架,專門做搜尋,支援分散式,叢集 2).ES即為了解決原生Lucene使用的不足,優化Lucene的呼叫方式 3).高可用的分散式叢集 ,處理PB級別資料
2.ES和資料結構對比
mysql | ES |
---|---|
庫(database) | 索引(index) |
表(table) | 型別(type) |
列(欄位) | 項 |
真實資料行 | 文件(doc) |
3.ES原理
1.儲存資料是進行全文檢索
2.全文檢索後建立倒排索引
1)全文檢索
將儲存的資料進行操作
1.分詞
2.找出關鍵詞
3.搜尋索引
4.匹配,命中,計算命中率
5.根據命中進行排序
2)倒排索引
java 是世界上最好的語言 php 是世界上最好的語言 python 是世界上最好的語言 https://www.cnblogs.com/ajianbeyourself/p/11280247.html
分詞後的詞條 | 第一句話 | 第二句話 | 第三句話 |
---|---|---|---|
java | 命中 | ||
php | 命中 | ||
python | 命中 | ||
是 | 命中 | 命中 | 命中 |
世界上 | 命中 | 命中 | 命中 |
最好的 | 命中 | 命中 | 命中 |
語言 | 命中 | 命中 | 命中 |
4.倒排索引的術語
1.詞條:索引最小的儲存單位,拆分一組詞之後每一個字或者詞
2.詞典:詞條儲存的位置,一般放在記憶體
3.倒排表:記錄字或者詞命中的次數或者順序
4.倒排檔案:存放倒排表的位置,一般放在磁碟中
5.ES功能
1.分散式儲存 2.全文檢索: select * from table; 3.結構化檢索: select * from table where id >1; 4.資料分析: select count(*) from table;
6.ES的使用場景
1.大量資料儲存
2.搜尋資料
3.分析資料(ELK)
4.搜尋高亮顯示
7.ES的特點
1.可以部署單點或者叢集
2.高效能
3.支援分散式
4.不需要會java
5.部署簡單
6.功能豐富
三、部署ES
1.伺服器時間同步
[root@es01 ~]# ntpdate time1.aliyun.com
2.安裝java環境
1.上傳java環境包
[root@es01 ~]# rz
[root@es01 ~]# ll
-rw-r--r-- 1 root root 170023183 Aug 14 2018 jdk-8u181-linux-x64.rpm
2.安裝
[root@es01 ~]# rpm -ivh jdk-8u181-linux-x64.rpm
3.驗證
[root@es01 ~]# java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
3.安裝es
1)上傳包
[root@es01 ~]# rz
[root@es01 ~]# ll
-rw-r--r-- 1 root root 114059630 Jan 29 2019 elasticsearch-6.6.0.rpm
2)安裝
[root@es01 ~]# rpm -ivh elasticsearch-6.6.0.rpm
#根據提示繼續操作
[root@es01 ~]# systemctl daemon-reload
[root@es01 ~]# systemctl enable elasticsearch.service
Created symlink from /etc/systemd/system/multi-user.target.wants/elasticsearch.service to /usr/lib/systemd/system/elasticsearch.service.
[root@es01 ~]# systemctl start elasticsearch.service
3)驗證
[root@es01 ~]# netstat -lntp | grep java
tcp6 0 0 127.0.0.1:9200 :::* LISTEN 8257/java
tcp6 0 0 ::1:9200 :::* LISTEN 8257/java
tcp6 0 0 127.0.0.1:9300 :::* LISTEN 8257/java
tcp6 0 0 ::1:9300 :::* LISTEN 8257/java
[root@es01 ~]# curl 127.0.0.1:9200
{
"name" : "wUdyBOT",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "okeoXx9-QWefW6jCVhU1Xw",
"version" : {
"number" : "6.6.0",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "a9861f4",
"build_date" : "2019-01-24T11:27:09.439740Z",
"build_snapshot" : false,
"lucene_version" : "7.6.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
4.ES相關的配置檔案
[root@es01 ~]# rpm -qc elasticsearch
/etc/elasticsearch/elasticsearch.yml # 主配置檔案
/etc/elasticsearch/jvm.options # 虛擬記憶體設定檔案
/etc/elasticsearch/log4j2.properties # 日誌配置
/etc/elasticsearch/role_mapping.yml # 索引的規則檔案
/etc/elasticsearch/roles.yml # 規則檔案
/etc/init.d/elasticsearch # 啟動指令碼
/etc/sysconfig/elasticsearch # es的系統配置
/usr/lib/sysctl.d/elasticsearch.conf # 庫檔案的配置
/usr/lib/systemd/system/elasticsearch.service # systemd管理啟動程式
5.配置ES
[root@es01 ~]# vim /etc/elasticsearch/elasticsearch.yml
#叢集名稱(單節點不需要配置)
#cluster.name: my-application
#節點名字
node.name: es-1
#ES的資料目錄
path.data: /data/es/data
#指定ES的日誌目錄
path.logs: /data/es/log
#開啟記憶體鎖(在CentOS7中支援記憶體鎖並且要修改啟動指令碼)
bootstrap.memory_lock: true
#ES監聽地址
network.host: 10.0.0.71 127.0.0.1 172.16.1.71
#ES監聽埠
http.port: 9200
#配置叢集地址
#discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#叢集投票節點數,一般為叢集節點的半數以上
#discovery.zen.minimum_master_nodes:
#最終配置
[root@es01 ~]# grep '^[a-z]' /etc/elasticsearch/elasticsearch.yml
node.name: es-1
path.data: /data/es/data
path.logs: /data/es/log
bootstrap.memory_lock: true
network.host: 10.0.0.71 127.0.0.1 172.16.1.71
http.port: 9200
6.建立資料和日誌目錄
[root@es01 ~]# mkdir /data/es/{data,log} -p
#授權
[root@es01 ~]# chown -R elasticsearch.elasticsearch /data/
7.重啟ES
1.重啟
[root@es01 ~]# systemctl restart elasticsearch
2.重啟失敗
3.檢視原因
[2020-11-30T16:36:12,351][ERROR][o.e.b.Bootstrap ] [es-1] node validation exception
[1] bootstrap checks failed
[1]: memory locking requested for elasticsearch process but memory is not locked
#報錯說記憶體沒有鎖定
4.解決
#修改啟動指令碼
[root@es01 ~]# vim /usr/lib/systemd/system/elasticsearch.service
... ...
[Service]
... ...
#修改記憶體限制(去掉此行註釋)
LimitMEMLOCK=infinity
#重新載入啟動指令碼
[root@es01 ~]# systemctl daemon-reload
8.驗證ES
#瀏覽器訪問 http://10.0.0.71:9200/
{
"name" : "es-1",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "KoSAwa9ZQwC4gji_qCJCWQ",
"version" : {
"number" : "6.6.0",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "a9861f4",
"build_date" : "2019-01-24T11:27:09.439740Z",
"build_snapshot" : false,
"lucene_version" : "7.6.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
四、與ES進行互動
1.使用curl命令與ES進行互動
1)特點
1.使用不方便,容易出錯,命令複雜
2.不需要安裝任何服務和外掛,只需要curl命令
2)使用方式
1>建立索引(建庫)
[root@es01 ~]# curl -XPUT '10.0.0.71:9200/student?pretty'
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "student"
}
2>插入資料
[root@es01 ~]# curl -XPUT '10.0.0.71:9200/student/user/1?pretty' -H 'Content-Type: application/json' -d '{"name": "lhd","sex":"man","age":"18","about":"good good study","interests":["chinese","english"]}'
{
"_index" : "student",
"_type" : "user",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
3>檢視資料
[root@es01 ~]# curl -GET '10.0.0.71:9200/student/user/1?pretty'
{
"_index" : "student",
"_type" : "user",
"_id" : "1",
"_version" : 3,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "lhd",
"sex" : "man",
"age" : "18",
"about" : "good good study",
"interests" : [
"chinese",
"english"
]
}
}
2.使用外掛的方式進行互動
外掛是為了完成不同的功能,官方提供了一些外掛但大部分是收費的,
另外也有一些開發愛好者提供的外掛,可以實現對elasticsearch叢集的狀態監控與管理配置等功能,
我們現在要安裝的是Elasticsearch的head外掛,此外掛提供elasticsearch的web介面功能。
安裝Elasticsearch的head外掛時,要安裝npm,npm的全稱是Node Package Manager,
是隨同NodeJS一起安裝的包管理和分發工具,它很方便讓JavaScript
開發者下載、安裝、上傳以及管理已經安裝的包。
在Elasticsearch 5.x版本以後不再支援直接安裝head外掛,而是需要通過啟動一個服務方式。
Github地址:https://github.com/mobz/elasticsearch-head
1)特點
1.檢視資料簡單,操作簡單
2.需要安裝nodejs,安裝複雜費時
2)安裝外掛方式一(複雜建議方式二)
#安裝npm(只需要在一個節點安裝即可,如果前端還有nginx做反向代理可以每個節點都裝)
[root@elkstack01 ~]# yum install -y npm
#進入下載head外掛程式碼目錄
[root@elkstack01 src]# cd /usr/local/
#從GitHub上克隆程式碼到本地
[root@elkstack01 local]# git clone git://github.com/mobz/elasticsearch-head.git
#克隆完成後,進入elasticsearch外掛目錄
[root@elkstack01 local]# cd elasticsearch-head/
#清除快取
[root@elkstack01 elasticsearch-head]# npm cache clean -f
#使用npm安裝n模組(不同的專案js指令碼所需的node版本可能不同,所以就需要node版本管理工具)
[root@elkstack01 elasticsearch-head]# npm install -g n
#安裝最新版本n模組
[root@elkstack01 elasticsearch-head]# n stable
#生成grunt
[root@elkstack01 elasticsearch-head]# npm install grunt -save
#確認生成grunt檔案
[root@elkstack01 elasticsearch-head]# ll node_modules/grunt
#執行安裝grunt
[root@elkstack01 elasticsearch-head]# npm install
#後臺啟動head外掛(切記,必須在外掛目錄下執行啟動命令)
[root@elkstack01 elasticsearch-head]# npm run start &
#驗證埠是否啟動成功
[root@elkstack01 elasticsearch-head]# netstat -lntup
tcp 0 0 0.0.0.0:9100 0.0.0.0:* LISTEN 11293/grunt
#啟動成功後,修改elasticsearch配置檔案
[root@elkstack01 elasticsearch-head]# vim /etc/elasticsearch/elasticsearch.yml
#新增如下兩行,開啟跨域訪問支援(新增在配置檔案最後即可)
http.cors.enabled: true
http.cors.allow-origin: "*"
#重啟elasticsearch
[root@elkstack01 elasticsearch-head]# /etc/init.d/elasticsearch restart
3)外掛安裝方式二
1.windows下建立一個目錄
2.將外掛解壓到目錄下
3.谷歌瀏覽器,右上角三個點或者是哪個豎線或者箭頭
4.更多工具 -> 擴充套件程式
5.開啟開發者模式
6.載入已解壓的擴充套件程式,選中解壓包的目錄
7.使用時,直接點選搜尋框後面的放大鏡
3.使用kibana進行互動
1)安裝kibana
1.上傳包
[root@es01 ~]# rz
[root@es01 ~]# ll
-rw-r--r-- 1 root root 185123116 Jan 29 2019 kibana-6.6.0-x86_64.rpm
2.安裝
[root@es01 ~]# rpm -ivh kibana-6.6.0-x86_64.rpm
2)配置
[root@es01 ~]# grep '^[a-z]' /etc/kibana/kibana.yml
server.port: 5601
server.host: "10.0.0.71"
elasticsearch.hosts: ["http://172.16.1.71:9200"]
3)啟動
#啟動
[root@es01 ~]# systemctl start kibana
#驗證
[root@es01 ~]# netstat -lntp
tcp 0 0 10.0.0.71:5601
4)訪問測試
http://10.0.0.71:5601