citus 多租户应用开发(来自官方文档)
citus 官方文档很不错,资料很全,同时包含一个多租户应用的文档,所以运行下,方便学习
环境准备
使用docker-compose 运行,同时集成了graphql 引擎,很方便
- docker-compose 文件
version: '2.1'
services:
graphql-engine:
image: hasura/graphql-engine:v1.0.0-alpha26
ports:
- "8080:8080"
command: >
/bin/sh -c "
graphql-engine --database-url postgres://postgres@master/postgres serve --enable-console;
"
master:
container_name: "${COMPOSE_PROJECT_NAME:-citus}_master"
image: 'citusdata/citus:7.5.1'
ports: ["${MASTER_EXTERNAL_PORT:-5432}:5432"]
labels: ['com.citusdata.role=Master']
worker:
image: 'citusdata/citus:7.5.1'
labels: ['com.citusdata.role=Worker']
depends_on: { manager: { condition: service_healthy } }
manager:
container_name: "${COMPOSE_PROJECT_NAME:-citus}_manager"
image: 'citusdata/membership-manager:0.2.0'
volumes: ['/var/run/docker.sock:/var/run/docker.sock']
depends_on: { master: { condition: service_healthy } }
- 数据准备
curl https://examples.citusdata.com/tutorial/companies.csv > companies.csv
curl https://examples.citusdata.com/tutorial/campaigns.csv > campaigns.csv
curl https://examples.citusdata.com/tutorial/ads.csv > ads.csv
- 创建表
CREATE TABLE companies (
id bigint NOT NULL,
name text NOT NULL,
image_url text,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
CREATE TABLE campaigns (
id bigint NOT NULL,
company_id bigint NOT NULL,
name text NOT NULL,
cost_model text NOT NULL,
state text NOT NULL,
monthly_budget bigint,
blacklisted_site_urls text[],
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
CREATE TABLE ads (
id bigint NOT NULL,
company_id bigint NOT NULL,
campaign_id bigint NOT NULL,
name text NOT NULL,
image_url text,
target_url text,
impressions_count bigint DEFAULT 0,
clicks_count bigint DEFAULT 0,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
- 添加表关系
ALTER TABLE companies ADD PRIMARY KEY (id);
ALTER TABLE campaigns ADD PRIMARY KEY (id, company_id);
ALTER TABLE ads ADD PRIMARY KEY (id, company_id);
citus 分布式处理
- 添加数据分布式表功能
很方便,就是select 语句,调用函数即可
SELECT create_distributed_table('companies', 'id');
SELECT create_distributed_table('campaigns', 'company_id');
SELECT create_distributed_table('ads', 'company_id');
- 导入数据
citus 环境起来之后就可以使用功能导入数据了

- 效果

- 一个json 查询
SELECT campaigns.id, campaigns.name, campaigns.monthly_budget,
sum(impressions_count) as total_impressions, sum(clicks_count) as total_clicks
FROM ads, campaigns
WHERE ads.company_id = campaigns.company_id
AND campaigns.company_id = 5
AND campaigns.state = 'running'
GROUP BY campaigns.id, campaigns.name, campaigns.monthly_budget
ORDER BY total_impressions, total_clicks;
- 效果

数据模型说明
实际上上面的核心是创建分布式表,使用的create_distributed_table,同时定义了,多租户的数据隔离id company_id
后边的操作都是基本的sql 操作,后边会有citus 多租户应用开发的一些好的实践介绍。
参考资料
https://docs.citusdata.com/en/v7.5/get_started/tutorial_multi_tenant.html
https://docs.citusdata.com/en/v7.5/sharding/data_modeling.html#distributing-by-tenant-id
https://github.com/hasura/graphql-on-various-pg
https://github.com/rongfengliang/citus-hasuar-graphql
citus 多租户应用开发(来自官方文档)的更多相关文章
- citus real-time 分析demo( 来自官方文档)
citus 对于多租户以及实时应用的开发都是比较好的,官方也提供了demo 参考项目 https://github.com/rongfengliang/citus-hasuar-graphql 环 ...
- Android接口与架构(驱动开发)翻译官方文档
Android接口与架构 Android在设备的规格与驱动方面给了你很大的自由来实现.HAL层提供了一个标准的方式来打通Android系统层与硬件层.Android系统是开源的,所以你能够在接口和性能 ...
- 谷歌console开发人员官方文档(注意是谷歌)
https://developers.google.com/web/tools/chrome-devtools/debug/console/console-ui?hl=en#opening-the-c ...
- IOS 开发的官方文档链接
下面这些文章都是苹果官方的开发文档,非常有用: iOS Developer Library https://developer.apple.com/library/ios/navigation/ 总入 ...
- NSRunLoop(来自官方文档)
The NSRunLoop class declares the programmatic interface to objects that manage input sources. An NSR ...
- c++官方文档
来自官方文档...感谢老王指出需要c++11,一下代码全在c++11下编译,编译参数加入 -std=c++11 #include<stdio.h> #include<iostrea ...
- iOS开发官方文档汇总
程序员的学习过程是无止境的,程序员学习的途径是多样的.可以从视频教程中领悟,也可以从他人的代码中 理解.但当我们专注于某一个平台在开发的时候,对于某个API使用或者功能实现有疑问,通常简单的测试可以让 ...
- 《SpringCloudDubbo开发日记》(一)Nacos连官方文档都没写好
背景 现在的微服务框架一般分dubbo和springcloud两套服务治理体系,dubbo是基于zookeeper为注册中心,springcloud是基于eureka作为注册中心. 但是现在eurek ...
- 【pytest官方文档】解读- 开发可pip安装的第三方插件
在上一篇的 hooks 函数分享中,开发了一个本地插件示例,其实已经算是在编写插件了.今天继续跟着官方文档学习更多知识点. 一个插件包含一个或多个钩子函数,pytest 正是通过调用各种钩子组成的插件 ...
随机推荐
- English trip -- VC(情景课)5 D
Read 阅读 Listen and read. 听并读 Notice from Riverside Library Come and visit Riverside Library.The new ...
- Win7 默认.lnk打开方式全是别的程序 还原的办法
Xu言: no zuo no die~ 今天,一个朋友问我,他电脑桌面上点任何东西都是提示下载... - -||| 本以为是中毒了,然后上去看了一眼..发现他自己把所有.lnk 的默认打开方式选择了搜 ...
- 【异常】Caused by: java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around
原因:缺少aspect,AOP的maven坐标 导入maven坐标: <dependency> <groupId>org.springframework</groupId ...
- OC 复合
在Objective-C中复合是通过包含作为实例变量的对象指针实现的 严格来说,只有对象间的组合才叫复合 --------------------Car.h---------------------- ...
- EL表达式、 jstl标签
https://www.cnblogs.com/zhaotiancheng/p/6391894.html https://blog.csdn.net/zdwzzu2006/article/detail ...
- STM32F103各PIN脚封装图
1.36PIN 2.48PIN 3.64PIN 4.100PIN STM32ZET6详细pin脚图
- web 攻击靶机解题过程
sql注入靶机攻击过程请参考 https://pentesterlab.com/exercises/from_sqli_to_shell/course http://www.sohu.com/a/12 ...
- bzoj1617
题解: dp f[i]表示运i头奶牛需要的时间 f[i]=f[i-j]+a[i]+2*m 然后ans=f[n]-m(最后一次不用跑回来) 代码: #include<bits/stdc++.h&g ...
- 不吹不擂,Python编程【315+道题】
写在前面 近日恰逢学生毕业季,课程后期大家“期待+苦逼”的时刻莫过于每天早上内容回顾和面试题问答部分[临近毕业每天课前用40-60分钟对之前内容回顾.提问和补充,专挑班里不爱说话就的同学回答]. 期待 ...
- 2.2 C++类的成员变量和成员函数
参考:http://www.weixueyuan.net/view/6334.html 总结: 类成员的声明和定义: 类成员函数的定义分类内定义(内联)和类外定义(可用 inline 关键字 强制转换 ...