ElasticSearch學(xué)習(xí)之Es集群Api操作示例
前言
該系列默認(rèn)開啟Nacos
服務(wù),還不會(huì)搭建的小伙伴可以參考往期文章~
上文,我們帶大家利用docker來快速搭建Es集群,如果還不會(huì)搭建的小伙伴,可以閱讀上文,參考我的部署方式。
本期我們重點(diǎn)圍繞es
本身來給大家作一些講解以及實(shí)戰(zhàn)。雖然市面上已經(jīng)有一些成熟的ORM框架可以很好的根springboot
整合,我們只要調(diào)調(diào)方法就好了,但是對(duì)于我們初學(xué)來講,不是很友好,首先我們得弄清楚它到底是怎么進(jìn)行操作的。
舉個(gè)例子,哪天領(lǐng)導(dǎo)跟你講,現(xiàn)在幫我拉一批數(shù)據(jù),半小時(shí)之后給我,你難不成回復(fù)等我開發(fā)完,上完線才能查到?而且這種可能只是一次性的需求,顯然是不大行的。這時(shí),如果你可以脫離框架,知道它的一些查詢語法,問題不就很快就解決了,也不用你開發(fā)再調(diào)試。
這里主要想告訴大家,學(xué)習(xí)新知識(shí)的時(shí)候,首先要弄清楚知識(shí)的本身,學(xué)習(xí)需要循序漸進(jìn),本節(jié)主要帶大家學(xué)習(xí)Es集群Api操作,好了, 廢話不多說直接開整吧~
Es集群Api操作
在這之前,我們需要做一些準(zhǔn)備工作,就是上一節(jié)最后的部分,我們運(yùn)行好es集群
服務(wù)后,打開kibana
的控制臺(tái),打開之后,我會(huì)帶著大家一一操作。
_cat
它的作用是幫助開發(fā)者快速查詢Elasticsearch
的相關(guān)信息,比如我現(xiàn)在想要直掛的了解es
集群的狀態(tài)。
我們?cè)诳刂婆_(tái)輸入GET _cat
,點(diǎn)擊運(yùn)行按鈕,它會(huì)輸出它的全部路由,可以簡單的理解為它的所有調(diào)用方式
=^.^= /_cat/allocation /_cat/shards /_cat/shards/{index} /_cat/master /_cat/nodes /_cat/tasks /_cat/indices /_cat/indices/{index} /_cat/segments /_cat/segments/{index} /_cat/count /_cat/count/{index} /_cat/recovery /_cat/recovery/{index} /_cat/health /_cat/pending_tasks /_cat/aliases /_cat/aliases/{alias} /_cat/thread_pool /_cat/thread_pool/{thread_pools} /_cat/plugins /_cat/fielddata /_cat/fielddata/{fields} /_cat/nodeattrs /_cat/repositories /_cat/snapshots/{repository} /_cat/templates
獲取當(dāng)前集群主節(jié)點(diǎn)信息
語法:
GET /_cat/master
輸出:
_1g6ImOFQNqC1OBObqqnvw 172.19.0.3 172.19.0.3 es02
我們可以看到當(dāng)前的一個(gè)主節(jié)點(diǎn)是es02
,雖然信息是輸出了,但是看的好像不大清楚,因?yàn)闆]有標(biāo)題。下面給大家介紹一個(gè)標(biāo)題小技巧,后邊加上?v
就可以看到標(biāo)題了
GET /_cat/master?v
輸出:
id host ip node _1g6ImOFQNqC1OBObqqnvw 172.19.0.3 172.19.0.3 es02
但是有的情況下,會(huì)有大量的標(biāo)題,而我們只想看到部分信息,怎么辦?比如我指向看到ip
,這么做就可以了
GET /_cat/master?h=ip
輸出:
172.19.0.3
當(dāng)然,我們也可以連帶標(biāo)題
GET /_cat/master?h=ip&v
輸出:
ip 172.19.0.3
當(dāng)然,我們?cè)诓磺宄侄我馑嫉臅r(shí)候,也可以尋求幫助
GET /_cat/master?help
id | | node id host | h | host name ip | | ip address node | n | node name
這種方法也適用其它的_cat
查詢語句
獲取集群當(dāng)中節(jié)點(diǎn)信息
語法:
GET /_cat/nodes?v
輸出:
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name 172.19.0.3 20 73 0 0.02 0.05 0.15 dilm * es02 172.19.0.4 30 73 0 0.02 0.05 0.15 dilm - es03 172.19.0.2 27 73 0 0.02 0.05 0.15 dilm - es01
獲取集群健康信息
語法:
GET /_cat/health?v
輸出:
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 1669950213 03:03:33 es-cluster green 3 3 6 3 0 0 0 0 - 100.0%
重點(diǎn)看一下status
,輸出了一個(gè)green
,說明它是健康狀態(tài)
獲取集群索引信息
GET _cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size green open .kibana_task_manager_1 Kgl57A8fSDygmcJrnFm1pA 1 1 2 0 61.4kb 41.4kb green open .apm-agent-configuration ozJ-wxmtSaSXhRjBkpYbrQ 1 1 0 0 566b 283b green open .kibana_1 1CywDD-zSxGotJuIfPc_4w 1 1 15 6 146.1kb 66.8kb
可以看到一些默認(rèn)的索引,以及它們的健康狀態(tài),占用的磁盤大小等信息
獲取集群別名信息
GET _cat/aliases?v
alias index filter routing.index routing.search is_write_index .kibana .kibana_1 - - - - .kibana_task_manager .kibana_task_manager_1 - - - -
_cluster
用于通過restful的api形式進(jìn)行集群信息的獲取和操作,與_cat
類似,但是_cluster
是基于json形式進(jìn)行數(shù)據(jù)返回
查詢集群健康狀態(tài)
GET /_cluster/health
{ "cluster_name" : "es-cluster", "status" : "green", "timed_out" : false, "number_of_nodes" : 3, "number_of_data_nodes" : 3, "active_primary_shards" : 3, "active_shards" : 6, "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.0 }
查詢集群統(tǒng)計(jì)信息
GET /_cluster/stats
會(huì)詳細(xì)列出當(dāng)前集群總分片樹,總主分片數(shù),副分片數(shù),安裝的插件等集群所有統(tǒng)計(jì)信息,如果不明白分片
是啥也沒關(guān)系,先學(xué)會(huì)使用,后邊會(huì)給大家總結(jié)它的原理,因?yàn)檫@部分需要一些前置知識(shí),我們先上手操作。
{ "_nodes" : { "total" : 3, "successful" : 3, "failed" : 0 }, "cluster_name" : "es-cluster", "cluster_uuid" : "ardUAhdGQvKKJJxei8Hs8A", "timestamp" : 1669951414177, "status" : "green", "indices" : { "count" : 3, "shards" : { "total" : 6, "primaries" : 3, "replication" : 1.0, "index" : { "shards" : { "min" : 2, "max" : 2, "avg" : 2.0 }, "primaries" : { "min" : 1, "max" : 1, "avg" : 1.0 }, "replication" : { "min" : 1.0, "max" : 1.0, "avg" : 1.0 } } }, "docs" : { "count" : 22, "deleted" : 6 }, "store" : { "size_in_bytes" : 118135 }, "fielddata" : { "memory_size_in_bytes" : 1216, "evictions" : 0 }, "query_cache" : { "memory_size_in_bytes" : 0, "total_count" : 0, "hit_count" : 0, "miss_count" : 0, "cache_size" : 0, "cache_count" : 0, "evictions" : 0 }, "completion" : { "size_in_bytes" : 0 }, "segments" : { "count" : 11, "memory_in_bytes" : 23623, "terms_memory_in_bytes" : 16467, "stored_fields_memory_in_bytes" : 3432, "term_vectors_memory_in_bytes" : 0, "norms_memory_in_bytes" : 1152, "points_memory_in_bytes" : 0, "doc_values_memory_in_bytes" : 2572, "index_writer_memory_in_bytes" : 0, "version_map_memory_in_bytes" : 0, "fixed_bit_set_memory_in_bytes" : 528, "max_unsafe_auto_id_timestamp" : -1, "file_sizes" : { } } }, "nodes" : { "count" : { "total" : 3, "coordinating_only" : 0, "data" : 3, "ingest" : 3, "master" : 3, "ml" : 3, "voting_only" : 0 }, "versions" : [ "7.6.2" ], "os" : { "available_processors" : 48, "allocated_processors" : 48, "names" : [ { "name" : "Linux", "count" : 3 } ], "pretty_names" : [ { "pretty_name" : "CentOS Linux 7 (Core)", "count" : 3 } ], "mem" : { "total_in_bytes" : 24514228224, "free_in_bytes" : 6337855488, "used_in_bytes" : 18176372736, "free_percent" : 26, "used_percent" : 74 } }, "process" : { "cpu" : { "percent" : 0 }, "open_file_descriptors" : { "min" : 444, "max" : 458, "avg" : 449 } }, "jvm" : { "max_uptime_in_millis" : 2533693, "versions" : [ { "version" : "13.0.2", "vm_name" : "OpenJDK 64-Bit Server VM", "vm_version" : "13.0.2+8", "vm_vendor" : "AdoptOpenJDK", "bundled_jdk" : true, "using_bundled_jdk" : true, "count" : 3 } ], "mem" : { "heap_used_in_bytes" : 470693600, "heap_max_in_bytes" : 1556938752 }, "threads" : 396 }, "fs" : { "total_in_bytes" : 723860312064, "free_in_bytes" : 508481421312, "available_in_bytes" : 508481421312 }, "plugins" : [ ], "network_types" : { "transport_types" : { "security4" : 3 }, "http_types" : { "security4" : 3 } }, "discovery_types" : { "zen" : 3 }, "packaging_types" : [ { "flavor" : "default", "type" : "docker", "count" : 3 } ], "ingest" : { "number_of_pipelines" : 2, "processor_stats" : { "gsub" : { "count" : 0, "failed" : 0, "current" : 0, "time_in_millis" : 0 }, "script" : { "count" : 0, "failed" : 0, "current" : 0, "time_in_millis" : 0 } } } } }
查詢集群狀態(tài)
GET /_cluster/state
會(huì)列出當(dāng)前集群所有節(jié)點(diǎn)信息,以及所有索引的setting和mapping。同樣的,json很大,數(shù)據(jù)也都很詳細(xì)
結(jié)束語
本節(jié)主要帶大家體驗(yàn)了一下它的api操作,重點(diǎn)帶大家學(xué)習(xí)了集群
相關(guān)的api,下一節(jié)帶大家學(xué)習(xí)索引
相關(guān)的api,這個(gè)部分大家要仔細(xì)閱讀,因?yàn)槎际侵攸c(diǎn),更多關(guān)于ElasticSearch Es集群Api操作的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
實(shí)現(xiàn)了基于TCP的Java Socket編程實(shí)例代碼
這篇文章主要介紹了基于TCP的Java Socket編程實(shí)例代碼,有需要的朋友可以參考一下2013-12-12Springboot整合FreeMarker的實(shí)現(xiàn)示例
本文主要介紹了Springboot整合FreeMarker的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02idea啟動(dòng)多個(gè)服務(wù)不顯示Services或者RunDashboard窗口的處理方法
這篇文章主要介紹了idea啟動(dòng)多個(gè)服務(wù)不顯示Services或者RunDashboard窗口,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03SpringBoot 如何實(shí)現(xiàn)Session共享
這篇文章主要介紹了SpringBoot 如何實(shí)現(xiàn)Session共享,幫助大家更好的理解和學(xué)習(xí)spring boot框架,感興趣的朋友可以了解下2020-09-09Java操作XML轉(zhuǎn)JSON數(shù)據(jù)格式詳細(xì)代碼實(shí)例
在Java中我們可以使用一些現(xiàn)成的庫來實(shí)現(xiàn)XML到JSON的轉(zhuǎn)換,下面這篇文章主要給大家介紹了關(guān)于Java操作XML轉(zhuǎn)JSON數(shù)據(jù)格式的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04java實(shí)現(xiàn)Redisson的基本使用
Redisson是一個(gè)在Redis的基礎(chǔ)上實(shí)現(xiàn)的Java駐內(nèi)存數(shù)據(jù)網(wǎng)格客戶端,本文主要介紹了java實(shí)現(xiàn)Redisson的基本使用,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12