PyMongo2Es常见问题汇总

1 插入数据出现连接Es Timeout

(1)错误展示
raise ConnectionTimeout("TIMEOUT", str(e), e) elasticsearch.exceptions.ConnectionTimeout: ConnectionTimeout caused by - ReadTimeoutError(HTTPConnectionPool(host='*****', port=9200): Read timed out. (read timeout=10))'

分析错误
连接超时,时间默认10s 实际场景下不适用 被error

解决办法
第一种添加超时参数:
第二种 在es语句中添加超时参数:
第三种 在连接处配置(这里使用第三种)
代码

self.es = Elasticsearch([ip], http_auth=('username', 'password'), port=9200, sniff_on_start=True,sniff_on_connection_fail=True, sniff_timeout=60)
参数说明:

sniff before doing anything:sniff_on_start = True

refresh nodes after a node fails to respond:sniff_on_connection_fail = True

and also every 60 seconds:sniff_timeout=60

(2)在读取Es中的数据时候,也会遇到TimeOut的情况
错误展示

elasticsearch.exceptions.RequestError: TransportError(400, 'parse_exception', 'failed to parse setting [timeout] with value [60] as a time value: unit is missing or unrecognized')

但报错为上面时需要将

query = self.es.search(index="***", doc_type="***", body={***}, scroll='5m', ignore_unavailable=True, size=1000, timeout=60)

修改为以下,既可以成功运行

query = self.es.search(index="***", doc_type="***", body={***}, scroll='5m', ignore_unavailable=True, size=1000, timeout=‘60s’)

2 插入数据时注意Date类型

错误展示
raise BulkIndexError('%i document(s) failed to index.' % len(errors), errors) elasticsearch.helpers.BulkIndexError: ('32 document(s) failed to index.', [{'index': {'_index': 'corporateannual', '_type': 'corporateannual', '_id': '6e3dd2c143574747a3d6b621b36c67c6', 'status': 400, 'error': {'type': 'mapper_parsing_exception', 'reason': 'failed to parse [shareContributive.sjDate]'

分析错误
shareContributive.sjDate 由于我这里这个字段是空的字符串,在设置了raise_on_error=True 参数之后有错误会触发进行收集错误(如果不需要这一部分数据的话可以直接设置为False这样就不会报错)参考链接
es会隐式转换,把es认为是date类型的字符串直接转为date类型。至于什么样的字符串es会认为可以转换成date类型

解决办法
将涉及到字段是Date(这里es会自动检索只要是“”就不行)的加个判断,将空串替换为None 这样就可以完美的解决

代码:

if dict_shareContributive['sj_date'] == "": 
	sjDate = None 
else:
	sjDate = dict_shareContributive['sj_date']

3 Elasticsearch集群负载不均的问题

详细参考链接

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值