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(情景课)8 D Reading
Listen and read. Shop Smart [smɑːt] Employee of the Month: Sara['særə] (萨拉) Lopez(洛佩斯) Congratulati ...
- 20170612xlVBA含方框文档填表
Sub mainProc() Application.ScreenUpdating = False Application.DisplayAlerts = wdAlertsNone 'Dim xlAp ...
- 3-23 Rspec自动化测试(开始练习)
闰年程序 leap_year_spec.rb require_relative './leap_year' describe "Leap Year" do it "201 ...
- CentOS 7 install Nginx
1. rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.r ...
- IOS UI-Button按钮
#import "ViewController.h" @interface ViewController () { UILabel *lable; BOOL moveFlag; N ...
- MyBatis:2
转载:http://www.cnblogs.com/xrq730/p/5256221.html 前言 前一篇文章,讲了MyBatis入门,讲到了MyBatis有两个基本的配置文件,一个用来配置环境信息 ...
- javascript primise本质——为了简化异步编码而针对异步操作的代理
概述 所谓Promise,简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果. 语法 new Promise(executor); new Promise(functio ...
- Vue 就地复用策略注意事项
---template部分 div el-popover(ref="message", placement="top-start", title="标 ...
- MySQL5.7 主从复制配置
一.主从复制原理 MySQL 主从复制是一个异步的复制过程,主库发送更新事件到从库,从库读取更新记录,并执行更新记录,使得从库的内容与主库保持一致.每一个主从复制的连接,都有三个线程.拥有多个从库的主 ...
- MyEclipse 2017 CI 9 发布(附下载)
挑战全年最低价!MyEclipse线上狂欢继续!火热开启中>> 在进入年底之时,2017 CI 9是我们最大的版本发布之一.在新版本中,我们添加了对Angular 5和TypeScript ...