gpt4 book ai didi

python - 使用elasticsearch python包时读取超时错误

转载 作者:行者123 更新时间:2023-12-01 09:27:57 24 4
gpt4 key购买 nike

我已经安装了elasticsearch python 包,我创建了一个弹性集群。我正在使用下面的 python 代码将数据发送到弹性云:

from elasticsearch import Elasticsearch, RequestsHttpConnection
import time
import datetime

es = Elasticsearch(['70.19.172.110:9200'],http_auth=('<username>','<password>'))

for x in range(0,5):
es.index(index='test', doc_type='json', id=x, body={
'data1':"Hello World',
'value':325,
'time': datetime.datetime.now()

})

print("Data sent {} ".format(x))
time.sleep(60)

正如您在代码中看到的,我以 1 分钟的间隔发送数据 time.sleep(60) 。这工作正常,所有 5 个数据都在 elasticsearch 中。然后我改变了time.sleep(60)time.sleep(300)它给了我以下错误:

elasticsearch.exceptions.ConnectionTimeout: ConnectionTimeout caused by - ReadTimeoutError(HTTPConnectionPool(host='70.19.172.110', port=9200): Read timed out. (read timeout=10))

我有什么做错的地方吗?有什么方法可以让我保持与elasticsearch的连接,这样我就不会遇到这些类型的错误。

谢谢。

最佳答案

尝试提高 es.index 的超时时间,因为 Elasticsearch 的超时时间限制为 10 秒,如果超过 30 秒它不会响应,这意味着您的主机未连接或不响应请求

from elasticsearch import Elasticsearch, RequestsHttpConnection
import time
import datetime
timenow = datetime.datetime.now()

es = Elasticsearch(['70.19.172.110:9200'],http_auth=('<username>','<password>'))

for x in range(0,5):
es.index(index='test', doc_type='json', id=x, body={
'data1':"Hello World",
'value':325,
'time':timenow,
'timeout':30, # The Time Of timeout you want

})

print("Data sent {} ".format(x))
time.sleep(60)

关于python - 使用elasticsearch python包时读取超时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50213782/

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