首页 > 解决方案 > 将python数据导出到elasticsearch

问题描述

我有以下数据框:

date_facture product_description  ...     nouv2    ecartt
0   2020/04/01 00:01:14                 xyz  ...  1.00  0.00
1   2020/04/01 00:02:29                None  ...  2.00  1.00
2   2020/04/01 00:03:14                  eu  ...  1.00  0.00
3   2020/04/01 00:04:17                 com  ...  1.00  0.00

我想将它导出到 elasticsearch,为此我需要将其转换为适当的格式。

目前我的代码是:

es_url_rw = url_elastic
es_index_rw = 't_angelique'

def json_converter(o):
if isinstance(o, datetime.datetime):
    return o.strftime("%Y/%m/%d %H:%M:%S")

print(requests.put(es_url_rw + es_index_rw).text)

headers = {'Content-type': 'application/json'}.

data = df.to_json()

bulk_json = []
bulk_json.append(json.dumps({'index': {'_index': es_index_rw, '_id': 123}}})))
bulk_json.append(json.dumps(data, default=json_converter))


requests.put(es_url_rw + '_bulk', data='\n'.join(bulk_json) + '\n', headers=headers)

但是这样做我找不到弹性数据。我知道问题出在这一行:

data = df.to_json()

确实,我第一次测试

data=df.to.dict() 

使用所有可能的方向参数,我找到了弹性数据,但它们的格式不正确。当我测试时:

data=df.to_json()

我什至没有找到有关弹性的数据。我真的不知道现在该怎么办。请问你能帮帮我吗 ?

标签: pythonpandaselasticsearch

解决方案


推荐阅读