Skip to content

Commit 50112e7

Browse files
committed
Add to clickhouse README.md database creation
1 parent dc26c41 commit 50112e7

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

database/clickhouse/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@
2323
* Clickhouse cluster mode is not officially supported, since it's not tested right now, but you can try enabling `schema_migrations` table replication by specifying a `x-cluster-name`:
2424
* When `x-cluster-name` is specified, `x-migrations-table-engine` also should be specified. See the docs regarding [replicated table engines](https://clickhouse.tech/docs/en/engines/table-engines/mergetree-family/replication/#table_engines-replication).
2525
* When `x-cluster-name` is specified, only the `schema_migrations` table is replicated across the cluster. You still need to write your migrations so that the application tables are replicated within the cluster.
26+
* If you want to create database inside the migration, you should know, that table which will manage migrations `schema-migrations table` will be in `deafault` table, so you can't use `USE <database_name>` inside migration. In this case you can not specify the database in the connection string (example you can find [here](examples/migrations/003_create_database.up.sql))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
DROP TABLE IF EXISTS driver_ratings ON CLUSTER cluster_1;
2+
DROP TABLE IF EXISTS user_ratings ON CLUSTER cluster_1;
3+
DROP TABLE IF EXISTS orders ON CLUSTER cluster_1;
4+
DROP TABLE IF EXISTS driver_ratings_queue ON CLUSTER cluster_1;
5+
DROP TABLE IF EXISTS user_ratings_queue ON CLUSTER cluster_1;
6+
DROP TABLE IF EXISTS orders_queue ON CLUSTER cluster_1;
7+
DROP VIEW IF EXISTS user_ratings_queue_mv ON CLUSTER cluster_1;
8+
DROP VIEW IF EXISTS driver_ratings_queue_mv ON CLUSTER cluster_1;
9+
DROP VIEW IF EXISTS orders_queue_mv ON CLUSTER cluster_1;
10+
DROP DATABASE IF EXISTS analytics ON CLUSTER cluster_1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
CREATE DATABASE IF NOT EXISTS analytics ON CLUSTER cluster_1;
2+
3+
CREATE TABLE IF NOT EXISTS analytics.driver_ratings ON CLUSTER cluster_1(
4+
rate UInt8,
5+
userID Int64,
6+
driverID String,
7+
orderID String,
8+
inserted_time DateTime DEFAULT now()
9+
) ENGINE = ReplicatedMergeTree
10+
PARTITION BY driverID
11+
ORDER BY (inserted_time);
12+
13+
CREATE TABLE analytics.driver_ratings_queue ON CLUSTER cluster_1(
14+
rate UInt8,
15+
userID Int64,
16+
driverID String,
17+
orderID String
18+
) ENGINE = Kafka
19+
SETTINGS kafka_broker_list = 'broker:9092',
20+
kafka_topic_list = 'driver-ratings',
21+
kafka_group_name = 'rating_readers',
22+
kafka_format = 'Avro',
23+
kafka_max_block_size = 1048576;
24+
25+
CREATE MATERIALIZED VIEW analytics.driver_ratings_queue_mv ON CLUSTER cluster_1 TO analytics.driver_ratings AS
26+
SELECT rate, userID, driverID, orderID
27+
FROM analytics.driver_ratings_queue;
28+
29+
CREATE TABLE IF NOT EXISTS analytics.user_ratings ON CLUSTER cluster_1(
30+
rate UInt8,
31+
userID Int64,
32+
driverID String,
33+
orderID String,
34+
inserted_time DateTime DEFAULT now()
35+
) ENGINE = ReplicatedMergeTree
36+
PARTITION BY userID
37+
ORDER BY (inserted_time);
38+
39+
CREATE TABLE analytics.user_ratings_queue ON CLUSTER cluster_1(
40+
rate UInt8,
41+
userID Int64,
42+
driverID String,
43+
orderID String
44+
) ENGINE = Kafka
45+
SETTINGS kafka_broker_list = 'broker:9092',
46+
kafka_topic_list = 'user-ratings',
47+
kafka_group_name = 'rating_readers',
48+
kafka_format = 'JSON',
49+
kafka_max_block_size = 1048576;
50+
51+
CREATE MATERIALIZED VIEW analytics.user_ratings_queue_mv ON CLUSTER cluster_1 TO analytics.user_ratings AS
52+
SELECT rate, userID, driverID, orderID
53+
FROM analytics.user_ratings_queue;
54+
55+
CREATE TABLE IF NOT EXISTS analytics.orders ON CLUSTER cluster_1(
56+
from_place String,
57+
to_place String,
58+
userID Int64,
59+
driverID String,
60+
orderID String,
61+
inserted_time DateTime DEFAULT now()
62+
) ENGINE = ReplicatedMergeTree
63+
PARTITION BY driverID
64+
ORDER BY (inserted_time);
65+
66+
CREATE TABLE analytics.orders_queue ON CLUSTER cluster_1(
67+
from_place String,
68+
to_place String,
69+
userID Int64,
70+
driverID String,
71+
orderID String
72+
) ENGINE = Kafka
73+
SETTINGS kafka_broker_list = 'broker:9092',
74+
kafka_topic_list = 'orders',
75+
kafka_group_name = 'order_readers',
76+
kafka_format = 'Avro',
77+
kafka_max_block_size = 1048576;
78+
79+
CREATE MATERIALIZED VIEW analytics.orders_queue_mv ON CLUSTER cluster_1 TO orders AS
80+
SELECT from_place, to_place, userID, driverID, orderID
81+
FROM analytics.orders_queue;

0 commit comments

Comments
 (0)