常规操作elasticSearch查看和索引(存储)数据
常規(guī)操作elasticSearch:
對(duì)于elasticSearch的操作 通常用rest API完成
查看所有節(jié)點(diǎn):
GET: 192.168.31.125:9200/_cat/nodes示例返回:
127.0.0.1 16 97 10 0.23 0.56 0.62 dilm * 6a850788e223查看健康狀態(tài):
GET: 192.168.31.125:9200/_cat/health示例返回:
1635298278 01:31:18 elasticsearch green 1 1 4 4 0 0 0 0 - 100.0%查看主節(jié)點(diǎn):
GET: 192.168.31.125:9200/_cat/master示例返回:
LxbmZJweRySmiwKYs4eAHg 127.0.0.1 127.0.0.1 6a850788e223查看所有索引(類比mysql查看所數(shù)據(jù)庫(kù)):
GET: 192.168.31.125:9200/_cat/indices示例返回:
green open .kibana_task_manager_1 CS2sQjxhTmqK_iwwyI0X4Q 1 0 2 0 31.2kb 31.2kb green open kibana_sample_data_ecommerce mPzvtBuMQ3KmIWZs9mGlyQ 1 0 4675 0 4.7mb 4.7mb green open .apm-agent-configuration HNEJe3GNSBipQlb-3_Ltow 1 0 0 0 283b 283b green open .kibana_1 hIKbqbavQhKjvhM9znRc9A 1 0 54 1 943.2kb 943.2kb yellow open customer -EYnoz-SQSyFAuXWL_4J5w 1 1 1 0 3.3kb 3.3kb保存一條數(shù)據(jù) put 保存:
注意:
put保存必須有id(唯一識(shí)別)
PUT: 192.168.31.125:9200/索引/類型/唯一識(shí)別 192.168.31.125:9200/customer/external/1 json參數(shù): {"name":"zxl"}示例返回:
成功示例: {"_index": "customer","_type": "external","_id": "1","_version": 5,"result": "updated","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 9,"_primary_term": 1 } 不帶唯一識(shí)別失敗: {"error": "Incorrect HTTP method for uri [/customer/external/] and method [PUT], allowed: [POST]","status": 405 }保存一條數(shù)據(jù) POST保存:
注意:
不帶id(唯一識(shí)別) 新增(唯一識(shí)別自動(dòng)生成)
帶id(唯一識(shí)別) id已存在 更新
帶id(唯一識(shí)別) id已存在 新增
POST: 192.168.31.125:9200/索引/類型/唯一識(shí)別 192.168.31.125:9200/customer/external/2 json參數(shù): {"name":"sss"}示例返回:
成功示例: {"_index": "customer","_type": "external","_id": "2","_version": 4,"result": "updated","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 12,"_primary_term": 1 } 不帶唯一識(shí)別成功實(shí)例: {"_index": "customer","_type": "external","_id": "y7atv3wBC3_10cmr4V9N","_version": 1,"result": "created","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 13,"_primary_term": 1 }查看數(shù)據(jù):
GET: 192.168.31.125:9200/索引/類型/唯一識(shí)別 192.168.31.125:9200/customer/external/1示例返回:
成功示例: {"_index": "customer","_type": "external","_id": "1","_version": 7,"_seq_no": 11,"_primary_term": 1,"found": true,"_source": {"name": "ooo"} }_seq_no 和 _primary_term 常用語(yǔ)存儲(chǔ)數(shù)據(jù)并發(fā)時(shí)樂(lè)觀鎖操作 防止同時(shí)請(qǐng)求被多次修改
***樂(lè)觀鎖保存示例:
PUT: 192.168.31.125:9200/customer/external/1?if_seq_no=11&if_primary_term=1 json參數(shù): {"name":"yyds"}示例返回:
第一次操作條件符合成功: {"_index": "customer","_type": "external","_id": "1","_version": 8,"result": "updated","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 14,"_primary_term": 1 } 繼續(xù)點(diǎn)擊提交 _seq_no發(fā)生變化 保存失敗: {"error": {"root_cause": [{"type": "version_conflict_engine_exception","reason": "[1]: version conflict, required seqNo [11], primary term [1]. current document has seqNo [14] and primary term [1]","index_uuid": "-EYnoz-SQSyFAuXWL_4J5w","shard": "0","index": "customer"}],"type": "version_conflict_engine_exception","reason": "[1]: version conflict, required seqNo [11], primary term [1]. current document has seqNo [14] and primary term [1]","index_uuid": "-EYnoz-SQSyFAuXWL_4J5w","shard": "0","index": "customer"},"status": 409 }***POST的_update更新數(shù)據(jù)更新文檔示例:
POST: 192.168.31.125:9200/customer/external/1/_update json參數(shù): {"name":"yyds"}POST更新文檔指定 索引/類型/唯一識(shí)別/_update
json參數(shù)應(yīng)該為:{“doc”:{“name”:“333”}}
數(shù)據(jù)參數(shù)項(xiàng)必須在json對(duì)象的doc內(nèi)。
數(shù)據(jù)會(huì)對(duì)比原數(shù)據(jù),如果數(shù)據(jù)相同, 數(shù)據(jù)不會(huì)有操作,并且返回result:noop。此時(shí)操作:版本號(hào)_version 操作序列號(hào)_seq_no 不變。
示例返回:
第一次操作條件符合成功:繼續(xù)點(diǎn)擊提交 保存: {"_index": "customer","_type": "external","_id": "1","_version": 11,"result": "updated","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 17,"_primary_term": 1 } 繼續(xù)提交: {"_index": "customer","_type": "external","_id": "1","_version": 11,"result": "noop","_shards": {"total": 0,"successful": 0,"failed": 0},"_seq_no": 17,"_primary_term": 1 }只有POST帶有_update參數(shù),更新數(shù)據(jù) 數(shù)據(jù)才會(huì)對(duì)比原數(shù)據(jù)。
put和post不帶有_udpate參數(shù),都是更新數(shù)據(jù)。
如果增加唯一識(shí)別下數(shù)據(jù)的其他屬性,直接帶上添加保存即可。增加新屬性 ,數(shù)據(jù)肯定變化,post帶有udpate也是更新哦!
刪除文檔
DELETE請(qǐng)求: 192.168.31.125:9200/customer/external/1/示例返回:
成功實(shí)例: {"_index": "customer","_type": "external","_id": "1","_version": 14,"result": "deleted","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 20,"_primary_term": 1 } 繼續(xù)提交 刪除不存在的主鍵文檔: {"_index": "customer","_type": "external","_id": "1","_version": 15,"result": "not_found","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 21,"_primary_term": 1 }刪除文檔后查找數(shù)據(jù)
get: 192.168.31.125:9200/customer/external/1/ 實(shí)例: {"_index": "customer","_type": "external","_id": "1","found": false }刪除是可以針對(duì)索引 文檔。沒(méi)有直接刪除類型的操作
刪除索引
DELETE請(qǐng)求: 192.168.31.125:9200/customer/示例返回:
成功實(shí)例: {"acknowledged": true }刪除索引后查找數(shù)據(jù)
get: 192.168.31.125:9200/customer/external/1/ 實(shí)例: {"error": {"root_cause": [{"type": "index_not_found_exception","reason": "no such index [customer]","resource.type": "index_expression","resource.id": "customer","index_uuid": "_na_","index": "customer"}],"type": "index_not_found_exception","reason": "no such index [customer]","resource.type": "index_expression","resource.id": "customer","index_uuid": "_na_","index": "customer"},"status": 404 }批量導(dǎo)入數(shù)據(jù):
POST: 192.168.31.125:9200/customer/external/_bulkjson參數(shù)語(yǔ)法:
{"index":{"_id":"1"}} 文檔1的主鍵 {"name":"zhao"} 文檔1的內(nèi)容 {"index":{"_id":"2"}} 文檔2的主鍵 {"name":"qian"} 文檔2的主鍵復(fù)雜批量實(shí)例:
POST /_bulk { "delete": { "_index": "website", "_type": "blog", "_id": "123" }} { "create": { "_index": "website", "_type": "blog", "_id": "123" }} { "title": "My first blog post" } { "index": { "_index": "website", "_type": "blog" }} { "title": "My second blog post" } { "update": { "_index": "website", "_type": "blog", "_id": "123"}} { "doc":{"title":"My updated blog post"} }POST /_bulk
{ “delete”: { “_index”: “website”, “_type”: “blog”, “_id”: “123” }} 刪除
{ “create”: { “_index”: “website”, “_type”: “blog”, “_id”: “123” }}創(chuàng)建
{ “title”: “My first blog post” } 創(chuàng)建的內(nèi)容
{ “index”: { “_index”: “website”, “_type”: “blog” }} 插入
{ “title”: “My second blog post” } 插入的內(nèi)容
{ “update”: { “_index”: “website”, “_type”: “blog”, “_id”: “123”}} 更新
{ “doc”:{“title”:“My updated blog post”} } 更新的內(nèi)容
總結(jié)
以上是生活随笔為你收集整理的常规操作elasticSearch查看和索引(存储)数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: NR的SSB子载波间隔讨论——为何无60
- 下一篇: 泊松分布以及相关分布的知识整理