Пет-проект: Витринный слой. Задание 2: Подготовь структуру витрины lesson_popularity_summary.

This commit is contained in:
mnoskov
2026-05-19 15:50:11 +03:00
parent a83112d155
commit 135ee82c14
3 changed files with 158 additions and 59 deletions
@@ -0,0 +1,99 @@
-- Вариант 1(не работает):
CREATE TABLE mnoskov.lesson_popularity_summary
ON CLUSTER clickhouse_cluster
(
lesson_id UInt64,
lesson_title String,
course_id UInt64,
course_title String,
total_views UInt64,
unique_users UInt64,
first_view DateTime,
last_view DateTime
)
ENGINE = ReplicatedMergeTree(
'/clickhouse/tables/{shard}/mnoskov/lesson_popularity_summary',
'{replica}'
)
ORDER BY lesson_id;
CREATE MATERIALIZED VIEW mnoskov.mv_lesson_popularity_summary
ON CLUSTER clickhouse_cluster
TO mnoskov.lesson_popularity_summary
AS
WITH result_flv AS (
SELECT
lesson_id,
count() AS total_views,
uniq(user_id) AS unique_users,
min(viewed_at) AS first_view,
max(viewed_at) AS last_view
FROM
mnoskov.core_fact_lesson_views flv
GROUP BY
lesson_id
)
SELECT
cdl.lesson_id AS lesson_id,
cdl.title AS lesson_title,
cdc.course_id AS course_id,
cdc.title AS course_title,
rflv.total_views AS total_views,
rflv.unique_users AS unique_users,
rflv.first_view AS first_view,
rflv.last_view AS last_view
FROM
mnoskov.core_dim_lesson cdl
JOIN
mnoskov.core_dim_course cdc ON cdl.course_id = cdc.course_id
JOIN
result_flv rflv ON cdl.lesson_id = rflv.lesson_id;
-- Вариант 2(не работает):
CREATE TABLE mnoskov.lesson_popularity_summary
ON CLUSTER clickhouse_cluster
(
lesson_id UInt64,
lesson_title String,
course_id UInt64,
course_title String,
total_views AggregateFunction(count),
unique_users AggregateFunction(uniq, UInt64),
first_view AggregateFunction(min, DateTime),
last_view AggregateFunction(max, DateTime)
)
ENGINE AggregatingMergeTree
ORDER BY lesson_id;
CREATE MATERIALIZED VIEW mnoskov.mv_lesson_popularity_summary
ON CLUSTER clickhouse_cluster
TO mnoskov.lesson_popularity_summary
AS
WITH result_flv AS (
SELECT
lesson_id,
countState() AS total_views,
uniqState(user_id) AS unique_users,
minState(viewed_at) AS first_view,
maxState(viewed_at) AS last_view
FROM
mnoskov.core_fact_lesson_views flv
GROUP BY
lesson_id
)
SELECT
cdl.lesson_id AS lesson_id,
cdl.title AS lesson_title,
cdc.course_id AS course_id,
cdc.title AS course_title,
rflv.total_views AS total_views,
rflv.unique_users AS unique_users,
rflv.first_view AS first_view,
rflv.last_view AS last_view
FROM
mnoskov.core_dim_lesson cdl
JOIN
mnoskov.core_dim_course cdc ON cdl.course_id = cdc.course_id
JOIN
result_flv rflv ON cdl.lesson_id = rflv.lesson_id;