学习记录
项目中利用PostgreSQL作为数据存储,同时利用postgis处理空间属性,再利用ElasticSearch进行数据的查询。所以需要实现PostgreSQL与Elasticsearch的同步。最终使用multicorn。环境是CentOS7。
主要是看了https://www.jianshu.com/p/629f698a7c58,加深印象。
安装PostgreSQL12
安装PostgreSQL12,可去https://www.postgresql.org/download/linux/redhat/
1. yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
2. yum install postgresql12-server
3. /usr/pgsql-12/bin/postgresql-12-setup initdb
4. systemctl enable postgresql-12
5. systemctl start postgresql-12
6. 切换至postgres用户:su postgres
7. 利用psql修改posrgres的密码:
psql
ALTER USER postgres WITH PASSWORD '你的密码';
8. 更改/var/lib/pgsql/12/data下的postgresql.conf和pg_hba.conf配置文件
postgresql.conf和pg_hba.conf配置文件通过vim postgresql.conf修改posrgresql.conf配置文件,修改内容如下图:
修改内容通过vim pg_hba.conf修改pg_hba.conf配置文件,修改内容如下图:
修改内容9. 更改配置文件后重启PostgreSQL:systemctl restart postgresql-12
10. 开启PostgreSQL端口对应的防火墙
firewall-cmd --add-port=5432/tcp --permanent
firewall-cmd --reload
安装ElasticSearch
详见https://www.elastic.co/guide/en/elasticsearch/reference/7.8/rpm.html#rpm-repo
安装Elasticsearch安装同步插件MULTICORN
MULTICORN的参考资料:https://multicorn.org/
1. 利用yum search multicorn查找合适的版本(对应PostgreSQL的版本,就用multicorn12.x86_64这个版本
search结果2. 利用yum install multicorn12.x86_64进行安装
在PostgreSQL中创建插件
1. su postgres 切换至postgres用户
2. 利用psql 执行 CREATE EXTENSION multicorn;
创建插件安装pg-es-fdw插件
1. pip3 search pg-es-fdw 查找
2. pip3 install pg-es-fdw 安装
设置PostgreSQL数据库
1. 加载扩展并创建服务器
CREATE SERVER multicorn_es FOREIGN DATA WRAPPER multicorn
OPTIONS(
wrapper 'pg_es_fdw.ElasticsearchFDW'
);
2. 创建测试表
CREATE TABLE articles(
id serial PRIMARY KEY,
title text NOT NULL,
content text NOT NULL,
createttime timestamp);
3. 创建外部表
CREATE FOREIGN TABLE articles_es (
id bigint,
title text,
content text
) SERVER multicorn_es OPTIONS (host '127.0.0.1', port '9200', node 'test', index 'articles');
4. 创建触发器
CREATE OR REPLACE FUNCTION index_article() RETURNS trigger AS $def$
BEGIN
INSERT INTO articles_es (id, title, content) VALUES
(NEW.id, NEW.title, NEW.content);
RETURN NEW;
END;
$def$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION reindex_article() RETURNS trigger AS $def$
BEGIN
UPDATE articles_es SET
title = NEW.title,
content=NEW.content
WHERE id= NEW.id;
RETURN NEW;
END;
$def$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION delete_article() RETURNS trigger AS $def$
BEGIN
DELETE FROMarticles_es a WHEREa.id =OLD.id;
RETURN OLD;
END;
$def$ LANGUAGE plpgsql;
CREATE TRIGGER es_insert_article
AFTER INSERT ON articles
FOR EACH ROW EXECUTE PROCEDURE index_article();
CREATE TRIGGER es_update_article
AFTER UPDATE OF title, content ON articles
FOR EACH ROW
WHEN (OLD.* IS DISTINCT FROMNEW.*)
EXECUTE PROCEDURE reindex_article();
CREATE TRIGGERes_delete_article
BEFORE DELETE ON articles
FOR EACH ROW EXECUTE PROCEDURE delete_article();
网友评论