PostgreSQL与elasticsearch(保持同步) – nodeJS
我想设置postgres和elasticsearch。 但在将数据投入到elasticsearch之前,我想要防止networking或服务器宕机时的数据丢失。 阅读了这个主题后: https : //gocardless.com/blog/syncing-postgres-to-elasticsearch-lessons-learned/ 。 我想出了3个解决scheme。
-
创build一个数据库表ei:
store
,并添加任何新的/更新的数据给它。- 查询期间:将数据插入
store
。 - select新数据:
SELECT data FROM store WHERE modified > (:last modified time from elasticsearch)
- 将“新”数据发送到elasticsearch
- 查询期间:将数据插入
-
使用redis
pub/sub
/订阅请求,并使elasticsearch侦听/订阅即将到来的数据。 如果elasticsearch中断,数据将在队列中 -
在交易过程中捕捉任何错误到elasticsearch并将数据保存到一个安全的地方(ei:上面提到的
store
表)。 然后有一个cron工作推回这个数据。
当然最简单的事情就是直接向elasticsearch插入数据。 但是这样做可以防止数据在腐败期间存储在安全的地方。 1
在我看来太慢了,不像2
。 而且3
需要维护error handling代码。
现在2
是我的select。
有没有更好的方法来做到这一点? 我想听听你的意见和新的build议
:d
Redis(2)不可靠。
我决定将数据立即添加到elasticsearch并将数据添加到updates
表中。 然后在连接到elasticsearch客户端之后直接运行sync()
函数(如果之前集群closures)+每24小时运行一次cron作业以启动sync()
。 所有sync()
都是从updates
A和elasticsearch B中select最新的数据(时间或ID),并比较是否有loggingA > B
如果是这样,请使用批量API插入数据。
希望这可以帮助 :)
而我仍然开放给build议和反馈…