本文介绍了如何将 pandas 数据导出到Elasticsearch?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用 elasticsearch-py 将熊猫数据框数据导出到elasticsearch.例如,下面是一些代码:

It is possible to export a pandas dataframe data to elasticsearch using elasticsearch-py. For example, here is some code:

https://www.analyticsvidhya.com/blog/2017/05/beginners-guide-to-data-exploration-using-elastic-search-and-kibana/

有很多类似的方法,例如 to_excel to_csv to_sql .

There are a lot of similar methods like to_excel, to_csv, to_sql.

是否有一种 to_elastic 方法?如果没有,我应该在哪里提出要求?

Is there a to_elastic method? If no, where should I request it?

推荐答案

以下脚本适用于localhost:

The following script works for localhost:

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))

INDEX="dataframe"
TYPE= "record"

def rec_to_actions(df):
    import json
    for record in df.to_dict(orient="records"):
        yield ('{ "index" : { "_index" : "%s", "_type" : "%s" }}'% (INDEX, TYPE))
        yield (json.dumps(record, default=int))

from elasticsearch import Elasticsearch
e = Elasticsearch() # no args, connect to localhost:9200
if not e.indices.exists(INDEX):
    raise RuntimeError('index does not exists, use `curl -X PUT "localhost:9200/%s"` and try again'%INDEX)

r = e.bulk(rec_to_actions(df)) # return a dict

print(not r["errors"])

使用 curl -g'http://localhost:9200/dataframe/_search?q = A:[29%20TO%2039]'

可以添加许多小东西来满足不同的需求,但是主要是存在的.

There are many little things that can be added to suit different needs but main is there.

这篇关于如何将 pandas 数据导出到Elasticsearch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 01:44