"Python" couchbase Audit script __python

Source: Internet
Author: User
Tags couchbase db2 win32
#couchbasescript. py #!/usr/bin/python #-*-CODING:GBK-*-#程序功能 Verify that the key in Couchbase corresponds to the value of the MySQL table from couchbase.bucket import Bucket Import configparser Import mysqldb import time import os #couchbase配置 cp= configparser.configparser () cp.read ("My. Conf ') couchbaseip=cp.get (' Couchbase ', ' IP ') couchbaseport=cp.get (' couchbase ', ' Port ') bucketname=cp.get (' Couchbase ', ' Buckname ') bucketpasswd=cp.get (' Couchbase ', ' buckpasswd ') #print Couchbaseip, BUCKNAME,BUCKPASSWD, Couchbaseport #mysql配置 mysqlip=cp.get (' MySQL ', ' IP ') mysqlport=cp.get (' MySQL ', ' Port ') mysqlusername=cp.get (' MySQL '), ' username ') mysqldbname_tatrade=cp.get (' MySQL ', ' Dbname_tatrade ') mysqlpasswd=cp.get (' MySQL ', ' passwd ') mysqlip1= Cp.get (' MySQL ', ' ip1 ') mysqlport1=cp.get (' MySQL ', ' port1 ') mysqlusername1=cp.get (' MySQL ', ' username1 ') mysqldbname_ Tatrade1=cp.get (' MySQL ', ' dbname_tatrade1 ') mysqlpasswd1=cp.get (' MySQL ', ' passwd1 ') #print mysqlip, Mysqlport, Mysqlusername, Mysqldbname_tatrade, mysqlpasswd # open Couchbase couchbaseconnect= 'couchbase://' +couchbaseip+ ': ' + couchbaseport + '/' + buckname #print couchbaseconnect #c = Bucket ("couchbase://  10.2.130.78:8091/assign ", password= ' assign ') Try:c = Bucket (Couchbaseconnect, password=buckpasswd) except:print
    "Couchbase Connection failed with the following connection information:" Print couchbaseconnect print buchket password is:%s% buckpasswd exit () # Open database connection try: DB1 = MySQLdb.connect (mysqlip,mysqlusername,mysqlpasswd,mysqldbname_tatrade) DB2 = MySQLdb.connect (mysqlip1,mysqlus ERNAME1,MYSQLPASSWD1,MYSQLDBNAME_TATRADE1) Except:print "database connection failed!" The connection information is as follows: "Print" ip=%s, port=%s, username=%s, DBN ame=%s, password=%s "% (Mysqlip, Mysqlport, Mysqlusername, Mysqldbname_tatrade, mysqlpasswd) exit () # using the cursor () method to obtain Fetch action Cursor Cursor1 = db1.cursor () Cursor2 = db2.cursor () print query begins! "sql=" select D_sysdate from Ta_ttainfo t where t.c_t  Acode= ' and t.c_tenantid= ' * ' Cursor1.execute (SQL) Dcdate=cursor1.fetchone () #目录是否存在 s=os.path.exists ("result") if (s = = False): Os.mkdir ("result")

# Open File filename= "result/result_%d.txt"% dcdate f=open (filename, ' W ') f.write ("\ n") F.write (" Account class Check data audit form \ n ") f.write ("--------------------------------------------------- -----------------------------------------------") f.write (" \ n ") f.write (" Database connection information follows: \ n ") f.write (" Database ip=%s, Port =%s,  Username =%s, database =%s, database password =%s \ n "% (Mysqlip, Mysqlport, Mysqlusername, Mysqldbname_tatrade, mysqlpasswd)) f.write (" Database ip=%s, Port =%s, username =%s, database =%s, database password =%s \ n "% (Mysqlip1, Mysqlport1, mysqlusername1, Mysqldbname_tatrade1, mysqlpasswd1)) F.writ E ("Couchbase connection information:%s\n"% couchbaseconnect) f.write ("------------------------------------------------------------ --------------------------------------") f.write (" \ n ") f.write (Check point: Check the Fund account in the confirmation form for couchbase) ############# ############################################## #账户申请couchbase查询 ################################################# #33 # SQL query Statement sql = ' SELECT C_fundacco from Ta_taccoconFirm where d_cdate= (select D_sysdate from Ta_ttainfo t where t.c_tacode= ' a ' and t.c_tenantid= ' * ')
                   and c_businflag= ' c_tacode= ' and c_tenantid= ' and c_status= ' 1 ' f.write ("\ n") f.write ("SQL statement for query:%s \ n"% sql) F.write ("--------------------------- -----------------------------------------------------------------------") f.write (" \ n ") f.write (" \ n ") f.write  Start time: "+ time.strftime ("%y-%m-%d%h:%m:%s ") +" \ n ") f.write (" Check Results for library%s: \ n "% Mysqldbname_tatrade) f.write (" \ n ") Try: #
    Execute SQL statement cursor1.execute (SQL) # get all records list results = Cursor1.fetchall () #print results for row in results: 
            C_fundacco = row[0] # Print results #print "Data fundacco=%s in Account Application Form" (C_fundacco) #获取couchbase中的基金账号 try: res = C.get ("*_" +c_fundacco) #print res.value[' Custkey '] res=c.get (res.value[' Custkey ']) COUCHBASE_FUNDACco=res.value[' Fundacco ' if (Couchbase_fundacco <> C_fundacco): #print "Different fund accounts (Confirmation form account Number = %s, couchbase=%s) "% (Couchbase_fundacco, C_fundacco) f.write (" Different fund accounts (confirmation form account =%s, couchbase=%s) \ n "% (co Uchbase_fundacco, C_fundacco) except: #print "account number%s not found in Couchbase"% (C_fundacco) F.writ E ("Account ID not found in couchbase:%s \ n"% (C_fundacco)) except Exception,e:print Exception, ":", e f.write ("\ n") f.write ("Sub-Library Check result for%s: \ n "% mysqldbname_tatrade1" try: # Execute SQL statement cursor2.execute (SQL) # get all records list results = Cursor2.fetchall () #print results for row in Results:c_fundacco = row[0] # Print results #print "Data from the account application fundacco=%s"% (c_ Fundacco) #获取couchbase中的基金账号 try:res = C.get ("*_" +c_fundacco) #print res.value[' Cust Key '] res=c.get (res.value[' Custkey ']) couchbase_fundacco=res.value[' Fundacco ' if (cou Chbase_fundacco <> C_fundacco): #print "different fund account (confirmation form account =%s, couchbase=%s)"% (Couchbase_fundacco, C_fundacco) F.write ("Different fund accounts (confirmation form account =%s, couchbase=%s) \ n"% (Couchbase_fundacco, C_fundacco)) except: #print "in
    Account number not found in couchbase%s "% (C_fundacco) f.write (" Account ID not found in couchbase:%s \ n "% (C_fundacco)) except Exception,e: Print Exception, ":", e Db1.close () db2.close () f.close f.write ("\ n") f.write ("End time:" + time.strftime ("%y-%m-%d%H:

%m:%s ") +" \ n ") print" Query complete! "
 Os.system ("Pause")
#my. CFG
#目录配置文件中可以支持两个分库的设置
[couchbase]
ip=10.2.130.78
port=8091
bucketname=assign
Bucketpasswd=assign

[MySQL]
ip=10.2.130.78
port=3306
username=root
dbname_tatrade=hs_ Tatrade
passwd=hstest@1

ip1=10.2.130.78
port1=3306
username1=root
dbname_tatrade1=hs_ Tatrade1
passwd1=hstest@1
The following software checklist needs to be installed:
1.pyhton-2.7.12
2.mysql-python-1.2.5.win32-py2.7
3.couchbase-2.1.2.win32-py2.7
Add the Python/bin installation directory to the environment variable.

Note:
All software download addresses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.