gpt4 book ai didi

python - 通过python发送日期时间戳到elasticsearch

转载 作者:行者123 更新时间:2023-12-03 00:56:57 25 4
gpt4 key购买 nike

我的数据共有5列,其中3列是pandas to_datetime格式:

col1                 col2                col3                col4            a           
2017-01-01 21:07:57 2017-01-01 21:07:58 2017-01-01 21:07:59 misc_text_data text
2017-01-01 21:07:42 2017-01-01 21:07:48 2017-01-01 21:07:49 misc_test_data text2
2017-01-01 21:07:33 2017-01-01 21:07:22 2017-01-01 21:07:21 mist_test_fata fext3

如何通过elasticsearch.py​​字段将数据发送到elasticsearch?

我正在尝试让Elasticsearch将格式识别为时间戳记,以便我可以从这些日期进行时间表分析。

我目前正在像下面这样发送我的数据,但无法识别时间:
mylist = []
for doc in df_json:
doc['tag'] = "mytags"
action = { "_index":"myindexoftimes", "_type": "mytimes", "_source":doc}
mylist.append(action)

helpers.bulk(es, mylist)

任何帮助都会有所帮助。谢谢。

最佳答案

不确定代码中的问题是什么,可能与时间戳格式有关。

无论如何,选择了iso格式的 Pandas to_json对我来说很有效:

import pandas as pd
from elasticsearch import Elasticsearch
import json

es = Elasticsearch()

data = ["2017-01-01 21:07:57, 2017-01-01 21:07:58, 2017-01-01 21:07:59, misc_text_data, text".split(','),
"2017-01-01 21:07:42, 2017-01-01 21:07:48, 2017-01-01 21 07:49, misc_test_data, text2".split(','),
"2017-01-01 21:07:33, 2017-01-01 21:07:22, 2017-01-01 21 07:21, misc_test_fata, fext3".split(',')]

df = pd.DataFrame(data,columns = ['col1','col2','col3','col4','a'])
for col in ['col1','col2','col3']:
df[col] = pd.to_datetime(df[col])

jsons = json.loads(df.to_json(orient='records',date_format='iso'))

for j in jsons:
j['injection_timestamp'] = pd.to_datetime('now')
es.index(index="test33", doc_type='time_text', body=j)

关于python - 通过python发送日期时间戳到elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43051934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com