#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ mysql_019 ground truth: 传感器事件宽表关联用户组帖子分类圈组维度 Task: Join sensor events with groups_users, posts+categories, and groups to produce a posts event detail wide table. """ import pymysql import sys DB_NAME = "internal_platform_db" INPUT_TABLE_SENSORS = "dwd_knowledge_base_sensors_event_di_mysql_019" INPUT_TABLE_GROUPS_USERS = "dwv_knowledge_base_groups_users_df_mysql_019" INPUT_TABLE_POSTS = "dwv_kb_posts_df_mysql_019" INPUT_TABLE_CADEPT_TORIES = "dwv_kb_categories_df_mysql_019" INPUT_TABLE_GROUPS = "dwv_knowledge_base_groups_df_mysql_019" OUTPUT_TABLE = "dwd_knowledge_base_k_bar_posts_event_di_cand_mysql_019" MYSQL_CONFIG = { "host": "localhost", "port": 3306, "user": "root", "password": "root123", "charset": "utf8mb4", } gt_sql = f""" INSERT INTO {DB_NAME}.{OUTPUT_TABLE} (event, distinct_id, appname, user_id, event_time, receive_time, os, track_signup_original_id, author_nick, platform_type, target_type, target_id, ip, post_id, show_groups, group_id, source_page, source_module, operation_type, pc_or_mobile, is_group_member, post_authorship, posts_category_id, posts_category_name, year, month, day) SELECT event, distinct_id, appname, user_id, event_time, receive_time, os, track_signup_original_id, author_nick, platform_type, target_type, target_id, ip, post_id, show_groups, IFNULL(t4_group_id, group_id) AS group_id, source_page, source_module, operation_type, pc_or_mobile, IF(t2.nick IS NULL, 0, 1) AS is_group_member, post_authorship, posts_category_id, posts_category_name, '2026' AS year, '06' AS month, '08' AS day FROM ( SELECT event, distinct_id, appname, user_id, time AS event_time, receive_time, os, track_signup_original_id, author_nick, platform_type, target_type, target_id, ip, post_id, show_groups, IFNULL(show_groups, group_id) AS group_id, source_page, source_module, operation_type, IF(LOWER(platform_type) IN ('ios', 'android', 'h5'), 'mobile', 'pc') AS pc_or_mobile FROM {DB_NAME}.{INPUT_TABLE_SENSORS} WHERE CONCAT(year, month, day) = '20260608' AND LOWER(event) IN ('postdetailview', 'postdigg', 'postbooknowledge_baseark', 'postrecommend', 'commentsend', 'postcomment') AND (group_id IS NOT NULL OR show_groups IS NOT NULL) AND post_id IS NOT NULL AND post_id != '' ) t1 LEFT JOIN ( SELECT nick, group_id AS t2_group_id FROM {DB_NAME}.{INPUT_TABLE_GROUPS_USERS} WHERE CONCAT(year, month, day) = '20260608' AND enabled = 1 ) t2 ON t1.distinct_id = t2.nick AND t1.group_id COLLATE utf8mb4_unicode_ci = CONVERT(t2.t2_group_id, CHAR) COLLATE utf8mb4_unicode_ci LEFT JOIN ( SELECT id AS t3_id, authorship AS post_authorship, category_id AS posts_category_id, category_name AS posts_category_name FROM ( SELECT id, authorship, category_id FROM {DB_NAME}.{INPUT_TABLE_POSTS} WHERE CONCAT(year, month, day) = '20260608' ) tt1 LEFT JOIN ( SELECT id AS tt2_id, name AS category_name FROM {DB_NAME}.{INPUT_TABLE_CADEPT_TORIES} WHERE CONCAT(year, month, day) = '20260608' ) tt2 ON tt1.category_id = tt2.tt2_id ) t3 ON t1.post_id COLLATE utf8mb4_unicode_ci = CONVERT(t3.t3_id, CHAR) COLLATE utf8mb4_unicode_ci LEFT JOIN ( SELECT id AS t4_group_id, code AS t4_group_code FROM {DB_NAME}.{INPUT_TABLE_GROUPS} WHERE CONCAT(year, month, day) = '20260608' ) t4 ON t1.group_id = t4.t4_group_code """ def main(): conn = pymysql.connect(**MYSQL_CONFIG) try: with conn.cursor() as cur: # Ensure output table exists cur.execute(f""" CREATE TABLE IF NOT EXISTS {DB_NAME}.{OUTPUT_TABLE} ( event VARCHAR(256) DEFAULT NULL, distinct_id VARCHAR(256) DEFAULT NULL, appname VARCHAR(256) DEFAULT NULL, user_id VARCHAR(256) DEFAULT NULL, event_time VARCHAR(256) DEFAULT NULL, receive_time VARCHAR(256) DEFAULT NULL, os VARCHAR(256) DEFAULT NULL, track_signup_original_id VARCHAR(256) DEFAULT NULL, author_nick VARCHAR(256) DEFAULT NULL, platform_type VARCHAR(256) DEFAULT NULL, target_type VARCHAR(256) DEFAULT NULL, target_id VARCHAR(256) DEFAULT NULL, ip VARCHAR(256) DEFAULT NULL, post_id VARCHAR(256) DEFAULT NULL, show_groups VARCHAR(256) DEFAULT NULL, group_id VARCHAR(256) DEFAULT NULL, source_page VARCHAR(256) DEFAULT NULL, source_module VARCHAR(256) DEFAULT NULL, operation_type VARCHAR(256) DEFAULT NULL, pc_or_mobile VARCHAR(256) DEFAULT NULL, is_group_member INT DEFAULT NULL, post_authorship VARCHAR(256) DEFAULT NULL, posts_category_id BIGINT DEFAULT NULL, posts_category_name VARCHAR(256) DEFAULT NULL, year VARCHAR(256) DEFAULT NULL, month VARCHAR(256) DEFAULT NULL, day VARCHAR(256) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 """) # Truncate + insert cur.execute(f"TRUNCATE TABLE {DB_NAME}.{OUTPUT_TABLE}") cur.execute(gt_sql) conn.commit() print("mysql_019 ground_truth done: rows written to output table") except Exception as e: print(f"ground_truth error: {e}", file=sys.stderr) conn.rollback() sys.exit(1) finally: conn.close() if __name__ == "__main__": main()