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

时间:2022-11-26 20:04:31

如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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 实现提取某个索引中某个时间段的数据方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/sxf_123456/article/details/80297860