How to export pandas data to elasticsearch?

前端 未结 4 2078
独厮守ぢ
独厮守ぢ 2021-02-20 00:26

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

https://www.analyticsvidhya.com/bl

4条回答
  •  你的背包
    2021-02-20 00:40

    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"])
    

    Verify using 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.

提交回复
热议问题