当前位置:网站首页>es数据导出csv文件
es数据导出csv文件
2022-06-28 07:36:00 【有勇气的牛排】
1 介绍
es数据导出到csv文件,暂时不考虑效率问题,仅谈实现方式。
2 python3
def connect_elk():
client = Elasticsearch(hosts='http://192.168.56.20:9200',
http_auth=("elastic", "elastic密码"),
# 在做任何操作之前,先进行嗅探
# sniff_on_start=True,
# 节点没有响应时,进行刷新,重新连接
sniff_on_connection_fail=True,
# 每 60 秒刷新一次
sniffer_timeout=60
)
return client
from elasticsearch import Elasticsearch
import csv
# 获取es数据库
from common.util_es import connect_elk
es = connect_elk()
''' 查询所有数据并导出 '''
index = 'blog_rate'
body = {
}
item = ["r_id", "a_id"]
# body = {
# "query": {
# "match": {"name": "张三"},
# }
# }
def ExportCsv(index, body,item):
query = es.search(index=index, body=body, scroll='5m', size=1000)
# es查询出的结果第一页
results = query['hits']['hits']
# es查询出的结果总量
total = query['hits']['total']["value"]
# 游标用于输出es查询出的所有结果
scroll_id = query['_scroll_id']
for i in range(0, int(total / 100) + 1):
# scroll参数必须指定否则会报错
query_scroll = es.scroll(scroll_id=scroll_id, scroll='5m')['hits']['hits']
results += query_scroll
with open('./' + index + '.csv', 'w', newline='', encoding="utf_8_sig") as flow:
csv_writer = csv.writer(flow)
for res in results:
csvrow1 = []
for i in item:
csvrow1.append(res["_source"][i])
csv_writer.writerow(csvrow1)
print('done!')
参考地址:
https://blog.csdn.net/github_27244019/article/details/115351640
边栏推荐
- Leetcode learning records
- Sentinel mechanism of redis cluster
- es6箭头函数中return的用法
- 7-1 understand everything
- kubelet垃圾(退出的容器和未使用的镜像)回收源码分析
- R language ggmap
- Modifying MySQL user name root under Linux
- 逆波兰表达式求值<难度系数>
- Safety training is the greatest benefit for employees! 2022 induction safety training for new employees
- Kubelet garbage collection (exiting containers and unused images) source code analysis
猜你喜欢
Cloud native (to be updated)
推荐系统系列精讲(第五讲): 排序模型的调优实践
GoLand IDE and delve debug Go programs in kubernetes cluster
Source code analysis of kubernetes' process of deleting pod
数字藏品市场“三大套路”
Mysql57 zip file installation
What should I do if the version is incompatible with the jar package conflict?
es6箭头函数中return的用法
"Three routines" of digital collection market
Unity UI shadow component
随机推荐
MMR rearrangement (similarity is calculated by editing distance and repeatability)
What is a consistent hash? What scenarios can it be applied to?
PLC -- Notes
R language drawing ggplot2 seasonal graph
R and RGL draw 3D knots
MySQL installation steps - how to create a virtual machine under Linux (1)
异或的应用。(提取出数字中最右侧的1,面试中经常用的到)
Cloud native (to be updated)
kubelet垃圾(退出的容器和未使用的镜像)回收源码分析
Kubernetes deploys a secret pit where thanos ruler sends repeated alarms
打新债注册开户靠谱吗?安全吗?
Leetcode+ 66 - 70 high precision, two sub topics
Static resource compression reduces bandwidth pressure and increases access speed
Practice of traffic recording and playback in vivo
R language ggmap visual cluster
Makefile
7-2 Finnish wooden chess structure Sorting
Section VI UART of zynq
Sword finger offer II 091 Paint the house
股票炒股注册开户靠谱吗?安全吗?