ELK系列(十四)、在Python中操作ES,創建/查詢/插入/更新/刪除

前面我們介紹了在kibana,curl,es-head,hive中對es的操作,本篇介紹如何使用python操作ES。

ELK系列(一)、安裝ElasticSearch+Logstash+Kibana+Filebeat-v7.7.0

ELK系列(二)、在Kibana中使用RESTful操作ES庫

ELK系列(三)、安裝Logstash插件及打包離線安裝包

ELK系列(四)、Logstash讀取nginx日誌寫入ES中

ELK系列(五)、Logstash修改@timestamp時間爲日誌的產生時間

ELK系列(六)、修改Nginx日誌爲Json格式並使用Logstash導入至ES

ELK系列(七)、Filebeat+Logstash採集多個日誌文件並寫入不同的ES索引中

ELK系列(八)、使用Filebeat+Redis+Logstash收集日誌數據

ELK系列(九)、配置ES和Kibana的用戶密碼

ELK系列(十)、ES中文分詞器IK插件安裝和配置遠程詞庫熱加載

ELK系列(十一)、ElasticSearch7.7.0插件es-head安裝及使用

ELK系列(十二)、使用SQL查詢ElasticSearch7.7.0

ELK系列(十三)、在Hive中操作ES的索引數據,創建/查詢/更新/插入

----------------------------------------在Python中操作ES----------------------------------

環境

使用pip安裝elasticsearch包:

pip install elasticsearch==7.7.0

使用

引包

這裏分別介紹使用elasticsearch包和request包查詢ES的方式: 

使用request包可以補充elasticsearch包裏不方便或者還沒有實現的功能,作爲對elasticsearch包的一個補充,建議組合使用。

from elasticsearch import Elasticsearch
import requests

ElasticSearch包

 獲取ES對象


#獲取es連接
def get_es_engine(host,port,user=None,pwd=None):
    if user and pwd:
        es = Elasticsearch(host+':'+str(port), http_auth=(user, pwd), maxsize=15)  # 有XPACK安全認證的ES集羣
    else:
        es = Elasticsearch(host+':'+str(port), maxsize=15)#無安全認證的集羣
    return es

狀態

es.ping()
es.info()

 

查詢

#查單個記錄,指定index,type,id
es.get(index='test2',id=1,doc_type='_doc')

#根據條件查詢,body裏是DSL
bd={
  "query":{
    "bool":{
      "should": [
        {
          "match_phrase_prefix":{
            "email":"yikai"
          }
        }
      ]
    }
  }
}
es.search(body=bd,index='test2')

 

#查看索引數據是否存在
es.exists(index='test2',id=1,doc_type='_doc')
Out[132]: True
es.exists(index='test2',id=2,doc_type='_doc')
Out[133]: False

更新

指定ID單條更新:

#指定ID進行更新單條記錄
data={
    "doc":{
        "age":77
    }
}
es.update(index='test2',id=3,doc_type='_doc',body=data)

根據DSL條件批量更新:

data_all={
    "query": {
        "match_all": {}  
    }, 
    "script": {
      "source": "ctx._source.age = params.age;",
      "lang": "painless",
      "params" : {
          "age": "88"
      }
    }
}
es.update_by_query(index='test2',body=data_all)

#語法參考
#https://www.elastic.co/guide/en/elasticsearch/painless/current/index.html

新增

插入一條記錄:

data_ins={
          "name" : "Rick.Wang",
          "company" : "CSDN",
          "age" : "10",
          "email" : "[email protected]"
        }

es.index(index='test2',body=data_ins,doc_type='_doc',id=8)

刪除

指定ID刪除記錄:

es.delete(index='test2',doc_type='_doc',id=8)

根據DSL條件批量刪除:

bd= {'query': {'bool': {'should': [{'match_phrase_prefix': {'email': 'yikai'}}]}}}

es.delete_by_query(index='test2',body=bd)

 

清空

清空索引不刪除索引,等同於關係型數據庫裏的truncate table:

trunc={
  "query": {"match_all": {}}
}
es.delete_by_query(index='test2',body=trunc)

使用BULK命令批量操作

批量插入

#JSON數據不能有回車換行
batch_data= [
     {"index": {}},
     {"name": "王義凱", "age": 11, "email":"[email protected]", "company":"CSDN1"},
     {"index": {}},
     {"name": "wang,yi-kai", "age": 22, "email":"[email protected]", "company":"CSDN2"},
     {"index": {}},
     {"name": "Rick.Wang", "age": 33, "email":"[email protected]", "company":"CSDN3"},
     {"index": {}},
     {"name": "義凱王", "age": 44, "email":"[email protected]", "company":"CSDN4"},
 ]
es.bulk(index='test2',doc_type='_doc',body=batch_data)

 

批量插入更新刪除

 使用bulk命令可批量對不同的索引進行插入更新刪除等操作:

#批量對不同的索引進行增刪改查操作,每個json一行
batch_action=[
    {"index": {"_index": "test2", "_type": "_doc", "_id": "999"}},
    {"name": "rick99", "age": 99, "email":"[email protected]", "company":"CSDN9" },
	{"index": {"_index": "test2", "_type": "_doc", "_id": "888"}},
    {"name": "rick88", "age": 88, "email":"[email protected]", "company":"CSDN8" },
    {"delete": {"_index": "test2", "_type": "_doc", "_id": "999"}},
    {"create": {"_index" : "test2", "_type" : "_doc", "_id": "000"}},
    {"name": "rick00", "age": 100, "email":"[email protected]", "company":"CSDN0" },
    {"update": {"_index": "test2", "_type": "_doc", "_id": "888"}},
    {"doc": {"age": "888"}}
 ]
es.bulk(index='test2',doc_type='_doc',body=batch_action)

使用bulk批量操作的時候,對於不同的操作類型,一定要在前面加上與之對應的操作頭信息({“index”: {}}, {‘delete’: {…}}, …),否則會報TransportError(400, u’illegal_argument_exception’)的錯誤。  

Request包

前面介紹過ES支持Restful接口,我們可以使用curl命令對其進行操作,同樣我們也可以使用python裏的request包訪問操作ES庫。

GET查詢

使用get函數查詢ES數據:

import requests
es_http = 'http://localhost:9200'
index='test2'
type='_doc'
id='888'
auth=('elastic','r12345635x')  #tuple格式的賬號密碼,如果沒有開啓xpack安全認證可忽略此參數

#查詢指定的id數據
res=requests.get(es_http+'/'+index+'/'+type+'/'+id,auth=auth) #如果沒有安全認證則不需要auth參數
res.text

#查詢該索引下所有數據
res=requests.get(es_http+'/'+index+'/_search',auth=auth)
res.text

#使用DSL查詢數據
bd={
  "query":{
    "bool":{
      "should": [
        {
          "match_phrase_prefix":{
            "name":"rick.wang"
          }
        }
      ]
    }
  }
}
res=requests.get(es_http+'/'+index+'/_search/?pretty',auth=auth,json=bd)#pretty是爲了格式化json樣式,看起來更好看,可以忽略
print(res.text)

POST

使用POST方法可以與在Kibana中進行一樣的操作,比如插入一條記錄,比如根據DSL批量更新: 

#使用POST方法往ES中插入數據
data={"name": "rick999", "age": 999, "email":"[email protected]", "company":"CSDN999" }
res = requests.post(es_http+'/'+index+'/_doc/999',auth=auth,json=data)
res.text

res = requests.get(es_http+'/'+index+'/_doc/999',auth=auth)
res.text


#使用POST方法根據DSL對ES進行操作
bd={
    "query": {
        "match_all": {}  
    }, 
    "script": {
      "source": "ctx._source.age = params.age;",
      "lang": "painless",
      "params" : {
          "age": "99"
      }
    }
}
res = requests.post(es_http+'/'+index+'/_update_by_query',auth=auth,json=bd)
res.text

PUT

使用PUT可以創建索引

res = requests.put(es_http+'/'+'new_index',auth=auth)
res.text

DELETE

使用DELETE方法可以刪除索引 刪除數據等

#往新創建的索引裏插入一條記錄
data={"name": "rick999", "age": 999, "email":"[email protected]", "company":"CSDN999" }
requests.post(es_http+'/'+'new_index'+'/_doc/999',auth=auth,json=data) 
#判斷ID爲999的是否存在
es.exists(index='new_index',id=999,doc_type='_doc')

#使用DELETE方法刪除ID爲999的記錄
requests.delete(es_http+'/'+'new_index'+'/_doc/999',auth=auth)
#判斷ID爲999的是否存在
es.exists(index='new_index',id=999,doc_type='_doc')

#使用DELETE方法刪除new_index的索引
res=requests.delete(es_http+'/'+'new_index',auth=auth) #刪除索引
res.text

 

希望本文對你有幫助,請點個贊鼓勵一下作者吧~ 謝謝!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章