#-*- coding: utf-8#File : start.py#Author : baoshan
importjsonimportpymysqlimportcx_Oracleimportpandas as pddefmain():
dataSum=[]
connInfo= "connInfo.json" #é ç½®æ件å称
connFile= open(connInfo, 'r', encoding='utf8')
connRecords= connFile.read(102400) #ä¸æ¬¡è¯»åå¤ä¸ªåè
connRecordsjs =json.loads(connRecords)for single inconnRecordsjs:if "mysql" == single.get("dbtype"):
conn= pymysql.connect(host=single.get("host"), port=single.get("port"), user=single.get("user"),
passwd=single.get("passwd"), charset=single.get("charset"))if "gongxiangwangzhan" == single.get("source", "0"): #å ±äº«ç½ç« å ¬å®å±ãæ°æ¿å±ãèåå¸åå±åæ¹é©å§åä¼ å®å¶
sql = "select table_schema as 'æ°æ®åº',"\"table_name as 'æ°æ®è¡¨',"\"TABLE_COMMENT as '表注é',"\"round(data_length/1024/1024,2) as 'æ°æ®å¤§å°(M)',"\"round(index_length/1024/1024,2) as 'ç´¢å¼å¤§å°(M)',"\"TABLE_ROWS as 'è¡æ°'"\"from information_schema.tables"\"where TABLE_SCHEMA in ('"+single.get("dbschema")+"')"\"AND TABLE_ROWS > 0"\"and table_name in"+single.get("selectkeystr")+""
else:
sql= "select"\"table_schema as 'æ°æ®åº',"\"table_name as 'æ°æ®è¡¨',"\"TABLE_COMMENT as '表注é',"\"round(data_length/1024/1024,2) as 'æ°æ®å¤§å°(M)',"\"round(index_length/1024/1024,2) as 'ç´¢å¼å¤§å°(M)',"\"TABLE_ROWS as 'è¡æ°'"\"from information_schema.tables"\"where TABLE_SCHEMA in ('"+single.get("dbschema")+"')"\"and (table_name"+single.get("selectstr")+"'"+single.get("selectkeystr")+"')"\"and TABLE_ROWS > 0"df=pd.read_sql(sql, conn)print(single.get("key"), str(df['è¡æ°'].sum()))
dataSum.append(df['è¡æ°'].sum())
conn.close()elif "oracle" == single.get("dbtype"):if "table" == single.get("selecttype"):
sql= "select owner as owner,"\"table_name as table_name,"\"tablespace_name as tablespace_name,"\"num_rows as num_rows"\"from all_tables"\"where num_rows > 0"\"and table_name like '"+single.get("selectkeystr")+"'"\"order by num_rows desc"
elif "database" == single.get("selecttype"): #å ±äº«ç½ç«-oracle-å·¥åå± å®å¶
sql = "select owner as owner,"\"table_name as table_name,"\"tablespace_name as tablespace_name,"\"num_rows as num_rows"\"from all_tables"\"where num_rows > 0"\"and tablespace_name in('"+single.get("dbschema")+"')"\"order by num_rows desc"db= cx_Oracle.connect(single.get("connstr"), encoding='utf-8')
cursor=db.cursor()
cursor.execute(sql)
rs=cursor.fetchall()
df=pd.DataFrame(rs)print(single.get("key"), str(df[3].sum()))
dataSum.append(df[3].sum())
cursor.close()
db.close()elif "sqlserver" == single.get("dbtype"):print(single.get("key"), '55568045')
dataSum.append(55568045)#"SELECT A.NAME ,B.ROWS FROM sysobjects A JOIN sysindexes B ON A.id = B.id WHERE A.xtype = 'U' AND B.indid IN(0,1) and b.rows >0 ORDER BY B.ROWS DESC"
else:print("please give right database type.")
connFile.close()print('-'*30)print("æ°æ®éæ»è®¡ï¼", str(sum(dataSum)))if __name__ == '__main__':print("***ä¸æ¬¡æ§ç»è®¡ææ对æ¥æ°æ®çå§åå±ï¼åå ¶å¯¹åºçæ°æ®(æ¡æ°)***")
main()