最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python 獲取 datax 執(zhí)行結(jié)果保存到數(shù)據(jù)庫(kù)的方法

 更新時(shí)間:2019年07月11日 10:30:49   作者:薛定諤的DBA  
今天小編就為大家分享一篇Python 獲取 datax 執(zhí)行結(jié)果保存到數(shù)據(jù)庫(kù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

執(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)文章

  • Pandas分組與排序的實(shí)現(xiàn)

    Pandas分組與排序的實(shí)現(xiàn)

    這篇文章主要介紹了Pandas分組與排序的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 解決安裝python3.7.4報(bào)錯(cuò)Can''''t connect to HTTPS URL because the SSL module is not available

    解決安裝python3.7.4報(bào)錯(cuò)Can''''t connect to HTTPS URL because the S

    這篇文章主要介紹了解決安裝python3.7.4報(bào)錯(cuò)Can't connect to HTTPS URL because the SSL module is not available,本文給大家簡(jiǎn)單分析了錯(cuò)誤原因,給出了解決方法,需要的朋友可以參考下
    2019-07-07
  • Python Pycharm虛擬下百度飛漿PaddleX安裝報(bào)錯(cuò)問(wèn)題及處理方法(親測(cè)100%有效)

    Python Pycharm虛擬下百度飛漿PaddleX安裝報(bào)錯(cuò)問(wèn)題及處理方法(親測(cè)100%有效)

    最近很多朋友給小編留言在安裝PaddleX的時(shí)候總是出現(xiàn)各種奇葩問(wèn)題,不知道該怎么處理,今天小編通過(guò)本文給大家介紹下Python Pycharm虛擬下百度飛漿PaddleX安裝報(bào)錯(cuò)問(wèn)題及處理方法,真的有效,遇到同樣問(wèn)題的朋友快來(lái)參考下吧
    2021-05-05
  • pytorch?tensor合并與分割方式

    pytorch?tensor合并與分割方式

    這篇文章主要介紹了pytorch?tensor合并與分割方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python實(shí)現(xiàn)pdf文檔轉(zhuǎn)txt的方法示例

    Python實(shí)現(xiàn)pdf文檔轉(zhuǎn)txt的方法示例

    這篇文章主要介紹了Python實(shí)現(xiàn)pdf文檔轉(zhuǎn)txt的方法,結(jié)合實(shí)例形式分析了Python基于第三方庫(kù)pdfminier實(shí)現(xiàn)針對(duì)pdf格式文檔的讀取、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • Python爬蟲(chóng)基礎(chǔ)之爬蟲(chóng)的分類知識(shí)總結(jié)

    Python爬蟲(chóng)基礎(chǔ)之爬蟲(chóng)的分類知識(shí)總結(jié)

    來(lái)給大家講python爬蟲(chóng)的基礎(chǔ)啦,首先我們從爬蟲(chóng)的分類開(kāi)始講起,下文有非常詳細(xì)的知識(shí)總結(jié),對(duì)正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • python處理寫(xiě)入數(shù)據(jù)代碼講解

    python處理寫(xiě)入數(shù)據(jù)代碼講解

    在本篇文章里小編給大家整理的是一篇關(guān)于python處理寫(xiě)入數(shù)據(jù)代碼講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2020-10-10
  • Python基于jieba, wordcloud庫(kù)生成中文詞云

    Python基于jieba, wordcloud庫(kù)生成中文詞云

    這篇文章主要介紹了Python基于jieba, wordcloud庫(kù)生成中文詞云,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • python開(kāi)發(fā)之str.format()用法實(shí)例分析

    python開(kāi)發(fā)之str.format()用法實(shí)例分析

    這篇文章主要介紹了python開(kāi)發(fā)之str.format()用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了str.format()函數(shù)的功能,使用方法與相關(guān)注意事項(xiàng),代碼包含詳盡的注釋說(shuō)明,需要的朋友可以參考下
    2016-02-02
  • wxPython實(shí)現(xiàn)分隔窗口

    wxPython實(shí)現(xiàn)分隔窗口

    這篇文章主要為大家詳細(xì)介紹了wxPython實(shí)現(xiàn)分隔窗口,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11

最新評(píng)論

尉犁县| 黄龙县| 上犹县| 南郑县| 象州县| 遵化市| 循化| 石狮市| 页游| 垫江县| 汕尾市| 泰和县| 乌兰察布市| 乌拉特前旗| 松阳县| 丹东市| 宝鸡市| 西林县| 资兴市| 昌平区| 广昌县| 珲春市| 宣武区| 百色市| 余姚市| 名山县| 深泽县| 福建省| 桃源县| 巨鹿县| 沂南县| 尼玛县| 延长县| 南投市| 鱼台县| 康乐县| 霍州市| 故城县| 临猗县| 会宁县| 沭阳县|