zoukankan      html  css  js  c++  java
  • es数据采集脚本样例

    采集es数据时并进行分析作用,示例脚本:

    #coding:utf-8
    
    from elasticsearch5 import Elasticsearch
    import time
    
    
    class GetElasticSearchClass(object):
        def __init__(self, url, time1):
            self.time1 = time1
            self.es = Elasticsearch([url,])
    
        def foreach(self, data):
            doc = data['hits']['hits']
            print(len(doc))
            if len(doc):
                for item in doc:
                    print(item['_source'])
    
        def search(self, index_name):
            #res = es.search(index='route*', size=10, body = {
            s_time = int(round((self.time1 - 60) * 1000000))
            e_time = int(round(self.time1 * 1000000))
            print(s_time)
            print(e_time)
            res = self.es.search(index='route*', size=1000, body = {
                            "query": {
                                "bool":{
                                    "must": [
                                      {
                                        "terms": {
                                            "日志等级": ["WARNING"]
                                        }
                                      },
                                      {
                                        "range": {
                                            "SERVER_TIMESTAMP": {
                                                "gte": s_time,
                                                "lte": e_time
                                          }
                                        }
                                      }
                                    ]
                                }
                            },
                        })
            self.foreach(res)
    
    
    if __name__== "__main__":
        t_now = int(time.time())
        tt = t_now % 60
        start_time = t_now - tt
        obj = GetElasticSearchClass('192.168.222.9:8400', start_time)
        obj.search('switch*')
    

      

    然后定时运行~~

  • 相关阅读:
    BZOJ2061 : Country
    BZOJ3591: 最长上升子序列
    BZOJ4356 : Ceoi2014 Wall
    BZOJ2159 : Crash 的文明世界
    BZOJ2149 : 拆迁队
    BZOJ2739 : 最远点
    BZOJ4068 : [Ctsc2015]app
    BZOJ4361 : isn
    BZOJ4404 : [Neerc2015]Binary vs Decimal
    BZOJ4402 : Claris的剑
  • 原文地址:https://www.cnblogs.com/fengzaoye/p/14972653.html
Copyright © 2011-2022 走看看