python 实现提取某个索引中某个时间段的数据方法

yipeiwu_com5年前Python基础

如下所示:

from elasticsearch import Elasticsearch
import datetime
import time
import dateutil.parser

class App(object):
 def __init__(self):
  pass

 def _es_conn(self):
  es = Elasticsearch()
  return es

 def get_data(self, day,start,end):
  index_ = "gather-apk-20180330"
  query_dsl = {
   "size": 10000,
   "query": {
    "bool": {
     "must": [
      {"range": {
       "receiveTime": {
        "gte": start.strftime('%Y-%m-%d %H:%M:%S'),
        "lte": end.strftime('%Y-%m-%d %H:%M:%S'),
        "format": "yyyy-MM-dd HH:mm:SS",
        "time_zone": "+08:00"
       }
      }},
      {
       "term": {
        "obd2_localnet_id": {
         "value": "101000"
        }
       }
      },
      {
       "term": {
        "obd2_substation_name": {
         "value": "石羊支局"
        }
       }
      }
     ]
    }
   },
   "_source": ["mac", "iptvAccount", "obd2_substation_name", "obd2_company_name", "obd2_grid_name",
      "receiveTime","streamBreak","kaNum"]
  }
  rs = self._es_conn().search(
   index=index_,
   body=query_dsl
  )
  

if __name__ == '__main__':
 day = datetime.datetime.now()
 # the_day = day.strftime('%Y%m%d')
 start = datetime.datetime.strptime('20180330 09:53:00','%Y%m%d %H:%M:%S')
 end = datetime.datetime.strptime('20180330 15:44:00','%Y%m%d %H:%M:%S')
 app = App()
 app.get_data(day,start,end)

以上这篇python 实现提取某个索引中某个时间段的数据方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

用Python实现KNN分类算法

用Python实现KNN分类算法

本文实例为大家分享了Python KNN分类算法的具体代码,供大家参考,具体内容如下 KNN分类算法应该算得上是机器学习中最简单的分类算法了,所谓KNN即为K-NearestNeighb...

python查找第k小元素代码分享

复制代码 代码如下:# -*- coding: utf-8 -*- from random import randintfrom math import ceil, floor def...

剖析Python的Twisted框架的核心特性

一. reactor twisted的核心是reactor,而提到reactor不可避免的是同步/异步,阻塞/非阻塞,在Dave的第一章概念性介绍中,对同步/异步的界限有点模糊,关于同步...

python实现图片筛选程序

今天因工作需要写了个小程序,用于在图片集中自动抽取需要的照片。该程序只是实现了基本功能,还有很多需要完善的地方,展示出来算是给自己鼓鼓气吧。 该程序应用有一定特殊条件,因我选择的图片集是...

Python序列化基础知识(json/pickle)

     我们把对象(变量)从内存中变成可存储的过程称之为序列化,比如XML,在Python中叫pickling,在其他语言中也被称之为seria...