1. 程式人生 > >ELK搭建步驟以及使用API的基本操作

ELK搭建步驟以及使用API的基本操作

ELK=elasticsearch+Logstash+kibana  elasticsearch:後臺分散式儲存以及全文檢索  logstash: 日誌加工、“搬運工”  kibana:資料視覺化展示。  ELK架構為資料分散式儲存、視覺化查詢和日誌解析建立了一個功能強大的管理鏈。 三者相互配合,取長補短,共同完成分散式大資料處理工作。 2. ES特點和優勢     1)分散式實時檔案儲存,可將每一個欄位存入索引,使其可以被檢索到。      2)實時分析的分散式搜尋引擎。          分散式:索引分拆成多個分片,每個分片可有零個或多個副本。叢集中的每個資料節點都可承載一個或多個分片,並且協調和處理各種操作;          負載再平衡和路由在大多數情況下自動完成。      3)可以擴充套件到上百臺伺服器,處理PB級別的結構化或非結構化資料。也可以執行在單臺PC上(已測試)      4)支援外掛機制,分詞外掛、同步外掛、Hadoop外掛、視覺化外掛等。 ---------------------  ES科普教程:https://blog.csdn.net/laoyang360/article/details/52244917 為什麼要用ES:https://www.quora.com/Why-do-people-use-Hadoop-or-Spark-when-there-is-ElasticSearch curl -X PUT "localhost:9200/customer?pretty"       建立一個index  

curl -X GET "localhost:9200/_cat/indices?v"            檢視index的狀態資訊

curl -X PUT "localhost:9200/customer/doc/2?pretty" -H 'Content-Type: application/json' -d' { "name": "Leo" } '                                                    在index裡插入一些json資料

curl -X DELETE "localhost:9200/customer?pretty"       刪除一個index(此處customer為一個index名) -------- <REST Verb> /<Index>/<Type>/<ID>                      REST訪問模式

curl -X POST "localhost:9200/customer/doc/2?pretty" -H 'Content-Type: application/json' -d' {   "name": "Jane Doe"   } '                                           修改index為customer,type為doc下,id為2的json,name改為“Jane Doe”

curl -X POST "localhost:9200/customer/doc/1/_update?pretty" -H 'Content-Type: application/json' -d' {   "doc": { "name": "Jane Doe", "age": 20 } } '                                             更新操作 添加了屬性age 

curl -X POST "localhost:9200/customer/doc/1/_update?pretty" -H 'Content-Type: application/json' -d' {   "script" : "ctx._source.age += 5" } '                                             更新操作 更新了age的值

curl -X DELETE "localhost:9200/customer/doc/2?pretty"     刪除id為2的所有json

curl -X POST "localhost:9200/customer/doc/_bulk?pretty" -H 'Content-Type: application/json' -d' {"index":{"_id":"1"}} {"name": "John Doe" } {"index":{"_id":"2"}} {"name": "Jane Doe" } ' curl -X POST "localhost:9200/customer/doc/_bulk?pretty" -H 'Content-Type: application/json' -d' {"update":{"_id":"1"}} {"doc": { "name": "John Doe becomes Jane Doe" } } {"delete":{"_id":"2"}} ' 以上為呼叫了_bulk API 實現批處理,可以同時實現多種操作

curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/account/_bulk?pretty&refresh' --data-binary "@accounts.json"                         批量處理:匯入一個json檔案,前提是1.建立一個名為bank的index;2.進入該json檔案的目錄                         

search API: 呼叫search API的返回值及其含義: took – time in milliseconds for Elasticsearch to execute the search timed_out – tells us if the search timed out or not _shards – tells us how many shards were searched, as well as a count of the successful/failed searched shards hits – search results hits.total – total number of documents matching our search criteria hits.hits – actual array of search results (defaults to first 10 documents) hits.sort - sort key for results (missing if sorting by score) hits._score and max_score - The score is a numeric value that is a relative measure of how well the document                              matches the search query that we specified. The higher the score, the more                              relevant the document is, the lower the score, the less relevant the document is.

curl -X GET "localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty"   按照account_number升序排列 curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' {   "query": { "match_all": {} },   "sort": [     { "account_number": "asc" }   ] } '                        與上一條語句執行結果相同

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' {   "query": { "match_all": {} },                #查詢範圍:整個文件   "from": 10,                                #查詢開始的索引:10(預設值為0)   "size": 10                                #返回文件數量10  (預設值為10) } '                        

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' {   "query": { "match_all": {} },   "sort": { "balance": { "order": "desc" } }     按照balance的降序排序,預設輸出10個文件 } '

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' {   "query": { "match_all": {} },              查詢所有   "_source": ["account_number", "balance"]   只返回account_number和balance欄位 } '

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' {   "query": { "match": { "address": "mill" } }   查詢address欄位中包含mill的所有賬戶 } '

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' {   "query": { "match": { "address": "mill lane" } }   查詢address欄位中包含mill  或者包含 lane 的賬戶 } '

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' {   "query": { "match_phrase": { "address": "mill lane" } }   ***注意與上一條的區別: 此處只返回同時包含 mill lane的資訊 } ' curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' {   "query": {     "bool": {         表示布林邏輯查詢,       "must": [       must表示必須滿足所有的語句(與關係),即:同時包含mill和lane     可以為should  表示或關係, 也可以為must_not 表示非關係         { "match": { "address": "mill" } },                         { "match": { "address": "lane" } }       ]     }   } } ' curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' {   "query": {     "bool": {       "must": [         { "match": { "age": "40" } }       返回包含age=40,同時不包含state:ID       ],       "must_not": [         { "match": { "state": "ID" } }       ]     }   } } ' curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' {   "query": {     "bool": {       "must": { "match_all": {} },       "filter": {           制定查詢範圍         "range": {          range查詢,返回的是一個範圍內的資料           "balance": {       指定balance欄位             "gte": 20000,    balance最小為20000             "lte": 30000            最大為30000 範圍內的所有資料           }         }       }     }   } } ' curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' {   "size": 0,              size設為0,表示只顯示聚合情況,不顯示具體資訊   "aggs": {               聚合查詢     "group_by_state": {   根據state欄位分組       "terms": {         "field": "state.keyword"       根據state的關鍵字聚合         }     }   } } '

curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d' {   "size": 0,   "aggs": {     "group_by_state": {       "terms": {         "field": "state.keyword"            },       "aggs": {         "average_balance": {           聚合結果返回每一個聚合的平均賬戶餘額(聚合巢狀)           "avg": {             "field": "balance"           }         }       }     }   } } '

以下語句,返回這個示例演示瞭如何按年齡等級(20-29歲、30-39歲和40-49歲)分組,然後按性別分組, 最後得到平均賬戶餘額、每個年齡等級、每個性別 curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d' {   "size": 0,   "aggs": {     "group_by_age": {       "range": {         "field": "age",         "ranges": [           {             "from": 20,             "to": 30           },           {             "from": 30,             "to": 40           },           {             "from": 40,             "to": 50           }         ]       },       "aggs": {         "group_by_gender": {           "terms": {             "field": "gender.keyword"           },           "aggs": {             "average_balance": {               "avg": {                 "field": "balance"               }             }           }         }       }     }   } } '

./bin/elasticsearch -d -p pid 將ES作為後臺守護程序啟動,並將日誌檔案儲存在ES下的logs裡 kill 'cat pid'    關閉程序

2018.10.29 1. 關閉防火牆的命令:  systemctl stop(distable/status) firewalld.service 2018.10.30 elasticsearch配置問題 1.解壓包: tar -zxvf 檔名 2.切換到root使用者下,改變最大開啟檔案數量的限制   ①su 切換到root使用者   ②ulimit -n 65536   ③切換到啟動ES的普通使用者 啟動ES   或者直接改文件:修改 /etc/security/limit.conf   啟動ES的使用者名稱 - nofile 65536   ④啟動ES 檢視設定是否生效  curl -X GET "localhost:9200/_nodes/stats/process?filter_path=**.max_file_descriptors"     若返回的是65536 則生效 3.設定JVM.option   ①設定最小堆和最大堆大小一樣   ②最大堆不超過物理RAM的50%, 4.減少記憶體交換(記憶體交換堆效能和節點穩定性影響較大)   ①註釋掉/etc/fstab 檔案裡所有包含swap單詞的行   ②將vm.swappiness設定為1 ,  sysctl vm.swappiness=1 5.將ES的程序鎖定在RAM裡,禁止記憶體交換   修改elasticsearch.yml    bootstrap.memory_lock: true (注意true前面有空格)   啟動es檢視設定是否成功 curl -X GET "localhost:9200/_nodes?filter_path=**.mlockall"  如果為true,即為成功 6. 修改mmap數量     (MMap:FS型別通過將檔案對映到記憶體(MMap),在檔案系統(對映到Lucene MMapDirectory)上儲存碎片索引。     記憶體對映佔用程序中虛擬記憶體地址空間的一部分,這部分空間與被對映檔案的大小相等。在使用這個類之前,     請確保有足夠的虛擬地址空間。)    ①修改檔案/etc/sysctl.conf  vm.max_map_count=262144    ②重啟之後檢視 sysctl vm.max_map_count 2018.10.31  1.x-pack 的安裝與解除安裝     1.1    安裝: https://segmentfault.com/a/1190000012789290     1.2    解除安裝: ./elasticsearch-plugin remove x-pack  然後 刪除elasticsearch.yml中有關x-pack的配置資訊 2.升級es版本,並安裝elasticsearch-head和Kibana     https://www.jianshu.com/p/d53ef9aa8416    3.安裝kibana      3.1    安裝步驟:①找到對應你的es版本的kibana                   ②wget https://artifacts.elastic.co/downloads/kibana/kibana-6.0.0-linux-x86_64.tar.gz  (注意修改版本號)                   ③tar -xzf kibana-6.0.0-linux-x86_64.tar.gz     3.2    配置kibana.yml 主要是 elasticsearch.url: "http://主機ip:9200"    剩下的可以預設,把配置前的#號去掉就行(之後又需求再進行修改)     3.3 啟動kibana時,Discover 頁預設會載入預設的索引模式。時間過濾器設定的時間為過去15分鐘,查詢設定為匹配所有 (\*) 。         如果看不到任何文件,試著把時間過濾器的範圍調大。如果還是看不到任何結果,很可能是根本就 沒有 任何文件。           所以,如果沒有任何文件,那麼第一次載入時間會很長 4.安裝head  注: 5.x之後的版本不支援head外掛的安裝   只能下載好之後單獨安裝 2018.11.1 1.kibana的使用     1.1 匯入資料前,先建立一個索引,對索引建立對映,在建立對映時,要輸入欄位型別,es版本不一樣,欄位型別也不一樣,比如:ES6.x的字串型別為text而不是string         (具體詳見https://blog.csdn.net/u014516601/article/details/82885392)         以bank資料集為例:         ①建立索引  curl -X PUT "localhost:9200/customer?pretty"         ②建立型別(一個索引下可以包含多個型別,如果兩個型別的欄位集是互不相同的,這就意味著索引中將有一半的資料是空的(欄位將是 稀疏的 ),                     最終將導致效能問題。在這種情況下,最好是使用兩個單獨的索引。一個索引下的不同型別之間的資料模式最好是相同或類似的)         ③建立對映(構建對映之前要先了解資料的格式,有哪些欄位)           注:此處可能報錯:1> Content-Type header [application/x-www-form-urlencoded] is not supported   原因是es6.0之後的建立對映的語句堪憂改變                           2> mapping type is missing; in elasticsearch   原因指定型別, 一個索引下可以建立多個型別(型別名稱自定義,我理解的是型別RDBMS裡的表名),一個型別下又可以建立多個欄位,每個欄位都需要指定資料型別                                                                                                  curl -X POST "192.168.1.240:9200/bank/product/_mapping" -H "Content-Type: application/json" -d '    # 索引:bank  型別: product 欄位:account_number,balance等等             {                 "properties" : {                 "account_number": {"type":"integer"},                 "balance": {"type":"integer"},                 "firstname": {"type":"text"},                 "lastname": {"type":"text"},                 "age": {"type":"integer"},                 "gender": {"type":"text"},                 "address": {"type":"text"},                 "employer": {"type":"text"},                 "email": {"type":"text"},                 "city": {"type":"text"},                 "state": {"type":"text"}                }             }'     1.2 具體對於kibana的圖示操作 詳見:https://blog.csdn.net/ming_311/article/details/50619859     1.3 kibana後臺執行的命令 nohup ./kibana (輸入回車之後稍微等一會,有點慢) 2.Java和Elasticsearch的相互操作 2018.11.3 1.ik中文分詞器的安裝與使用     1.1 匹配版本         使用./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.3.0/elasticsearch-analysis-ik-6.3.0.zip (注意改版本號)         安裝完成重新啟動elasticsearch     1.2 新建索引和型別,     curl -XPOST http://localhost:9200/index/fulltext/_mapping -H 'Content-Type:application/json' -d'     {         "properties": {             "content": {                 "type": "text",                 "analyzer": "ik_max_word",                 "search_analyzer": "ik_max_word"   #細粒度分詞,ik_smart:粗粒度分詞             }         }

    }'