业务场景:本地数据库(Oracle)有200张表需要同步到odps,需要先在odps建表,再进行表数据同步。如果一张张的去建表,很浪费时间。如果后续还有这种业务,则考虑用Python写脚本,读取本地数据库这些表的表结构,在odps上进行批量建表,字段类型统一用string。
import cx_Oracle
from odps import ODPS, options
def create_table(table_name, table_comments, owner):
#执行查询表结构语句,获取表结构信息
col_sql = '''select COLUMN_NAME,COMMENTS from all_col_comments where table_name = '{}' and owner = '{}' '''.format(
table_name, owner)
#print(col_sql) #打印SQL
col_data = cursor.execute(col_sql).fetchall()
# print(type(col_data)) #list
readcolumnlist = [] #存放字段
commentslist = [