Elasticsearch7.9.1 學習
個人學習筆記,謝絕轉載!!!
原文:https://www.cnblogs.com/wshenjin/p/13816873.html
索引:
在Elasticsearch中儲存資料的行為就叫做索引(indexing),不過在索引之前,我們需要明確資料應該儲存在哪裡。
在Elasticsearch中,文件歸屬於一種型別(type),而這些型別存在於索引(index)中,我們可以畫一些簡單的對比圖來類比傳統關係型資料庫:
Relational DB -> Databases -> Tables -> Rows -> Columns Elasticsearch -> Indices -> Types -> Documents -> Fields
Elasticsearch叢集可以包含多個索引(indices)(資料庫),每一個索引可以包含多個型別(types)(表),每一個型別包含多個文件(documents)(行),然後每個文件包含多個欄位(Fields)(列)。
- 關係型資料庫中的資料庫(DataBase),等價於ES中的索引(Index)
- 1個數據庫下面有N張表(Table),等價於1個索引Index下面有N多型別(Type),
- 1個數據庫表(Table)下的資料由多行(ROW)多列(column,屬性)組成,等價於1個Type由多個文件(Document)和多Field組成。
- 在關係型資料庫裡面,schema定義了表、每個表的欄位,還有表和欄位之間的關係。 在ES中:Mapping定義索引下的Type的欄位處理規則,即索引如何建立、索引型別、是否儲存原始索引JSON文件、是否壓縮原始JSON文件、是否需要分詞處理、如何進行分詞處理等。
- 在資料庫中的增insert、刪delete、改update、查select操作等價於ES中的增PUT/POST、刪Delete、改_update、查GET.
「索引」含義的區分
索引(index)這個詞在Elasticsearch中有著不同的含義:
● 索引(名詞) 如上文所述,一個索引(index)就像是傳統關係資料庫中的資料庫,它是相關文件儲存的地方,index的複數是indices 或indexes。 ● 索引(動詞) 「索引一個文件」表示把一個文件儲存到索引(名詞)裡,以便它可以被檢索或者查詢。這很像SQL中的INSERT關鍵字,差別是,如果文件已經存在,新的文件將覆蓋舊的文件。 ● 倒排索引 傳統資料庫為特定列增加一個索引,例如B-Tree索引來加速檢索。Elasticsearch和Lucene使用一種叫做倒排索引(inverted index)的資料結構來達到相同目的。
預設情況下,文件中的所有欄位都會被索引(擁有一個倒排索引),只有這樣他們才是可被搜尋的。
分片和副本
為了將資料新增到Elasticsearch,我們需要索引(index)——一個儲存關聯資料的地方。實際上,索引只是一個用來指向一個或多個分片(shards)的“邏輯名稱空間(logical namespace)”.
一個分片(shard)是一個最小級別“工作單元(worker unit)”,它只是儲存了索引中所有資料的一部分。分片就是一個Lucene例項,並且它本身就是一個完整的搜尋引擎。文件儲存在分片中,並且在分片中被索引,但是應用程式不會直接與它們通訊,取而代之的是,直接與索引通訊。
分片是Elasticsearch在叢集中分發資料的關鍵。把分片想象成資料的容器。文件儲存在分片中,然後分片分配到叢集中的節點上。當叢集擴容或縮小,Elasticsearch將會自動在節點間遷移分片,以使叢集保持平衡。
分片可以是主分片(primary shard)或者是複製分片(replica shard)。索引中的每個文件屬於一個單獨的主分片,所以主分片的數量決定了索引最多能儲存多少資料。
理論上主分片能儲存的資料大小是沒有限制的,限制取決於實際的使用情況。分片的最大容量完全取決於:硬體儲存的大小、文件的大小和複雜度、如何索引和查詢你的文件,以及期望的響應時間。
複製分片只是主分片的一個副本,它可以防止硬體故障導致的資料丟失,同時可以提供讀請求,比如搜尋或者從別的shard取回文件。
當索引建立完成的時候,主分片的數量就固定了,但是複製分片的數量可以隨時調整。
叢集健康:
在Elasticsearch叢集中可以監控統計很多資訊,但是隻有一個是最重要的:叢集健康(cluster health)。叢集健康有三種狀態:green、yellow或red。
# curl -XGET http://127.0.0.1:9200/_cluster/health
在一個沒有索引的空叢集中執行如上查詢,將返回這些資訊:
{
"cluster_name": "elasticsearch",
"status": "green", <1>
"timed_out": false,
"number_of_nodes": 1,
"number_of_data_nodes": 1,
"active_primary_shards": 0,
"active_shards": 0,
"relocating_shards": 0,
"initializing_shards": 0,
"unassigned_shards": 0
}
status欄位提供一個綜合的指標來表示叢集的的服務狀況。三種顏色各自的含義:
green所有主要分片和複製分片都可用
yellow所有主要分片可用,但不是所有複製分片都可用
red不是所有的主要分片都可用
所有索引:
# curl -XGET http://127.0.0.1:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open webserver-access-log-2017.11.01 KXbLvV2iT7a7SDTzwmuMpw 5 1 62469 0 89.1mb 44.5mb
green open nginx-access-log-2017.10.31 y0RpxeGVQZ2tRpl1hfDFRw 5 1 12619 0 19.5mb 9.7mb
green open .monitoring-data-2 htaFQHKtRNaJOpGXxIEs3Q 1 1 3 0 14.3kb 7.1kb
green open .monitoring-kibana-2-2017.11.01 lK8587HOS7apx1fsKG8Uow 1 1 654 0 387.6kb 193.8kb
green open nginx-access-log-2017.11.02 JEAqXh9bTiGDjOczhVPhOA 5 1 18863 0 28.6mb 14.3mb
green open .monitoring-es-2-2017.10.31 5oE3xVVPTTKYaflBnNLyUg 1 1 423 78 762.7kb 381.3kb
green open .security nu8qiah9S0aw6QR4VXoRcA 1 1 2 0 11kb 5.5kb
green open nginx-access-log-2017.11.01 b8lzWMKWQOuvGAhZOe-Tqw 5 1 62582 0 88.5mb 44.2mb
green open .monitoring-es-2-2017.11.01 cZnvACgzSeicB-s_LCsGaQ 1 1 8768 51 8mb 4mb
green open .kibana LRc0dr8MRN6jLhciJDaG2w 1 1 3 0 44.6kb 22.3kb
green open webserver-access-log-2017.11.02 3EkGPnCGTmqegXehcqYYdA 5 1 18863 0 28.4mb 14.2mb
green open webserver-access-log-2017.10.31 kh3cDN3QQCOVz8WrvO6lsQ 5 1 12657 0 20mb 10mb
Elasticsearch單機部署
安裝
# rpm -ivh elasticsearch-7.9.1-x86_64.rpm
# mkdir -p /data/elk/elasticsearch/{data,logs}
# chown -R elasticsearch:elasticsearch /data/elk/elasticsearch
配置
jvm.options
-Xms2g
-Xmx2g
elasticsearch.yml
node.name: es_node
cluster.initial_master_nodes: ["es_node"]
path.data: /data/elk/elasticsearch/data
path.logs: /data/elk/elasticsearch/logs
network.host: 0.0.0.0
http.port: 9200
啟動
#/etc/init.d/elasticsearch start
檢查
# curl -s -XGET http://127.0.0.1:9200/_cluster/health | jq .
{
"cluster_name": "elasticsearch",
"status": "green",
"timed_out": false,
"number_of_nodes": 1,
"number_of_data_nodes": 1,
"active_primary_shards": 0,
"active_shards": 0,
"relocating_shards": 0,
"initializing_shards": 0,
"unassigned_shards": 0,
"delayed_unassigned_shards": 0,
"number_of_pending_tasks": 0,
"number_of_in_flight_fetch": 0,
"task_max_waiting_in_queue_millis": 0,
"active_shards_percent_as_number": 100
}