惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
Security Archives - TechRepublic
Security Archives - TechRepublic
V
Vulnerabilities – Threatpost
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Proofpoint News Feed
G
GRAHAM CLULEY
P
Privacy International News Feed
The Hacker News
The Hacker News
Forbes - Security
Forbes - Security
U
Unit 42
N
News and Events Feed by Topic
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Cisco Blogs
A
About on SuperTechFans
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
D
Docker
I
Intezer
Spread Privacy
Spread Privacy
The Last Watchdog
The Last Watchdog
V2EX - 技术
V2EX - 技术
S
Security @ Cisco Blogs
F
Full Disclosure
S
Secure Thoughts
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
W
WeLiveSecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Project Zero
Project Zero
Recorded Future
Recorded Future
Cyberwarzone
Cyberwarzone
S
Security Affairs
AWS News Blog
AWS News Blog
H
Help Net Security
The GitHub Blog
The GitHub Blog
Hacker News: Ask HN
Hacker News: Ask HN
Vercel News
Vercel News
P
Proofpoint News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Register - Security
The Register - Security
S
Schneier on Security
F
Fortinet All Blogs
C
CERT Recently Published Vulnerability Notes
L
LINUX DO - 最新话题
T
Tor Project blog
T
The Exploit Database - CXSecurity.com
MongoDB | Blog
MongoDB | Blog
Webroot Blog
Webroot Blog

博客园 - pmh905001

爬虫-今日头条我的收藏-反爬虫分析(六) pycharm的没落,vs code的兴起 爬虫-今日头条我的收藏-增量式下载网页内容(五) 今日头条源代块一行代码很长情况下的拖动问题 爬虫-今日头条我的收藏-增量式导入到mongodb(三) 爬虫-今日头条我的收藏-增量式(二) openpyxl一个bug 爬虫-今日头条我的收藏(一) pystray被隐藏菜单项显示出来的问题 pyinstaller生成的exe程序使用使用默认程序打开execel文件 pyinstaller生成的exe文件的所在的工作目录问题 windows下python的keyboard库在锁屏之后再次登陆快捷键(热键)失效问题 pyinstaller 报错ImportError: No module named _bootlocale windows下gitbash 鼠标左键选中文字自动自行终止命令 ctrl+c ^C spring-security 如何使用用户名或邮箱登录 tomcat jndi context.xml的特殊字符转义问题 struts2的优缺点以及如何改造 jetty-maven-plugin 版本导致jetty启动失败问题 Eclipse下pom.xml的提示 Cannot access defaults field of Properties
爬虫-今日头条我的收藏-增量式导入到Elastic Search(四)
pmh905001 · 2024-03-29 · via 博客园 - pmh905001

背景:

继成功导入输入数据到mongodb,sqlite3之后,发现了一些问题,(写到此处觉得还是有些地方没有去深入的学习可能mongodb已经有解决方案了?):

  • 对关键字查询支持不友好,如果要在sql中拆分出不同的关键字sql会比较麻烦。
  • 另外排序不友好,如何把最匹配的记录放在最前面?

elasticsearch是对搜索专门支持的文档数据库,对于搜索功能支持很友好,于是尝试了一把增量式导入到ES的功能。

增量式导入:

  • 用户会不断有新的收藏信息进入到数据库。
  • 导入的过程中,可能会出现意外导致进行到一半不得不退出。下次导入的时候,需要知道最后导入的记录是哪条,然后接着继续导入。
  • 可以复用mongodb、sqlite3部分的代码,对代码做了重构。有一个基础类DBImporter可以帮做文件解析,断点定位的共享逻辑。

方案:

为了支持增量式导入,必须要遭到最后一条插入数据库的记录,sqlite3/mongodb轻易而,但是对于es来说就有问题,无法找到最后一条记录是哪条,需要有一个自增的字段来记录。通过这个最大的id来找到最后一条。

尝试了两个方案,都可以做到,最终选择方案2:

  1. 插入记录中自带_id由一个uuid作用的字符串变成一个自增的数字。这样的好处是不用新增加字段,坏处是破坏了_id字段原本的作用。
  2. 增加一个叫increasement_id字段。好处是克服了方案1的缺陷,相应的代码量要增加一点。

遇到的问题:

  • 一条一条的插入记录很慢的,需要批量插入。改成批量就很快了。
  • 插入记录的过程中,有可能有超时的异常,需要设置重试。
  • content字段有可能是字符串,也有可能是json对象。mapping已经自动识别为字符串类型,再插入json对象的时候就会异常退出。对于该问题的解决是把json转化成字符串。mongodb以及sqlite3没有遇到。需要在查询的时候,str需要被转化成json对象。

参考代码:https://gitee.com/pmh905001/myfavorite/blob/master/toutiao/esimporter.py