批量写数据到ES

 

#!/usr/bin/env python
#coding=utf-8

from elasticsearch import Elasticsearch
import time
import random
from elasticsearch import helpers


##es = Elasticsearch('http://192.168.1.135:9200')
es = Elasticsearch(hosts=["http://elastic:elastic@192.168.1.134:9200/"])
def test_insert():
    actions = []
    for i in range(1, 10):
        str_i = str(i)
        rnd = random.randint(1, 100)
        now_time=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        now_day=time.strftime('%Y-%m-%d', time.localtime(time.time()))

        app = 'digitalclinic-hospital-web '
        blocked_qps = 0
        count = 1
        exception_qps = 0
        gmt_create=now_time
        gmt_modified=now_time
        id=i
        machine_ip="127.0.0.1"
        pass_qps = i * rnd
        resource = 'InformTagService'
        resource_code=1443828042
        rt = 100
        success_qps = 200
        timestamp=now_time
        month=now_day

        action = {
                  "_index": "metric_pl",
                  ##"_type": "metric_log", ##7 版本之后不需要指定_type
                  "app": app,
                  "blocked_qps": blocked_qps,
                  "count": count,
                  "exception_qps": exception_qps,
                  "gmt_create": gmt_create,
                  "gmt_modified": gmt_modified,
                  "id": id,
                  "machine_ip": machine_ip,
                  "pass_qps": pass_qps,
                  "resource": resource,
                  "resource_code": resource_code,
                  "rt": rt,
                  "success_qps": success_qps,
                  "timestamp": timestamp,
                  "month": month
                  }
        i=i+1
        actions.append(action)
        if (len(actions) == 1000):
            helpers.bulk(es, actions)
            actions = []

    if (len(actions) > 0):
        helpers.bulk(es, actions)

if __name__ == '__main__':
    print("开始时间:"+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
    l_flag = test_insert()
    print("结束时间:"+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

 

posted @ 2023-06-29 17:39  slnngk  阅读(59)  评论(0)    收藏  举报