【问题标题】:Reading Elastic cluster data into python data frame将弹性集群数据读入 python 数据框
【发布时间】:2019-01-27 17:05:55
【问题描述】:

我对弹性搜索很陌生。所以,如果我问的是一个非常简单的问题,请原谅。

在我的工作场所,我们有适当的 ELK 设置。

由于数据量很大,我们只存储了 14 天的数据,我的问题是如何在 Python 中读取数据,然后将我的分析存储在一些 NOSQL 中。

到目前为止,我的主要目标是以数据框的形式或弹性集群中的任何格式将原始数据读入 python。

我想在 1 天、1 周、1 个月等不同的时间间隔内获取它。

过去 1 周我都在挣扎。

【问题讨论】:

标签: python python-3.x python-2.7 elasticsearch elastic-stack


【解决方案1】:

您可以使用下面的代码来实现这一点

# Create a DataFrame object
from pandasticsearch import DataFrame
df = DataFrame.from_es(url='http://localhost:9200', index='indexname')

要获取索引的架构:-

 df.print_schema()

之后,您可以对 df 执行一般数据帧操作。

如果要解析结果,请执行以下操作:-

from elasticsearch import Elasticsearch
es = Elasticsearch('http://localhost:9200')
result_dict = es.search(index="indexname", body={"query": {"match_all": {}}})

然后最后将所有内容放入您的最终数据框中:-

from pandasticsearch import Select
pandas_df = Select.from_dict(result_dict).to_pandas()

希望对你有帮助..

【讨论】:

  • 当文档大小>10K 时,通过 ES 搜索将失败。您需要使用 Scroll API。
  • 感谢您的意见...如果您可以分享任何更好的方法...我也在学习...如果您知道直接从服务器读取数据并转储到 pandas 数据框的更好方法.
【解决方案2】:

这取决于您希望如何从 Elasticsearch 中读取数据。是增量阅读,即阅读每天收到的新数据,还是像批量阅读一样。对于后者,您需要在python中使用Elasticsearch的批量API,对于前者,您可以限制自己进行简单的范围查询。

读取批量数据示意图代码:https://gist.github.com/dpkshrma/04be6092eda6ae108bfc1ed820621130

如何使用 ES 的批量 API:

How to use Bulk API to store the keywords in ES by using Python

https://elasticsearch-py.readthedocs.io/en/master/helpers.html#elasticsearch.helpers.bulk

如何使用范围查询进行增量插入:

https://martinapugliese.github.io/python-for-(some)-elasticsearch-queries/

How to have Range and Match query in one elastic search query using python?

由于您希望以不同的间隔插入数据,因此您还需要执行日期聚合。

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html

How to perform multiple aggregation on an object in Elasticsearch using Python?

一旦您发出 Elasticsearch 查询,您的数据将被收集到一个临时变量中,您可以使用 Python 库而不是 NOSQL 数据库,例如 PyMongo 将 Elasticsearch 数据插入其中。

【讨论】:

  • 您能帮忙解决一些问题,我们可以将所有数据抓取到弹性集群中并放入 python 数据帧中……不是来自 csv,而是实时提供数据。如果你有这方面的经验。
猜你喜欢
  • 2017-12-14
  • 2021-08-16
  • 2015-08-07
  • 1970-01-01
  • 2021-08-22
  • 1970-01-01
  • 2021-12-29
  • 2020-09-27
  • 2012-09-02
相关资源
最近更新 更多