更新mysql元数据_更新hive元数据-python代码

#!/usr/bin/python

#-*- coding: utf-8 -*-

import sys

import os

import re

import chardet

import codecs

import MySQLdb

path="/Users/zhangyupeng/SVN/10_UAT_DEPLOY/SCFS/"

#connect the database

db = MySQLdb.connect(host="localhost" , user="root" , passwd="XXXX" , db="test_python" , charset="utf8")

cursor=db.cursor()

for i in os.walk(path):

file= str(i[2]).strip().split(',')

for j in file:

if j.lower().find('.sql')>0:

#take the SVN file path

#kill the symbol

j=j.strip().replace('[','').replace(']','').replace('\'','',10).replace('/','')

path=str(i[0])+'/'+j

#pass the udf & 10_TEMP & FXDS

if path.find('10_TEMP')>0:

continue;

if path.find('UDF')>0:

continue

if path.find('FXDS')>0:

continue;

#open the SVN file and take the info

with open(path,"rb") as f:

#open the file with code

data = f.read()

code=(chardet.detect(data))

code_p=str(code)[str(code)[:-2].rfind('\'')+1:].replace('\'','').replace('}','').replace(': ','')

mysql=codecs.open(path, 'rb',code_p)

#take the infomation

for line in mysql:

p1=line.lower().strip().encode('utf-8')

a3=''

a4=''

p=str(p1)

#kill the comment

if str(p)[0:2]=='--':

continue;

#search the database & table

if str(p).find('create')>=0  and str(p).find('table')>=0:

#take the database

aa1=str(p)[0:str(p).find('.')]

a1=aa1[aa1.rfind(' ')+1:]

#take the table

a2=str(p)[str(p).rfind('.')+1:].replace('(','')

#search the field & comment

if str(p).find('comment')>0:

#take the field

a3=str(p)[:str(p).find(' ')]

#take the comment

a4=str(p)[str(p).rfind('comment',10)+8:].replace(' ','',20).replace('\'','',10).replace(',','',20)

#kill the null

if a3=='' or a3==')':

continue;

#print a1+','+a2+','+a3+','+a4

#print 'database:',a1,'|','table:',a2,'|','field:',a3,'|','comment:',a4

cursor.execute('insert into svn_field_comment_test values("%s", "%s", "%s", "%s")' % \

(a1,a2,a3,a4))

db.commit()

db.close

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值