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

yipeiwu_com6年前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/ArcPy遍历指定目录中的MDB文件方法

如下所示: #遍历指定目录中的MDB文件,构造FeatureClass名 >>> target_folder = 'D:\T20161202' ... file_...

Python 读写文件和file对象的方法(推荐)

1.open 使用open打开文件后一定要记得调用文件对象的close()方法。比如可以用try/finally语句来确保最后能关闭文件。 file_object = open('the...

Python字符串大小写转换拼接删除空白

1.字符串大小写转换 string.title() #将字符串中所有单词的首字母以大写形式显示 string.upper() #将字符串中所有字母转化为大写字母 stri...

python3 中文乱码与默认编码格式设定方法

python默认编码格式是utf-8。在python2.7中,可以通过sys.setdefaultencoding('gbk')设定默认编码格式,而在python3.3中sys.setd...

Python自定义一个类实现字典dict功能的方法

如下所示: import collections class Mydict(collections.UserDict): def __missing__(self, ke...