天天看点

PostgreSQL内核扩展之 - ElasticSearch同步插件

elasticsearch 是开源搜索平台的新成员,实时数据分析的神器,发展迅猛,基于 lucene、restful、分布式、面向云计算设计、实时搜索、全文搜索、稳定、高可靠、可扩展、安装+使用方便。

postgresql 是起源自伯克利大学的开源数据库,历史悠久,内核扩展性极强,用户横跨各个行业。

关于postgresql的内核扩展指南请参考

<a href="https://yq.aliyun.com/articles/55981">https://yq.aliyun.com/articles/55981</a>

例如用户需要将数据库中某些数据同步到es建立索引,传统的方法需要应用来负责数据的同步。

这种方法会增加一定的开发成本,时效也不是非常的实时。

PostgreSQL内核扩展之 - ElasticSearch同步插件
PostgreSQL内核扩展之 - ElasticSearch同步插件

postgresql的扩展插件pg-es-fdw,使用postgresql的foreign data wrap,允许直接在数据库中读写es,方便用户实时的在es建立索引。

这种方法不需要额外的程序支持,时效也能得到保障。

PostgreSQL内核扩展之 - ElasticSearch同步插件

略,需要包含 --with-python

对实体表,创建触发器函数,在用户对实体表插入,删除,更新时,通过触发器函数自动将数据同步到对应es的外部表。

同步过程调用fdw的接口,对es进行索引的建立,更新,删除。

<a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/setup-repositories.html">https://www.elastic.co/guide/en/elasticsearch/reference/current/setup-repositories.html</a>

<a href="http://www.vpsee.com/2014/05/install-and-play-with-elasticsearch/">http://www.vpsee.com/2014/05/install-and-play-with-elasticsearch/</a>

<a href="https://github.com/mikulas/pg-es-fdw">https://github.com/mikulas/pg-es-fdw</a>

<a href="https://wiki.postgresql.org/wiki/fdw">https://wiki.postgresql.org/wiki/fdw</a>

<a href="http://multicorn.org/">http://multicorn.org/</a>

<a href="http://pgxn.org/dist/multicorn/">http://pgxn.org/dist/multicorn/</a>

<a href="http://multicorn.readthedocs.io/en/latest/index.html">http://multicorn.readthedocs.io/en/latest/index.html</a>

postgresql提供的fdw接口,允许用户在数据库中直接操纵外部的数据源,所以支持es只是一个例子,还可以支持更多的数据源。

这是已经支持的,几乎涵盖了所有的数据源。

multicorn在fdw接口的上层再抽象了一层,支持使用python写fdw接口,方便快速试错,如果对性能要求不是那么高,直接用multicore就可以了。

开发人员如何编写fdw? 可以参考一下如下:

<a href="https://www.postgresql.org/docs/9.6/static/fdwhandler.html">https://www.postgresql.org/docs/9.6/static/fdwhandler.html</a>