Commit b1a34ade by 江名洋

初始化数据

0 parents
No preview for this file type
-- 检查所有表的字段注释覆盖率
SELECT
t.TABLE_NAME,
t.TABLE_COMMENT,
COUNT(c.COLUMN_NAME) AS total_cols,
SUM(CASE WHEN c.COLUMN_COMMENT IS NULL OR c.COLUMN_COMMENT = '' THEN 1 ELSE 0 END) AS no_comment_cols,
SUM(CASE WHEN c.COLUMN_COMMENT IS NOT NULL AND c.COLUMN_COMMENT != '' THEN 1 ELSE 0 END) AS has_comment_cols,
ROUND(SUM(CASE WHEN c.COLUMN_COMMENT IS NOT NULL AND c.COLUMN_COMMENT != '' THEN 1 ELSE 0 END) * 100.0 / COUNT(c.COLUMN_NAME), 1) AS comment_pct
FROM information_schema.tables t
JOIN information_schema.columns c ON t.TABLE_SCHEMA = c.TABLE_SCHEMA AND t.TABLE_NAME = c.TABLE_NAME
WHERE t.TABLE_SCHEMA = 'test_excel'
AND (t.TABLE_NAME LIKE 'ods_%' OR t.TABLE_NAME LIKE 'dws_%' OR t.TABLE_NAME LIKE 'dwm_%' OR t.TABLE_NAME LIKE 'dwa_%')
GROUP BY t.TABLE_NAME, t.TABLE_COMMENT
ORDER BY comment_pct ASC, t.TABLE_NAME;
# -*- coding: utf-8 -*-
"""验证: 源表中是否有 updated_date != created_date 的记录(说明被修改过,且updated_date有更新)"""
import pymysql
from pymysql.cursors import DictCursor
SOURCE_CONFIG = {
'host': '192.168.5.23', 'port': 3306,
'user': 'user_jiangmy', 'password': 'VMziLJun',
'database': 'dj_smartcarlife', 'charset': 'utf8mb4',
'cursorclass': DictCursor,
}
TABLES = [
'dj_car_life_staff_daily_report_huawei',
'dj_car_life_staff_daily_report',
'dj_car_life_merchant_daily_report_huawei',
'dj_car_life_staff_info',
'dj_car_life_goods_order',
'dj_car_life_goods_sub_order',
'dj_car_life_goods_order_customer',
'dj_car_life_service_merchant',
]
conn = pymysql.connect(**SOURCE_CONFIG)
for table in TABLES:
with conn.cursor() as cur:
# 查: 有多少条 updated_date > created_date (被修改过的)
cur.execute(f"""
SELECT
COUNT(*) AS total,
SUM(CASE WHEN updated_date > created_date THEN 1 ELSE 0 END) AS modified,
SUM(CASE WHEN updated_date = created_date THEN 1 ELSE 0 END) AS unmodified,
MAX(updated_date) AS max_updated
FROM `{table}`
WHERE created_date IS NOT NULL AND updated_date IS NOT NULL
""")
row = cur.fetchone()
print(f"{table}")
print(f" total={row['total']:,} modified={row['modified']:,} unmodified={row['unmodified']:,} max_updated={row['max_updated']}")
print()
conn.close()
# -*- coding: utf-8 -*-
"""检查源表 updated_date 列是否有 ON UPDATE CURRENT_TIMESTAMP"""
import pymysql
from pymysql.cursors import DictCursor
SOURCE_CONFIG = {
'host': '192.168.5.23', 'port': 3306,
'user': 'user_jiangmy', 'password': 'VMziLJun',
'database': 'dj_smartcarlife', 'charset': 'utf8mb4',
'cursorclass': DictCursor,
}
TABLES = [
'dj_car_life_staff_daily_report_huawei',
'dj_car_life_staff_daily_report',
'dj_car_life_merchant_daily_report_huawei',
'dj_car_life_goods_order',
'dj_car_life_goods_sub_order',
'dj_car_life_goods_order_customer',
'dj_car_life_service_merchant',
'dj_car_life_staff_info',
]
conn = pymysql.connect(**SOURCE_CONFIG)
for table in TABLES:
with conn.cursor() as cur:
cur.execute(f"SHOW COLUMNS FROM `{table}` LIKE 'updated_date'")
col = cur.fetchone()
if col:
extra = col.get('Extra', '')
print(f"{table}")
print(f" updated_date Extra: {extra or '(empty)'}")
else:
print(f"{table}: no updated_date column")
with conn.cursor() as cur:
cur.execute(f"SHOW COLUMNS FROM `{table}` LIKE 'created_date'")
col = cur.fetchone()
if col:
extra = col.get('Extra', '')
print(f" created_date Extra: {extra or '(empty)'}")
print()
conn.close()
-- 检查源表 updated_date 列是否有 ON UPDATE CURRENT_TIMESTAMP
-- 如果没有, 说明依赖应用层更新, 需要确认应用是否每次修改都set updated_date
SHOW CREATE TABLE dj_car_life_staff_daily_report_huawei;
This diff could not be displayed because it is too large.
SELECT '=== DWS ===' AS layer, '' AS tbl, '' AS cnt, '' AS min_date, '' AS max_date
UNION ALL SELECT 'dws_staff_daily_wide', '', COUNT(*), MIN(report_date), MAX(report_date) FROM dws_staff_daily_wide
UNION ALL SELECT 'dws_merchant_daily_wide', '', COUNT(*), MIN(report_date), MAX(report_date) FROM dws_merchant_daily_wide
UNION ALL SELECT 'dws_order_detail_wide', '', COUNT(*), MIN(install_date), MAX(install_date) FROM dws_order_detail_wide
UNION ALL SELECT 'dws_merchant_daily_target_wide', '', COUNT(*), MIN(report_date), MAX(report_date) FROM dws_merchant_daily_target_wide
UNION ALL SELECT '=== DWM ===', '', '', '', ''
UNION ALL SELECT 'dwm_staff_touch_daily', '', COUNT(*), MIN(report_date), MAX(report_date) FROM dwm_staff_touch_daily
UNION ALL SELECT 'dwm_staff_transaction_daily', '', COUNT(*), MIN(report_date), MAX(report_date) FROM dwm_staff_transaction_daily
UNION ALL SELECT 'dwm_merchant_touch_daily', '', COUNT(*), MIN(report_date), MAX(report_date) FROM dwm_merchant_touch_daily
UNION ALL SELECT 'dwm_merchant_transaction_daily', '', COUNT(*), MIN(report_date), MAX(report_date) FROM dwm_merchant_transaction_daily
UNION ALL SELECT 'dwm_merchant_delivery_daily', '', COUNT(*), MIN(report_date), MAX(report_date) FROM dwm_merchant_delivery_daily
UNION ALL SELECT 'dwm_battery_analysis_daily', '', COUNT(*), MIN(report_date), MAX(report_date) FROM dwm_battery_analysis_daily
UNION ALL SELECT 'dwm_guarantee_daily', '', COUNT(*), MIN(report_date), MAX(report_date) FROM dwm_guarantee_daily
UNION ALL SELECT 'dwm_store_ranking_daily', '', COUNT(*), MIN(report_date), MAX(report_date) FROM dwm_store_ranking_daily;
# -*- coding: utf-8 -*-
"""
修正 _sync_meta: 将 last_sync_time 设为各ODS表实际的 max(updated_date)
然后跑一次增量同步验证
"""
import pymysql
from pymysql.cursors import DictCursor
TARGET_CONFIG = {
'host': '192.168.2.155', 'port': 3308,
'user': 'user_jiangmy', 'password': 'PYuxMTB1',
'database': 'test_excel', 'charset': 'utf8mb4',
'cursorclass': DictCursor,
}
# ODS表名列表
ODS_TABLES = [
'ods_service_merchant',
'ods_shop_merchant_director_config',
'ods_merchant_brand_model',
'ods_staff_info',
'ods_base_goods',
'ods_goods_info',
'ods_upgrade_goods',
'ods_merchant_goods_info',
'ods_merchant_package_goods_info',
'ods_staff_daily_report_huawei',
'ods_staff_daily_report',
'ods_merchant_daily_report_huawei',
'ods_goods_order',
'ods_goods_sub_order',
'ods_goods_order_customer',
'ods_merchant_deliver_target_config',
'ods_merchant_output_target_config',
'ods_employee_delivery_deal_rate',
]
# ODS表名 -> 源表名映射
SOURCE_MAP = {
'ods_service_merchant': 'dj_car_life_service_merchant',
'ods_shop_merchant_director_config': 'dj_car_life_shop_merchant_director_config',
'ods_merchant_brand_model': 'dj_car_life_merchant_brand_model',
'ods_staff_info': 'dj_car_life_staff_info',
'ods_base_goods': 'dj_car_life_base_goods',
'ods_goods_info': 'dj_car_life_goods_info',
'ods_upgrade_goods': 'dj_car_life_upgrade_goods',
'ods_merchant_goods_info': 'dj_car_life_merchant_goods_info',
'ods_merchant_package_goods_info': 'dj_car_life_merchant_package_goods_info',
'ods_staff_daily_report_huawei': 'dj_car_life_staff_daily_report_huawei',
'ods_staff_daily_report': 'dj_car_life_staff_daily_report',
'ods_merchant_daily_report_huawei': 'dj_car_life_merchant_daily_report_huawei',
'ods_goods_order': 'dj_car_life_goods_order',
'ods_goods_sub_order': 'dj_car_life_goods_sub_order',
'ods_goods_order_customer': 'dj_car_life_goods_order_customer',
'ods_merchant_deliver_target_config': 'dj_car_life_merchant_deliver_target_config',
'ods_merchant_output_target_config': 'dj_car_life_merchant_output_target_config',
'ods_employee_delivery_deal_rate': 'employee_delivery_deal_rate',
}
conn = pymysql.connect(**TARGET_CONFIG)
print(f"{'ODS表名':<45} {'max(updated_date)':<25} {'当前sync_time':<25}")
print("-" * 95)
for ods_table in ODS_TABLES:
source_table = SOURCE_MAP[ods_table]
# 获取ODS表中实际的 max(updated_date)
with conn.cursor() as cur:
cur.execute(f"SELECT MAX(updated_date) AS max_ud FROM `{ods_table}`")
row = cur.fetchone()
max_updated = row['max_ud'] if row else None
# 获取当前 _sync_meta 中的 last_sync_time
with conn.cursor() as cur:
cur.execute(
"SELECT last_sync_time FROM `_sync_meta` WHERE source_table=%s AND target_table=%s",
(source_table, ods_table)
)
row = cur.fetchone()
current_sync = row['last_sync_time'] if row else None
print(f"{ods_table:<45} {str(max_updated):<25} {str(current_sync):<25}")
# 更新 _sync_meta: 将 last_sync_time 设为实际的 max(updated_date)
if max_updated:
with conn.cursor() as cur:
cur.execute("""
INSERT INTO `_sync_meta` (source_table, target_table, last_sync_time, last_sync_rows, sync_mode)
VALUES (%s, %s, %s, 0, 'incremental')
ON DUPLICATE KEY UPDATE last_sync_time = VALUES(last_sync_time), sync_mode = 'incremental'
""", (source_table, ods_table, max_updated))
conn.commit()
print("\n_sync_meta 已修正为各表实际 max(updated_date)")
print("现在可以安全地跑增量同步了")
conn.close()
-- 初始化同步元数据,将所有表的last_sync_time设为当前时间
INSERT INTO _sync_meta (source_table, target_table, last_sync_time, last_sync_rows, sync_mode)
SELECT t.source_table, t.target_table, NOW(), 0, 'full'
FROM (
SELECT 'dj_car_life_service_merchant' AS source_table, 'ods_service_merchant' AS target_table UNION ALL
SELECT 'dj_car_life_shop_merchant_director_config', 'ods_shop_merchant_director_config' UNION ALL
SELECT 'dj_car_life_merchant_brand_model', 'ods_merchant_brand_model' UNION ALL
SELECT 'dj_car_life_staff_info', 'ods_staff_info' UNION ALL
SELECT 'dj_car_life_base_goods', 'ods_base_goods' UNION ALL
SELECT 'dj_car_life_goods_info', 'ods_goods_info' UNION ALL
SELECT 'dj_car_life_upgrade_goods', 'ods_upgrade_goods' UNION ALL
SELECT 'dj_car_life_merchant_goods_info', 'ods_merchant_goods_info' UNION ALL
SELECT 'dj_car_life_merchant_package_goods_info', 'ods_merchant_package_goods_info' UNION ALL
SELECT 'dj_car_life_staff_daily_report_huawei', 'ods_staff_daily_report_huawei' UNION ALL
SELECT 'dj_car_life_staff_daily_report', 'ods_staff_daily_report' UNION ALL
SELECT 'dj_car_life_merchant_daily_report_huawei', 'ods_merchant_daily_report_huawei' UNION ALL
SELECT 'dj_car_life_goods_order', 'ods_goods_order' UNION ALL
SELECT 'dj_car_life_goods_sub_order', 'ods_goods_sub_order' UNION ALL
SELECT 'dj_car_life_goods_order_customer', 'ods_goods_order_customer' UNION ALL
SELECT 'dj_car_life_merchant_deliver_target_config', 'ods_merchant_deliver_target_config' UNION ALL
SELECT 'dj_car_life_merchant_output_target_config', 'ods_merchant_output_target_config' UNION ALL
SELECT 'employee_delivery_deal_rate', 'ods_employee_delivery_deal_rate'
) t
ON DUPLICATE KEY UPDATE last_sync_time = NOW(), sync_mode = 'full';
-- 验证ODS表数据量(确认数据已在)
SELECT 'ods_staff_daily_report_huawei' AS tbl, COUNT(*) AS cnt, MIN(report_date) AS min_date, MAX(report_date) AS max_date FROM ods_staff_daily_report_huawei
UNION ALL SELECT 'ods_merchant_daily_report_huawei', COUNT(*), MIN(report_date), MAX(report_date) FROM ods_merchant_daily_report_huawei
UNION ALL SELECT 'ods_goods_order', COUNT(*), MIN(DATE(install_upgrade_time)), MAX(DATE(install_upgrade_time)) FROM ods_goods_order;
-- ============================================================================
-- ODS 层补充 DDL — 新增2张维度表
-- 数据库: test_excel
-- ============================================================================
-- ============================================================================
-- 新增1. ods_shop_merchant_director_config — 门店负责人配置表
-- 来源: dj_car_life_shop_merchant_director_config
-- 用途: 区域运营/线上运营/飞行督导等人员维度
-- ============================================================================
DROP TABLE IF EXISTS ods_shop_merchant_director_config;
CREATE TABLE ods_shop_merchant_director_config (
id BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键',
merchant_id BIGINT NOT NULL COMMENT '门店ID',
director_type TINYINT NOT NULL COMMENT '负责人类型: 5=1.1线上运营, 6=1.1飞行督导, 7=2.1线上运营, 8=1.1区域运营, 9=2.1区域运营, 13=2.1飞行督导',
director_user_id VARCHAR(64) DEFAULT NULL COMMENT '负责人用户ID',
director_name VARCHAR(64) DEFAULT NULL COMMENT '负责人姓名',
director_phone VARCHAR(32) DEFAULT NULL COMMENT '负责人手机号',
deleted_flag CHAR(1) DEFAULT 'N' COMMENT '删除标记',
created_date DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
updated_date DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (id),
INDEX idx_merchant_type (merchant_id, director_type),
INDEX idx_director_user (director_user_id),
INDEX idx_director_type (director_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='门店负责人配置表-ODS';
-- ============================================================================
-- 新增2. ods_merchant_brand_model — 商户品牌车系配置表
-- 来源: dj_car_life_merchant_brand_model
-- 用途: 品牌/子品牌/车系维度
-- ============================================================================
DROP TABLE IF EXISTS ods_merchant_brand_model;
CREATE TABLE ods_merchant_brand_model (
id BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键',
merchant_id BIGINT NOT NULL COMMENT '商户ID',
merchant_name VARCHAR(128) DEFAULT NULL COMMENT '商户名称',
brand_id INT DEFAULT NULL COMMENT '品牌ID(一级)',
brand_name VARCHAR(64) DEFAULT NULL COMMENT '品牌名称',
child_id INT DEFAULT NULL COMMENT '子品牌ID(二级)',
child_name VARCHAR(64) DEFAULT NULL COMMENT '子品牌名称',
series_id INT DEFAULT NULL COMMENT '车系ID',
series_name VARCHAR(64) DEFAULT NULL COMMENT '车系名称',
model_type INT DEFAULT NULL COMMENT '车型分类: 0=无, 1=资源车, 2=畅销车, 3=普通车, 4=库存车',
deleted_flag CHAR(1) DEFAULT 'N' COMMENT '删除标记',
created_date DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
updated_date DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (id),
INDEX idx_merchant (merchant_id),
INDEX idx_brand (brand_id, child_id),
INDEX idx_series (series_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='商户品牌车系配置表-ODS';
-- 清空并重建数据库
DROP DATABASE IF EXISTS test_excel;
CREATE DATABASE test_excel DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE test_excel;
No preview for this file type
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!