python鏈接ES進行數據過濾刪除

# -*- coding: utf-8 -*-
"""
    @Time       :2020/06/04
    @Author     :Mr.Yang
    @File       :ElasticSearch_operations.py
    @Software   :PyCharm
    @Description:對ES進行查詢,按時間戳刪除操做,後續增長寫入操做
"""

import time
import datetime
import json
import sys
from elasticsearch import Elasticsearch

"""當前時間及時間戳轉換"""Time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
"""當前時間計算"""
now_time = datetime.date.today()
"""當前時間的前一個月"""
last_time = datetime.datetime.now() - datetime.timedelta(days = 30)
"""字符格式類型轉換"""
now_time = last_time.strftime("%Y-%m-%d %H:%M:%S")
"""換算時間戳爲毫秒級"""
mtime = (int(time.mktime(last_time.timetuple())) * 1000)"""ES鏈接配置"""
es_host = ['域名', '域名', '域名']#可寫入域名或IP
es = Elasticsearch(hosts=es_host,port=9200)#上一步寫IP,此處加入port

"""ES查詢修改下列參數"""
index_name = "cn_qlibobject"
id = "mUMdVHIBJYEnkK7W9DzK"

def es_query(index_name,id):
    """查詢轉換爲json格式"""
    res = es.get(index=index_name,id=id)
    print json.dumps(res, sort_keys=True, indent=4, separators=(', ', ': '))


def es_del():
    """小於時間戳的數據刪除"""
    body = {
       "query":{
           "range":{
               "updateTime": {
                   "lte": mtime
               }
           }
       }
    }
    res = es.search(index=index_name,body=body)
    #print json.dumps(res, sort_keys=True, indent=4, separators=(', ', ': '))
    es.delete_by_query(index=index_name,body=body)

#"""建立索引"""
#try:
#    result = es.indices.create(index='ceshi')
#    print result
#except  Exception as err:
#    print err
#    #es.count(index="cn_qlibobject",doc_type="_doc")
#
#"""單條數據寫入"""
#data = {"updateTime":"1583241764000"}
#res = es.index(index=index_name,doc_type="_doc",body=data)
#print res

if sys.argv[1] == "query":
    es_query(index_name, id)#
elif sys.argv[1] == "del":
    es_del()
else:
    print "請正確輸入參數(query(查詢)或del(刪除))"
相關文章
相關標籤/搜索