批量写数据到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())))
分类:
elasticsearch
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?