当前位置:网站首页>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
边栏推荐
猜你喜欢

Rediscluster cluster mode capacity expansion node

Recommended system series (Lecture 5): Optimization Practice of sorting model

kubelet垃圾(退出的容器和未使用的镜像)回收源码分析
![[ thanos源码分析系列 ]thanos query组件源码简析](/img/e4/2a87ef0d5cee0cc1c1e1b91b6fd4af.png)
[ thanos源码分析系列 ]thanos query组件源码简析

Alibaba cloud server creates snapshots and rolls back disks

Vivo browser rapid development platform practice - Overview

GoLand IDE and delve debug Go programs in kubernetes cluster

PLC -- Notes

Leetcode learning records

What is EC blower fan?
随机推荐
DBeaver 22.1.1 发布,可视化数据库管理平台
A gadget can write crawlers faster
Introduction and several months' experience of extending the solution thanos of Prometheus
股票炒股注册开户靠谱吗?安全吗?
8 张图 | 剖析 Eureka 的首次同步注册表
HJ成绩排序
Code submission specification
The practice of event driven architecture in vivo content platform
es6箭头函数中return的用法
Section 9: dual core startup of zynq
7-1 understand everything
Llvm and clang
Ice, protobuf, thrift -- Notes
扩展Prometheus的解决方案thanos的简介和几个月使用心得
vite2.9 中指定路径别名
Leetcode+ 51 - 55 retrospective and dynamic planning topics
Investment transaction and settlement of the fund
Sword finger offer II 091 Paint the house
LLVM 与 Clang
Sentinel mechanism of redis cluster