Python 获取 datax 执行结果保存到数据库的方法

作者:薛定谔的DBA 时间:2024-01-26 23:32:08 

执行 datax 作业,创建执行文件,在 crontab 中每天1点(下面有关系)执行:

其中 job_start 及 job_finish 这两行记录是自己添加的,为了方便识别出哪张表。


#!/bin/bash
source /etc/profile
user1="root"
pass1="pwd"
user2="root"
pass2="pwd"
job_path="/opt/datax/job/"

jobfile=(
job_table_a.json
job_table_b.json
)

for filename in ${jobfile[@]}
do
echo "job_start: "`date "+%Y-%m-%d %H:%M:%S"`" ${filename}"
python /opt/datax/bin/datax.py -p "-Duser1=${user1} -Dpass1=${pass1} -Duser2=${user2} -Dpass2=${pass2}" ${job_path}${filename}
echo "job_finish: "`date "+%Y-%m-%d %H:%M:%S"`" ${filename}"
done

# 0 1 * * * /opt/datax/job/dc_to_ods_incr.sh >> /opt/datax/job/log/dc_to_ods_incr_$(date +\%Y\%m\%d_\%H\%M\%S).log 2>&1
# egrep '任务|速度|总数|job_start|job_finish' /opt/datax/job/log/

datax 执行日志:


job_start: 2018-08-08 01:13:28 job_table_a.json
任务启动时刻          : 2018-08-08 01:13:28
任务结束时刻          : 2018-08-08 01:14:49
任务总计耗时          :         81s
任务平均流量          :     192.82KB/s
记录写入速度          :      1998rec/s
读出记录总数          :       159916
读写失败总数          :          0
job_finish: 2018-08-08 01:14:49 job_table_a.json
job_start: 2018-08-08 01:14:49 job_table_b.json
任务启动时刻          : 2018-08-08 01:14:50
任务结束时刻          : 2018-08-08 01:15:01
任务总计耗时          :         11s
任务平均流量          :        0B/s
记录写入速度          :       0rec/s
读出记录总数          :          0
读写失败总数          :          0
job_finish: 2018-08-08 01:15:01 job_table_b.json

接下来读取这些信息保存到数据库,在数据库中创建表:


CREATE TABLE `datax_job_result` (
`log_file` varchar(200) DEFAULT NULL,
`job_file` varchar(200) DEFAULT NULL,
`start_time` datetime DEFAULT NULL,
`end_time` datetime DEFAULT NULL,
`seconds` int(11) DEFAULT NULL,
`traffic` varchar(50) DEFAULT NULL,
`write_speed` varchar(50) DEFAULT NULL,
`read_record` int(11) DEFAULT NULL,
`failed_record` int(11) DEFAULT NULL,
`job_start` varchar(200) DEFAULT NULL,
`job_finish` varchar(200) DEFAULT NULL,
`insert_time` datetime DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

定时执行以下文件,因为 datax 作业 1 点执行,为了获取一天内最新生产的日志,脚本中取 82800内生产的日志文件,及23 小时内生产的那个最新日志。所以一天内任何时间执行都可以。此文件也是定时每天执行(判断 datax 作业完成后执行)


#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 0 5 * * * source /etc/profile && /usr/bin/python2.7 /opt/datax/job/save_log_to_db.py > /dev/null 2>&1

import re
import os
import sqlalchemy
import pandas as pd
import datetime as dt

def save_to_db(df):
engine = sqlalchemy.create_engine("mysql+pymysql://root:pwd@localhost:3306/test", encoding="utf-8")
df.to_sql("datax_job_result", engine, index=False, if_exists='append')

def get_the_latest_file(path):
t0 = dt.datetime.utcfromtimestamp(0)
d2 = (dt.datetime.now() - t0).total_seconds()
d1 = d2 - 82800
for (dirpath, dirnames, filenames) in os.walk(path):
for filename in sorted(filenames, reverse = True):
if filename.endswith(".log"):
f = os.path.join(dirpath,filename)
ctime = os.stat(f)[-1]
if ctime>=d1 and ctime <=d2:
return f

def get_job_result_from_logfile(path):
result = pd.DataFrame(columns=['log_file','job_file','start_time','end_time','seconds','traffic','write_speed','read_record','failed_record','job_start','job_finish'])
log_file = get_the_latest_file(path)
index = 0
content = open(log_file, "r")
for line in content:
result.loc[index, 'log_file'] = log_file
if re.compile(r'job_start').match(line):
result.loc[index, 'job_file'] = line.split(' ')[4].strip()
result.loc[index, 'job_start'] = line,
elif re.compile(r'任务启动时刻').match(line):
result.loc[index, 'start_time'] = line.split('刻')[1].strip().split(' ')[1].strip() + ' ' + line.split('刻')[1].strip().split(' ')[2].strip()
elif re.compile(r'任务结束时刻').match(line):
result.loc[index, 'end_time'] = line.split('刻')[1].strip().split(' ')[1].strip() + ' ' + line.split('刻')[1].strip().split(' ')[2].strip()
elif re.compile(r'任务总计耗时').match(line):
result.loc[index, 'seconds'] = line.split(':')[1].strip().replace('s','')
elif re.compile(r'任务平均流量').match(line):
result.loc[index, 'traffic'] = line.split(':')[1].strip()
elif re.compile(r'记录写入速度').match(line):
result.loc[index, 'write_speed'] = line.split(':')[1].strip()
elif re.compile(r'读出记录总数').match(line):
result.loc[index, 'read_record'] = line.split(':')[1].strip()
elif re.compile(r'读写失败总数').match(line):
result.loc[index, 'failed_record'] = line.split(':')[1].strip()
elif re.compile(r'job_finish').match(line):
result.loc[index, 'job_finish'] = line,
index = index + 1
else:
pass
save_to_db(result)

get_job_result_from_logfile("/opt/datax/job/log")

来源:https://blog.csdn.net/kk185800961/article/details/81506457

标签:Python,datax,结果,保存,数据库
0
投稿

猜你喜欢

  • 玩转表单之花样表单

    2011-04-25 19:17:00
  • vue实现鼠标滑动展示tab栏切换

    2023-07-02 16:38:05
  • 跟老齐学Python之开始真正编程

    2021-06-26 21:42:22
  • keras 特征图可视化实例(中间层)

    2021-12-05 22:54:46
  • Python实现双色球号码随机生成

    2023-10-24 23:56:22
  • Python数据分析与处理(一)--北京高考分数线统计分析

    2022-08-13 02:11:05
  • ASP函数过滤数组重复数据代码

    2010-01-02 20:36:00
  • 解析SQL语句中Replace INTO与INSERT INTO的不同之处

    2024-01-23 05:57:18
  • Python获取一个用户名的组ID过程解析

    2021-09-04 15:40:05
  • 浅谈Vue render函数在ElementUi中的应用

    2024-05-09 10:52:26
  • 深入探究Go语言从反射到元编程的实践与探讨

    2024-05-22 10:28:50
  • 详解MySQL的Seconds_Behind_Master

    2024-01-18 04:58:00
  • JSP学生信息管理系统

    2024-03-20 22:28:27
  • vue封装一个弹幕组件详解

    2024-05-09 15:28:38
  • Mysql5.7定时备份的实现

    2024-01-22 13:08:27
  • python实现密码验证合格程序的思路详解

    2022-12-10 05:07:38
  • Softmax函数原理及Python实现过程解析

    2022-12-15 02:18:24
  • 教你利用python的matplotlib(pyplot)绘制折线图和柱状图

    2022-02-25 17:30:49
  • SQL Server下几个危险的扩展存储过程

    2024-01-18 06:49:25
  • 使用vue制作探探滑动堆叠组件的实例代码

    2024-04-28 09:25:26
  • asp之家 网络编程 m.aspxhome.com