Python 獲取 datax 執(zhí)行結(jié)果保存到數(shù)據(jù)庫(kù)的方法
執(zhí)行 datax 作業(yè),創(chuàng)建執(zhí)行文件,在 crontab 中每天1點(diǎn)(下面有關(guān)系)執(zhí)行:
其中 job_start 及 job_finish 這兩行記錄是自己添加的,為了方便識(shí)別出哪張表。
#!/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 '任務(wù)|速度|總數(shù)|job_start|job_finish' /opt/datax/job/log/
datax 執(zhí)行日志:
job_start: 2018-08-08 01:13:28 job_table_a.json 任務(wù)啟動(dòng)時(shí)刻 : 2018-08-08 01:13:28 任務(wù)結(jié)束時(shí)刻 : 2018-08-08 01:14:49 任務(wù)總計(jì)耗時(shí) : 81s 任務(wù)平均流量 : 192.82KB/s 記錄寫(xiě)入速度 : 1998rec/s 讀出記錄總數(shù) : 159916 讀寫(xiě)失敗總數(shù) : 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 任務(wù)啟動(dòng)時(shí)刻 : 2018-08-08 01:14:50 任務(wù)結(jié)束時(shí)刻 : 2018-08-08 01:15:01 任務(wù)總計(jì)耗時(shí) : 11s 任務(wù)平均流量 : 0B/s 記錄寫(xiě)入速度 : 0rec/s 讀出記錄總數(shù) : 0 讀寫(xiě)失敗總數(shù) : 0 job_finish: 2018-08-08 01:15:01 job_table_b.json
接下來(lái)讀取這些信息保存到數(shù)據(jù)庫(kù),在數(shù)據(jù)庫(kù)中創(chuàng)建表:
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;
定時(shí)執(zhí)行以下文件,因?yàn)?datax 作業(yè) 1 點(diǎn)執(zhí)行,為了獲取一天內(nèi)最新生產(chǎn)的日志,腳本中取 82800內(nèi)生產(chǎn)的日志文件,及23 小時(shí)內(nèi)生產(chǎn)的那個(gè)最新日志。所以一天內(nèi)任何時(shí)間執(zhí)行都可以。此文件也是定時(shí)每天執(zhí)行(判斷 datax 作業(yè)完成后執(zhí)行)
#!/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'任務(wù)啟動(dòng)時(shí)刻').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'任務(wù)結(jié)束時(shí)刻').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'任務(wù)總計(jì)耗時(shí)').match(line): result.loc[index, 'seconds'] = line.split(':')[1].strip().replace('s','') elif re.compile(r'任務(wù)平均流量').match(line): result.loc[index, 'traffic'] = line.split(':')[1].strip() elif re.compile(r'記錄寫(xiě)入速度').match(line): result.loc[index, 'write_speed'] = line.split(':')[1].strip() elif re.compile(r'讀出記錄總數(shù)').match(line): result.loc[index, 'read_record'] = line.split(':')[1].strip() elif re.compile(r'讀寫(xiě)失敗總數(shù)').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")
以上這篇Python 獲取 datax 執(zhí)行結(jié)果保存到數(shù)據(jù)庫(kù)的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python進(jìn)階學(xué)習(xí)實(shí)時(shí)目標(biāo)跟蹤示例詳解
這篇文章主要為大家介紹了python進(jìn)階學(xué)習(xí)實(shí)時(shí)目標(biāo)跟蹤示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03PyTorch 實(shí)現(xiàn)L2正則化以及Dropout的操作
這篇文章主要介紹了PyTorch 實(shí)現(xiàn)L2正則化以及Dropout的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05Pytorch實(shí)現(xiàn)將模型的所有參數(shù)的梯度清0
這篇文章主要介紹了Pytorch實(shí)現(xiàn)將模型的所有參數(shù)的梯度清0,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06Python使用ThreadPoolExecutor一次開(kāi)啟多個(gè)線程
通過(guò)使用ThreadPoolExecutor,您可以同時(shí)開(kāi)啟多個(gè)線程,從而提高程序的并發(fā)性能,本文就來(lái)介紹一下Python使用ThreadPoolExecutor一次開(kāi)啟多個(gè)線程,感興趣的可以了解一下2023-11-11python flask自定義404錯(cuò)誤頁(yè)面方式
這篇文章主要介紹了python flask自定義404錯(cuò)誤頁(yè)面方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12python下os模塊強(qiáng)大的重命名方法renames詳解
這篇文章主要介紹了python下os模塊強(qiáng)大的重命名方法renames詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03pymssql數(shù)據(jù)庫(kù)操作MSSQL2005實(shí)例分析
這篇文章主要介紹了pymssql數(shù)據(jù)庫(kù)操作MSSQL2005的方法,可實(shí)現(xiàn)基本的連接、查詢、插入、更新及調(diào)用存儲(chǔ)過(guò)程等功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05win10+anaconda安裝yolov5的方法及問(wèn)題解決方案
這篇文章主要介紹了win10+anaconda安裝yolov5的方法及問(wèn)題解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04