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

标签 python elasticsearch

我已经安装了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/

相关文章:

php - ElasticSearch 9200 端口对所有人开放。这是错的吗?

python - 在行为上,你如何只运行一个场景?

python - 为什么 scipy stats 描述的没有中位数?

python - "version file"是什么样的?

elasticsearch - 配置Elasticsearch集群时出错

elasticsearch - 使用节点客户端索引数据时出现异常

python - 迭代 PE 文件

python使用字典给定的参数评估函数

elasticsearch - 具有持久队列的 Logstash

json - ElasticSearch-将查询匹配与通配符组合