当前位置:网站首页>Es data export CSV file
Es data export CSV file
2022-06-28 07:37:00 【Courageous steak】
1 Introduce
es Export data to csv file , For the time being, we will not consider efficiency , Just talk about the implementation method .
2 python3
def connect_elk():
client = Elasticsearch(hosts='http://192.168.56.20:9200',
http_auth=("elastic", "elastic password "),
# Before doing anything , Sniff first
# sniff_on_start=True,
# When the node does not respond , refresh , Reconnect the
sniff_on_connection_fail=True,
# Every time 60 Refresh every second
sniffer_timeout=60
)
return client
from elasticsearch import Elasticsearch
import csv
# obtain es database
from common.util_es import connect_elk
es = connect_elk()
''' Query all data and export '''
index = 'blog_rate'
body = {
}
item = ["r_id", "a_id"]
# body = {
# "query": {
# "match": {"name": " Zhang San "},
# }
# }
def ExportCsv(index, body,item):
query = es.search(index=index, body=body, scroll='5m', size=1000)
# es The first page of the query results
results = query['hits']['hits']
# es The total number of results found
total = query['hits']['total']["value"]
# Cursor for output es All the results of the query
scroll_id = query['_scroll_id']
for i in range(0, int(total / 100) + 1):
# scroll Parameter must be specified or an error will be reported
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!')
Reference address :
https://blog.csdn.net/github_27244019/article/details/115351640
边栏推荐
- Practice of traffic recording and playback in vivo
- Open62541 import nodeset file directly
- R 语言绘制 动画气泡图
- Hash slot of rediscluster cluster cluster implementation principle
- 数字藏品市场“三大套路”
- DBeaver 22.1.1 发布,可视化数据库管理平台
- ice, protobuf ,thrift -- 笔记
- HJ质数因子
- R language ggmap visual cluster
- Reading notes - MySQL technology l act: InnoDB storage engine (version 2)
猜你喜欢

PLC -- 笔记

Static resource compression reduces bandwidth pressure and increases access speed

数字藏品市场“三大套路”

No suspense about the No. 1 Internet company overtime table
![[ thanos源码分析系列 ]thanos query组件源码简析](/img/e4/2a87ef0d5cee0cc1c1e1b91b6fd4af.png)
[ thanos源码分析系列 ]thanos query组件源码简析

扩展Prometheus的解决方案thanos的简介和几个月使用心得

Tencent continued to lay off staff in the second half of the year, and all business groups reduced by at least 10%. What do you think of this? Followers

云原生:云计算技术再次升级 开启全面云开发时代

Source code analysis of kubernetes' process of deleting pod

kubelet驱逐机制的源码分析
随机推荐
股票炒股注册开户靠谱吗?安全吗?
8 张图 | 剖析 Eureka 的首次同步注册表
Sword finger offer II 091 Paint the house
Reading notes - MySQL technology l act: InnoDB storage engine (version 2)
vite2.9 中指定路径别名
Devtools implementation principle and performance analysis practice
The practice of traffic and data isolation in vivo Reviews
HJ21 简单密码
异或的应用。(提取出数字中最右侧的1,面试中经常用的到)
腾讯下半年继续裁员,所有事业群至少缩减 10%,对此你怎么看?关注者
Leetcode+ 66 - 70 high precision, two sub topics
Kubernetes deploys a secret pit where thanos ruler sends repeated alarms
Top 25 most popular articles on vivo Internet technology in 2021
LLVM 与 Clang
mysql57 zip文件安装
goland IDE和delve调试位于kubernetes集群中的go程序
The practice of event driven architecture in vivo content platform
QT -- communication protocol
Makefile
Section VI UART of zynq