PyMongo2Es常见问题汇总
发布日期:2022-02-27 02:38:02 浏览次数:67 分类:技术文章

本文共 1941 字,大约阅读时间需要 6 分钟。

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 = Truerefresh nodes after a node fails to respond:sniff_on_connection_fail = Trueand 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集群负载不均的问题

转载地址:https://blog.csdn.net/weixin_43401381/article/details/112373848 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:使用Python将MongoDB数据同步到Elasticsearch
下一篇:利用 cv2 给图片添加文字

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年03月30日 13时46分33秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

spring boot 与 Ant Design of Vue 实现删除按钮(十八) 2019-04-27
spring boot 与 Ant Design of Vue 实现角色管理布局以及角色的列表(十九) 2019-04-27
spring boot 与 Ant Design of Vue 实现新增角色(二十) 2019-04-27
spring boot 与 Ant Design of Vue 实现修改角色(二十一) 2019-04-27
spring boot 与 Ant Design of Vue 实现删除角色(补二十一) 2019-04-27
spring boot 与 Ant Design of Vue 实现组织管理布局的实现(二十二) 2019-04-27
spring boot 与 Ant Design of Vue 实现左侧组织树(二十三) 2019-04-27
spring boot 与 Ant Design of Vue 实现新增组织(二十四) 2019-04-27
spring boot 与 Ant Design of Vue 实现修改组织(二十五) 2019-04-27
spring boot 与 Ant Design of Vue 实现删除组织(二十六) 2019-04-27
spring boot 与 Ant Design of Vue 实现获取用户列表(二十七) 2019-04-27
spring boot 与 Ant Design of Vue 实现删除用户(三十) 2019-04-27
spring boot 与 Ant Design of Vue 鉴权体系获取用户信息的实现(三十二) 2019-04-27
Druid连接池实现自定义场景的多数据库的连接 2019-04-27
PL/SQL数据库管理工具的使用 2019-04-27
带你玩转属于自己的spring-boot-starter系列(一) 2019-04-27
带你玩转属于自己自己的spring-boot-starter系列(二) 2019-04-27
带你玩转属于自己的spring-boot-starter系列(三) 2019-04-27
基于SnowFlake算法如何让分库分表中不同的ID落在同一个库的算法的实现 2019-04-27
基于springboot的ShardingSphere5.X的分库分表的解决方案之分表解决方案(一) 2019-04-27