You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.5 KiB

5 months ago
import logging
5 months ago
from connectors.mysql_connector import MySQLConnector
from connectors.clickhouse_connector import ClickHouseConnector
from mappers.data_mapper import DataMapper
5 months ago
from services.sync_service import SyncService, SyncType
5 months ago
from utils.logger import configure_logger
5 months ago
from apscheduler.schedulers.background import BackgroundScheduler
import time
5 months ago
from config.db_config import MYSQL_CONFIG, CH_CONFIG, print_config
5 months ago
# 要同步的表
tables = ['t_station', 't_equipment', 't_equipment_charge_order']
5 months ago
5 months ago
# 配置日志记录
5 months ago
logger = configure_logger()
5 months ago
# 添加文件处理器
file_handler = logging.FileHandler('sync.log')
file_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
5 months ago
5 months ago
# 同步函数
def sync(table, sync_type):
logger.info(f"开始同步表: {table}, 同步类型: {sync_type}")
5 months ago
5 months ago
# 初始化组件
mysql_conn = MySQLConnector(MYSQL_CONFIG)
ch_conn = ClickHouseConnector(CH_CONFIG)
5 months ago
if sync_type == SyncType.FULL:
5 months ago
# 清空目标表
5 months ago
ch_conn.connect().execute(f"TRUNCATE TABLE {table}")
logger.info(f"已清空目标表: {table}")
5 months ago
ch_conn.disconnect()
5 months ago
# 创建数据映射器
mapper = DataMapper(mysql_conn, table)
5 months ago
# 创建同步服务
5 months ago
service = SyncService(mysql_conn=mysql_conn, ch_conn=ch_conn, mapper=mapper)
5 months ago
try:
# 同步数据
5 months ago
service.sync_data(batch_size=5000, table=table, sync_type=sync_type) # 确保这里传递的是 SyncType 枚举
logger.info(f"同步成功: {table}")
5 months ago
except Exception as e:
logger.error(f"同步失败: {str(e)}", exc_info=True)
finally:
ch_conn.disconnect()
5 months ago
5 months ago
# 全量同步
5 months ago
def full_sync():
for table in tables:
sync(table, SyncType.FULL)
5 months ago
# 增量同步
5 months ago
def incremental_sync():
for table in tables:
sync(table, SyncType.INCREMENTAL)
5 months ago
if __name__ == "__main__":
5 months ago
# 输出配置信息
5 months ago
print_config()
5 months ago
# 先执行一次增量同步
incremental_sync()
# 创建调度器
scheduler = BackgroundScheduler()
# 每天2点执行全量同步
scheduler.add_job(full_sync, 'cron', hour=2, minute=0)
# 每小时执行增量同步排除2点
scheduler.add_job(incremental_sync, 'cron', hour='*', minute=0, id='incremental_sync_job', replace_existing=True)
# 启动调度器
scheduler.start()
try:
# 保持主线程运行
while True:
time.sleep(1)
except (KeyboardInterrupt, SystemExit):
5 months ago
scheduler.shutdown()