昨天有简单写过使用http fdw+plv8 处理数据的,以下是一个结合pg_cron 的
场景: 使用pg_cron 每隔一分钟操作此获取ip 的操作,ip 获取通过http fdw 处理,同时结合plv8 处理json 数据
环境准备
- docker-compose 文件
version: "3"
services:
graphql-engine:
image: hasura/graphql-engine:v1.1.0
ports:
- "8080:8080"
environment:
HASURA_GRAPHQL_DATABASE_URL: postgres://postgres:dalong@pgspider-fdw:5432/postgres
HASURA_GRAPHQL_ENABLE_CONSOLE: "true" # set to "false" to disable console
HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
pgspider-fdw:
image: dalongrong/pgspider:gzip-http-plv8
ports:
- "5432:5432"
environment:
- "POSTGRES_PASSWORD=dalong"
- 启动
docker-compose up -d
- 创建数据表
记录ip
CREATE TABLE userips (
id SERIAL PRIMARY KEY,
origin text
);
使用扩展
- 创建扩展
create extension plv8;
create extension http;
create extension pg_cron;
- 使用http fdw+ plv8 获取ip
CREATE TYPE rec AS (origin text);
CREATE or replace FUNCTION apps() RETURNS SETOF rec AS
$$
var ip = plv8.execute(`select content FROM http_get('http://httpbin.org/ip')`);
return [{"origin": JSON.parse(ip[0].content).origin}]
$$
LANGUAGE plv8;
- 创建函数处理ip insert (pg_cron 调用)
CREATE OR REPLACE FUNCTION insert_ip()
RETURNS void AS
$$
BEGIN
insert into userips(origin) select origin from apps();
END;
$$ LANGUAGE plpgsql;
- 创建任务调度
SELECT cron.schedule('* * * * *','select insert_ip()');
- 效果
任务调度列表
数据
集成graphql-engine
这个就比较简单了,添加track,查询就可以了,数据是基于任务调动添加的
参考资料
https://github.com/plv8/plv8
https://github.com/pramsey/pgsql-http
https://www.cnblogs.com/rongfengliang/p/12372879.html
https://www.cnblogs.com/rongfengliang/p/12375186.html
https://github.com/citusdata/pg_cron