准备
- docker
- citus最新版本(docker pull citusdata/citus)
docker网络
docker network create --subnet=172.72.9.0/24 citus-test
docker network ls
启动citus服务
- 启动协调节点
docker run -dit --name citus-cod -p 5433:5432 -e POSTGRES_PASSWORD=123456 \-v /Users/zjw/Documents/docker/citus-cod:/var/lib/postgresql/data \--net=citus-test \citusdata/citus -c listen_addresses='*' -c shared_preload_libraries='citus
- 启动两个工作节点
docker run -dit --name citus-work10 -e POSTGRES_PASSWORD=123456 \-v /Users/zjw/Documents/docker/citus-work10:/var/lib/postgresql/data \--net=citus-test \citusdata/citus -c listen_addresses='*' -c shared_preload_libraries='citus'
docker run -dit --name citus-work11 -e POSTGRES_PASSWORD=123456 \-v /Users/zjw/Documents/docker/citus-work11:/var/lib/postgresql/data \--net=citus-test \citusdata/citus -c listen_addresses='*' -c shared_preload_libraries='citus'
添加节点
进入psql,添加节点并查看节点个数
docker exec -it citus-cod psql -U postgres
select * from master_add_node('citus-worker10', 5432);
select * from master_add_node('citus-work11', 5432);
select * from master_get_active_worker_nodes();
测试
- 创建表
create table student(id int PRIMARY KEY,name varchar(20)
);
- 插入数据
insert into student(id, name) values(1, 'zhangsan');
insert into student(id, name) values(2, 'lisi');
insert into student(id, name) values(3, 'wangwu');
insert into student(id, name) values(4, 'zhaoliu');
- 设置副本数为2
postgres=# set citus.shard_replication_factor=2;
- 设置student为分布表
postgres=# select create_distributed_table('student', 'id');
显示默认分片数
postgres=# show citus.shard_count;
查看分片
postgres=# select * from pg_dist_shard;
postgres=# select * from pg_dist_shard_placement;
更改分片数
postgres=# select alter_distributed_table('student', shard_count:=2, cascade_to_colocated:=true);
NOTICE: creating a new table for public.student
NOTICE: moving the data of public.student
NOTICE: dropping the old public.student
NOTICE: renaming the new table to public.studentalter_distributed_table
-------------------------(1 row)
查看分片位置
postgres=# select get_shard_id_for_distribution_column('student', 1);get_shard_id_for_distribution_column
--------------------------------------102040
(1 row)