click house api tables

This commit is contained in:
2025-03-13 17:22:35 +08:00
parent e179b9b6db
commit f5c660217a
2 changed files with 41 additions and 164 deletions

View File

@@ -1,24 +1,5 @@
-- 删除旧表
DROP TABLE IF EXISTS events;
DROP TABLE IF EXISTS follower_events;
DROP TABLE IF EXISTS like_events;
DROP TABLE IF EXISTS view_events;
DROP TABLE IF EXISTS mv_kol_performance;
DROP TABLE IF EXISTS mv_platform_distribution;
DROP TABLE IF EXISTS mv_sentiment_analysis;
DROP TABLE IF EXISTS mv_interaction_time;
DROP TABLE IF EXISTS mv_conversion_funnel;
-- 创建新的events表 -- 创建新的events表
CREATE TABLE events ( CREATE TABLE IF NOT EXISTS events (
-- 基本信息 -- 基本信息
event_id UUID DEFAULT generateUUIDv4(), event_id UUID DEFAULT generateUUIDv4(),
timestamp DateTime DEFAULT now(), timestamp DateTime DEFAULT now(),
@@ -122,136 +103,46 @@ CREATE TABLE events (
ORDER BY ORDER BY
(event_type, influencer_id, date, hour) SETTINGS index_granularity = 8192; (event_type, influencer_id, date, hour) SETTINGS index_granularity = 8192;
-- 创建物化视图KOL表现概览 -- 创建influencers表
CREATE MATERIALIZED VIEW mv_kol_performance ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date) CREATE TABLE IF NOT EXISTS influencers (
ORDER BY influencer_id String,
(influencer_id, date) AS name String,
SELECT platform String,
influencer_id, profile_url String,
date, project_id String,
sum(if(event_type = 'follow', 1, 0)) - sum(if(event_type = 'unfollow', 1, 0)) AS new_followers, date Date DEFAULT toDate(now()),
sum(if(event_type = 'like', 1, 0)) - sum(if(event_type = 'unlike', 1, 0)) AS new_likes, followers UInt32 DEFAULT 0,
sum(if(event_type = 'view', 1, 0)) AS views, PRIMARY KEY (influencer_id)
sum(if(event_type = 'comment', 1, 0)) AS comments, ) ENGINE = MergeTree();
sum(if(event_type = 'share', 1, 0)) AS shares
FROM
events
GROUP BY
influencer_id,
date;
-- 创建物化视图:平台分布 -- 创建posts表
CREATE MATERIALIZED VIEW mv_platform_distribution ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date) CREATE TABLE IF NOT EXISTS posts (
ORDER BY post_id String,
(platform, date) AS title String,
SELECT influencer_id String,
platform, project_id String,
date, platform String,
count() AS events_count, type String DEFAULT 'post',
uniqExact(user_id) AS unique_users, format String DEFAULT 'text',
uniqExact(content_id) AS unique_contents date Date DEFAULT toDate(now()),
FROM timestamp DateTime DEFAULT now(),
events created_at DateTime DEFAULT now(),
GROUP BY views UInt32 DEFAULT 0,
platform, likes UInt32 DEFAULT 0,
date; comments UInt32 DEFAULT 0,
shares UInt32 DEFAULT 0,
PRIMARY KEY (post_id)
) ENGINE = MergeTree();
-- 创建物化视图:情感分析 CREATE TABLE IF NOT EXISTS promote.sync_logs (
CREATE MATERIALIZED VIEW mv_sentiment_analysis ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date) timestamp DateTime DEFAULT now(),
duration_ms UInt64,
posts_synced UInt32,
comments_synced UInt32,
influencer_changes_synced UInt32,
projects_synced UInt32,
success UInt8,
error_messages String
) ENGINE = MergeTree()
ORDER BY ORDER BY
(sentiment, date) AS (timestamp)
SELECT
sentiment,
date,
count() AS count
FROM
events
WHERE
sentiment IS NOT NULL
AND event_type = 'comment'
GROUP BY
sentiment,
date;
-- 创建物化视图:用户互动时间
CREATE MATERIALIZED VIEW mv_interaction_time ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date)
ORDER BY
(date, hour) AS
SELECT
date,
hour,
count() AS events_count,
uniqExact(user_id) AS unique_users
FROM
events
GROUP BY
date,
hour;
-- 创建物化视图:内容审核状态
CREATE MATERIALIZED VIEW mv_content_status ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date)
ORDER BY
(content_status, date) AS
SELECT
content_status,
date,
count() AS count
FROM
events
WHERE
content_status IS NOT NULL
GROUP BY
content_status,
date;
-- 创建物化视图:转化漏斗
CREATE MATERIALIZED VIEW mv_conversion_funnel ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date)
ORDER BY
(funnel_stage, date) AS
SELECT
funnel_stage,
date,
count() AS stage_count,
uniqExact(user_id) AS unique_users
FROM
events
WHERE
funnel_stage IS NOT NULL
GROUP BY
funnel_stage,
date;
-- 创建物化视图:热门内容
CREATE MATERIALIZED VIEW mv_popular_content ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date)
ORDER BY
(content_id, date) AS
SELECT
content_id,
influencer_id,
date,
sum(if(event_type = 'view', 1, 0)) AS views,
sum(if(event_type = 'like', 1, 0)) AS likes,
sum(if(event_type = 'comment', 1, 0)) AS comments,
sum(if(event_type = 'share', 1, 0)) AS shares
FROM
events
GROUP BY
content_id,
influencer_id,
date;
-- 创建物化视图:关键词分析
CREATE MATERIALIZED VIEW mv_keywords ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date)
ORDER BY
(keyword, date) AS
SELECT
arrayJoin(keywords) AS keyword,
date,
count() AS frequency
FROM
events
WHERE
length(keywords) > 0
GROUP BY
keyword,
date;

View File

@@ -1,14 +0,0 @@
DROP TABLE IF EXISTS promote.sync_logs;
CREATE TABLE IF NOT EXISTS promote.sync_logs (
timestamp DateTime DEFAULT now(),
duration_ms UInt64,
posts_synced UInt32,
comments_synced UInt32,
influencer_changes_synced UInt32,
projects_synced UInt32,
success UInt8,
error_messages String
) ENGINE = MergeTree()
ORDER BY
(timestamp)