diff --git a/backend/db/sql/clickhouse/create_events.sql b/backend/db/sql/clickhouse/create_events.sql index 5f008ae..83e7df2 100644 --- a/backend/db/sql/clickhouse/create_events.sql +++ b/backend/db/sql/clickhouse/create_events.sql @@ -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表 -CREATE TABLE events ( +CREATE TABLE IF NOT EXISTS events ( -- 基本信息 event_id UUID DEFAULT generateUUIDv4(), timestamp DateTime DEFAULT now(), @@ -122,136 +103,46 @@ CREATE TABLE events ( ORDER BY (event_type, influencer_id, date, hour) SETTINGS index_granularity = 8192; --- 创建物化视图:KOL表现概览 -CREATE MATERIALIZED VIEW mv_kol_performance ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date) -ORDER BY - (influencer_id, date) AS -SELECT - influencer_id, - date, - sum(if(event_type = 'follow', 1, 0)) - sum(if(event_type = 'unfollow', 1, 0)) AS new_followers, - sum(if(event_type = 'like', 1, 0)) - sum(if(event_type = 'unlike', 1, 0)) AS new_likes, - sum(if(event_type = 'view', 1, 0)) AS views, - sum(if(event_type = 'comment', 1, 0)) AS comments, - sum(if(event_type = 'share', 1, 0)) AS shares -FROM - events -GROUP BY - influencer_id, - date; +-- 创建influencers表 +CREATE TABLE IF NOT EXISTS influencers ( + influencer_id String, + name String, + platform String, + profile_url String, + project_id String, + date Date DEFAULT toDate(now()), + followers UInt32 DEFAULT 0, + PRIMARY KEY (influencer_id) +) ENGINE = MergeTree(); --- 创建物化视图:平台分布 -CREATE MATERIALIZED VIEW mv_platform_distribution ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date) -ORDER BY - (platform, date) AS -SELECT - platform, - date, - count() AS events_count, - uniqExact(user_id) AS unique_users, - uniqExact(content_id) AS unique_contents -FROM - events -GROUP BY - platform, - date; +-- 创建posts表 +CREATE TABLE IF NOT EXISTS posts ( + post_id String, + title String, + influencer_id String, + project_id String, + platform String, + type String DEFAULT 'post', + format String DEFAULT 'text', + date Date DEFAULT toDate(now()), + timestamp DateTime DEFAULT now(), + created_at DateTime DEFAULT now(), + views UInt32 DEFAULT 0, + likes UInt32 DEFAULT 0, + comments UInt32 DEFAULT 0, + shares UInt32 DEFAULT 0, + PRIMARY KEY (post_id) +) ENGINE = MergeTree(); --- 创建物化视图:情感分析 -CREATE MATERIALIZED VIEW mv_sentiment_analysis ENGINE = SummingMergeTree() PARTITION BY toYYYYMM(date) +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 - (sentiment, date) AS -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; \ No newline at end of file + (timestamp) \ No newline at end of file diff --git a/backend/db/sql/clickhouse/sync_logs.sql b/backend/db/sql/clickhouse/sync_logs.sql deleted file mode 100644 index 19e3a0a..0000000 --- a/backend/db/sql/clickhouse/sync_logs.sql +++ /dev/null @@ -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) \ No newline at end of file