Skip to content

Instantly share code, notes, and snippets.

@gam6itko
Last active October 24, 2022 23:01
Show Gist options
  • Save gam6itko/3e8fc5d13e460ec93a6f03107b63eaed to your computer and use it in GitHub Desktop.
Save gam6itko/3e8fc5d13e460ec93a6f03107b63eaed to your computer and use it in GitHub Desktop.
Rebrain clickhouse
CREATE TABLE events_local ON CLUSTER 'local'
(
event_type String,
title String,
description String,
content String,
date Date
)
ENGINE = ReplicatedMergeTree('/ch/{cluster}/{shard}/tables/events_local', '{replica}')
PARTITION BY date
ORDER BY `date`;
CREATE TABLE events_local_distributed ON CLUSTER 'local'
(
event_type String,
title String,
description String,
content String,
date Date
)
ENGINE = Distributed('{cluster}', 'default', 'events_local', rand());
# on server1
INSERT INTO events_local VALUES ('denatification', '1', '1', '1', '2022-02-24');
# on server2
INSERT INTO events_local VALUES ('demilitarization', '2', '2', '2', '2022-02-24');
# on server3
INSERT INTO events_local VALUES ('decomunization', '3', '3', '3', '2022-02-24');
# on server4
INSERT INTO events_local VALUES ('dehumanization', '4', '4', '4', '2022-02-24');
# check
SELECT * FROM events_local;
SELECT * FROM events_local_distributed;
<?xml version="1.0"?>
<clickhouse>
<clickhouse_remote_servers>
<local>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>server1</host>
<port>9000</port>
</replica>
<replica>
<host>server2</host>
<port>9000</port>
</replica>
</shard>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>server3</host>
<port>9000</port>
</replica>
<replica>
<host>server4</host>
<port>9000</port>
</replica>
</shard>
</local>
</clickhouse_remote_servers>
<zookeeper-servers>
<node index="1">
<host>server1</host>
<port>2181</port>
</node>
<node index="2">
<host>server2</host>
<port>2181</port>
</node>
<node index="3">
<host>server3</host>
<port>2181</port>
</node>
</zookeeper-servers>
<macros>
<cluster>local</cluster>
<shard>01</shard>
<replica>server1</replica>
</macros>
</clickhouse>
<?xml version="1.0"?>
<clickhouse>
<clickhouse_remote_servers>
<local>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>server1</host>
<port>9000</port>
</replica>
<replica>
<host>server2</host>
<port>9000</port>
</replica>
</shard>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>server3</host>
<port>9000</port>
</replica>
<replica>
<host>server4</host>
<port>9000</port>
</replica>
</shard>
</local>
</clickhouse_remote_servers>
<zookeeper-servers>
<node index="1">
<host>server1</host>
<port>2181</port>
</node>
<node index="2">
<host>server2</host>
<port>2181</port>
</node>
<node index="3">
<host>server3</host>
<port>2181</port>
</node>
</zookeeper-servers>
<macros>
<cluster>local</cluster>
<shard>01</shard>
<replica>server2</replica>
</macros>
</clickhouse>
<?xml version="1.0"?>
<clickhouse>
<clickhouse_remote_servers>
<local>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>server1</host>
<port>9000</port>
</replica>
<replica>
<host>server2</host>
<port>9000</port>
</replica>
</shard>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>server3</host>
<port>9000</port>
</replica>
<replica>
<host>server4</host>
<port>9000</port>
</replica>
</shard>
</local>
</clickhouse_remote_servers>
<zookeeper-servers>
<node index="1">
<host>server1</host>
<port>2181</port>
</node>
<node index="2">
<host>server2</host>
<port>2181</port>
</node>
<node index="3">
<host>server3</host>
<port>2181</port>
</node>
</zookeeper-servers>
<macros>
<cluster>local</cluster>
<shard>02</shard>
<replica>server3</replica>
</macros>
</clickhouse>
<?xml version="1.0"?>
<clickhouse>
<clickhouse_remote_servers>
<local>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>server1</host>
<port>9000</port>
</replica>
<replica>
<host>server2</host>
<port>9000</port>
</replica>
</shard>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>server3</host>
<port>9000</port>
</replica>
<replica>
<host>server4</host>
<port>9000</port>
</replica>
</shard>
</local>
</clickhouse_remote_servers>
<zookeeper-servers>
<node index="1">
<host>server1</host>
<port>2181</port>
</node>
<node index="2">
<host>server2</host>
<port>2181</port>
</node>
<node index="3">
<host>server3</host>
<port>2181</port>
</node>
</zookeeper-servers>
<macros>
<cluster>local</cluster>
<shard>02</shard>
<replica>server4</replica>
</macros>
</clickhouse>
<?xml version="1.0"?>
<clickhouse>
<include_from>/etc/clickhouse-server/cluster.xml</include_from>
<remote_servers incl="clickhouse_remote_servers" />
<zookeeper incl="zookeeper-servers" optional="true" />
<macros incl="macros" optional="true" />
<listen_host>0.0.0.0</listen_host>
</clickhouse>
146.190.16.74 server1.0b62601f167c08698f0f0cf49a08e21a server1
146.190.31.110 server2.0b62601f167c08698f0f0cf49a08e21a server2
134.209.196.179 server3.0b62601f167c08698f0f0cf49a08e21a server3
142.93.233.227 server4.0b62601f167c08698f0f0cf49a08e21a server4
server1.0b62601f167c08698f0f0cf49a08e21a :) SELECT * FROM events_local;
SELECT *
FROM events_local
Query id: d6ac73a0-6da8-45fa-a5db-adff569890b4
┌─event_type─────┬─title─┬─description─┬─content─┬───────date─┐
│ denatification │ 1 │ 1 │ 1 │ 2022-02-24 │
└────────────────┴───────┴─────────────┴─────────┴────────────┘
┌─event_type───────┬─title─┬─description─┬─content─┬───────date─┐
│ demilitarization │ 2 │ 2 │ 2 │ 2022-02-24 │
└──────────────────┴───────┴─────────────┴─────────┴────────────┘
2 rows in set. Elapsed: 0.003 sec.
server1.0b62601f167c08698f0f0cf49a08e21a :) SELECT * FROM events_local_distributed;
SELECT *
FROM events_local_distributed
Query id: a94f5f45-98be-417b-8e70-116ccc397495
┌─event_type─────┬─title─┬─description─┬─content─┬───────date─┐
│ denatification │ 1 │ 1 │ 1 │ 2022-02-24 │
└────────────────┴───────┴─────────────┴─────────┴────────────┘
┌─event_type───────┬─title─┬─description─┬─content─┬───────date─┐
│ demilitarization │ 2 │ 2 │ 2 │ 2022-02-24 │
└──────────────────┴───────┴─────────────┴─────────┴────────────┘
┌─event_type─────┬─title─┬─description─┬─content─┬───────date─┐
│ decomunization │ 3 │ 3 │ 3 │ 2022-02-24 │
└────────────────┴───────┴─────────────┴─────────┴────────────┘
┌─event_type─────┬─title─┬─description─┬─content─┬───────date─┐
│ dehumanization │ 4 │ 4 │ 4 │ 2022-02-24 │
└────────────────┴───────┴─────────────┴─────────┴────────────┘
4 rows in set. Elapsed: 0.014 sec.
server2.0b62601f167c08698f0f0cf49a08e21a :) SELECT * FROM events_local;
SELECT *
FROM events_local
Query id: eba79906-3df2-4893-8ada-6e16475d18ef
┌─event_type─────┬─title─┬─description─┬─content─┬───────date─┐
│ denatification │ 1 │ 1 │ 1 │ 2022-02-24 │
└────────────────┴───────┴─────────────┴─────────┴────────────┘
┌─event_type───────┬─title─┬─description─┬─content─┬───────date─┐
│ demilitarization │ 2 │ 2 │ 2 │ 2022-02-24 │
└──────────────────┴───────┴─────────────┴─────────┴────────────┘
2 rows in set. Elapsed: 0.003 sec.
server2.0b62601f167c08698f0f0cf49a08e21a :) SELECT * FROM events_local_distributed;
SELECT *
FROM events_local_distributed
Query id: f2c77982-adfd-4746-8f03-231065be5eb0
┌─event_type─────┬─title─┬─description─┬─content─┬───────date─┐
│ denatification │ 1 │ 1 │ 1 │ 2022-02-24 │
└────────────────┴───────┴─────────────┴─────────┴────────────┘
┌─event_type───────┬─title─┬─description─┬─content─┬───────date─┐
│ demilitarization │ 2 │ 2 │ 2 │ 2022-02-24 │
└──────────────────┴───────┴─────────────┴─────────┴────────────┘
┌─event_type─────┬─title─┬─description─┬─content─┬───────date─┐
│ decomunization │ 3 │ 3 │ 3 │ 2022-02-24 │
└────────────────┴───────┴─────────────┴─────────┴────────────┘
┌─event_type─────┬─title─┬─description─┬─content─┬───────date─┐
│ dehumanization │ 4 │ 4 │ 4 │ 2022-02-24 │
└────────────────┴───────┴─────────────┴─────────┴────────────┘
4 rows in set. Elapsed: 0.008 sec.
server3.0b62601f167c08698f0f0cf49a08e21a :) SELECT * FROM events_local;
SELECT *
FROM events_local
Query id: eefcd4f6-5c92-4b9f-9839-9a37a6b438b0
┌─event_type─────┬─title─┬─description─┬─content─┬───────date─┐
│ decomunization │ 3 │ 3 │ 3 │ 2022-02-24 │
└────────────────┴───────┴─────────────┴─────────┴────────────┘
┌─event_type─────┬─title─┬─description─┬─content─┬───────date─┐
│ dehumanization │ 4 │ 4 │ 4 │ 2022-02-24 │
└────────────────┴───────┴─────────────┴─────────┴────────────┘
2 rows in set. Elapsed: 0.003 sec.
server3.0b62601f167c08698f0f0cf49a08e21a :) SELECT * FROM events_local_distributed;
SELECT *
FROM events_local_distributed
Query id: 5884f719-923b-4ded-bfa2-5fa428bfb3cf
┌─event_type─────┬─title─┬─description─┬─content─┬───────date─┐
│ decomunization │ 3 │ 3 │ 3 │ 2022-02-24 │
└────────────────┴───────┴─────────────┴─────────┴────────────┘
┌─event_type─────┬─title─┬─description─┬─content─┬───────date─┐
│ dehumanization │ 4 │ 4 │ 4 │ 2022-02-24 │
└────────────────┴───────┴─────────────┴─────────┴────────────┘
┌─event_type─────┬─title─┬─description─┬─content─┬───────date─┐
│ denatification │ 1 │ 1 │ 1 │ 2022-02-24 │
└────────────────┴───────┴─────────────┴─────────┴────────────┘
┌─event_type───────┬─title─┬─description─┬─content─┬───────date─┐
│ demilitarization │ 2 │ 2 │ 2 │ 2022-02-24 │
└──────────────────┴───────┴─────────────┴─────────┴────────────┘
4 rows in set. Elapsed: 0.011 sec.
server4.0b62601f167c08698f0f0cf49a08e21a :) SELECT * FROM events_local;
SELECT *
FROM events_local
Query id: 15726b8b-f3ac-421a-a32f-9bd9f5888ac9
┌─event_type─────┬─title─┬─description─┬─content─┬───────date─┐
│ decomunization │ 3 │ 3 │ 3 │ 2022-02-24 │
└────────────────┴───────┴─────────────┴─────────┴────────────┘
┌─event_type─────┬─title─┬─description─┬─content─┬───────date─┐
│ dehumanization │ 4 │ 4 │ 4 │ 2022-02-24 │
└────────────────┴───────┴─────────────┴─────────┴────────────┘
2 rows in set. Elapsed: 0.003 sec.
server4.0b62601f167c08698f0f0cf49a08e21a :) SELECT * FROM events_local_distributed;
SELECT *
FROM events_local_distributed
Query id: 51bfbdac-a90f-4137-95ca-bdf1c2523f4a
┌─event_type─────┬─title─┬─description─┬─content─┬───────date─┐
│ decomunization │ 3 │ 3 │ 3 │ 2022-02-24 │
└────────────────┴───────┴─────────────┴─────────┴────────────┘
┌─event_type─────┬─title─┬─description─┬─content─┬───────date─┐
│ dehumanization │ 4 │ 4 │ 4 │ 2022-02-24 │
└────────────────┴───────┴─────────────┴─────────┴────────────┘
┌─event_type─────┬─title─┬─description─┬─content─┬───────date─┐
│ denatification │ 1 │ 1 │ 1 │ 2022-02-24 │
└────────────────┴───────┴─────────────┴─────────┴────────────┘
┌─event_type───────┬─title─┬─description─┬─content─┬───────date─┐
│ demilitarization │ 2 │ 2 │ 2 │ 2022-02-24 │
└──────────────────┴───────┴─────────────┴─────────┴────────────┘
4 rows in set. Elapsed: 0.008 sec.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment