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进行简单图像识别(验证码)

用Python进行简单图像识别(验证码)

这是一个最简单的图像识别,将图片加载后直接利用Python的一个识别引擎进行识别 将图片中的数字通过 pytesseract.image_to_string(image)识别后将结果存入...

Python3 中作为一等对象的函数解析

Python3 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。 函数能提高应用的模块性,和代码的重复利用率。你已经知道Python提供了许多内建函数,比如pr...

python实现文件助手中查看微信撤回消息

利用python实现防撤回,对方撤回的消息可在自己的微信文件传输助手中查看。 如果想变成可执行文件放在电脑中运行,可用pyinstaller将此程序打包成exe文件。 pyinsta...

解决Python requests库编码 socks5代理的问题

解决Python requests库编码 socks5代理的问题

编码问题 response = requests.get(URL, params=params, headers=headers, timeout=10) print '...

简单实现python聊天程序

本文实例为大家分享了简单实现python聊天程序的具体代码,供大家参考,具体内容如下 客户端 #coding:utf-8 import socket, sys host = 'lo...