用到的包:xml.dom.minidom 文件
程式碼託管位置 github-pytools
需求
有一個表,裡面資料量比較大,每天一更新,其欄位可以通過xml配置檔案進行配置,即,可能每次建表的欄位不一樣。
上游跑時會根據配置從原始檔中提取,到入庫這一步需要根據配置進行建表。
解決
寫了一個簡單的xml,配置需要欄位及型別
上游讀取到對應的資料
入庫這一步,先把原表刪除,根據配置建新表
XML檔案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
name="top_query" db_name="evaluting_sys"> id query varchar(200) false query pv integer false pv avg_money integer false |
處理指令碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#!/usr/bin/python # -*- coding:utf-8 -*- #author: wklken #desc: use to read db xml config. #----------------------- #2012-02-18 created #---------------------- import sys,os from xml.dom import minidom, Node def read_dbconfig_xml(xml_file_path): content = {} root = minidom.parse(xml_file_path) table = root.getElementsByTagName("table")[0] #read dbname and table name. table_name = table.getAttribute("name") db_name = table.getAttribute("db_name") if len(table_name) > 0 and len(db_name) > 0: db_sql = "create database if not exists `" + db_name +"`; use " + db_name + ";" table_drop_sql = "drop " + table_name + " if exists " + table_name + ";" content.update({"db_sql" : db_sql}) content.update({"table_sql" : table_drop_sql }) else: print "Error:attribute is not define well! db_name=" + db_name + " ;table_name=" + table_name sys.exit(1) #print table_name, db_name table_create_sql = "create table " + table_name +"(" #read primary cell primary_key = table.getElementsByTagName("primary_key")[0] primary_key_name = primary_key.getElementsByTagName("name")[0].childNodes[0].nodeValue table_create_sql += primary_key_name + " INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY," #print primary_key.toxml() #read ordernary field fields = table.getElementsByTagName("field") f_index = 0 for field in fields: f_index += 1 name = field.getElementsByTagName("name")[0].childNodes[0].nodeValue type = field.getElementsByTagName("type")[0].childNodes[0].nodeValue table_create_sql += name + " " + type if f_index != len(fields): table_create_sql += "," is_index = field.getElementsByTagName("is_index")[0].childNodes[0].nodeValue table_create_sql += ");" content.update({"table_create_sql" : table_create_sql}) #character set latin1 collate latin1_danish_ci; print content if __name__ == "__main__": read_dbconfig_xml(sys.argv[1]) |
涉及方法
root = minidom.parse(xml_file_path) 獲取dom物件
root.getElementsByTagName(“table”) 根據tag獲取節點列表
table.getAttribute(“name”) 獲取屬性
primary_key.getElementsByTagName(“name”)[0].childNodes[0].nodeValue 獲取子節點的值(id 得到id)
2012-02-18
打賞支援我寫出更多好文章,謝謝!
打賞作者
打賞支援我寫出更多好文章,謝謝!
任選一種支付方式