doris批量导出表结构 show create tables schame.tableName(python脚本)

源自GitHub 贴链接

doris 目前是不支持批量导出表结构的(当前最新0.13)
但可以用python脚本做

使用前导入两个包

pip install argparse
pip install pymysql

下面贴代码

#!-*-coding:utf8-*-
import argparse
import pymysql

parser = argparse.ArgumentParser(description="dorisdump  Ver 0.1", add_help=False)
parser.add_argument('--host', '-h', help='Connect to host.')
parser.add_argument('--port', '-P', type=int, help='Port number to use for connection.')
parser.add_argument('--user', '-u', help='User for login.')
parser.add_argument('--password', '-p', help='Password to use when connecting to server.')
parser.add_argument('--database', '-d', help='One database name.')
args = parser.parse_args()

host = '你的hostname'
port = 9030
user = '用户'
password = '密码'
database = '指定数据库'

if args.host:
    host = args.host
if args.port:
    port = int(args.port)
if args.user:
    user = args.user
if args.password:
    password = args.password
if args.database:
    database = args.database

db = pymysql.connect(host=host, port=port, user=user,
                     password=password, db=database)
cursor = db.cursor()
all_dump_idx = cursor.execute("show tables;")
# print("Database {} has {} table(s)".format(database, all_dump_idx))

tables = cursor.fetchall()

cur_dump_idx = 0

createtable = []
for table in tables:
    cur_dump_idx += 1
#    print("Progressing {}/{}. current table is {}".format(cur_dump_idx, all_dump_idx, table[0]))
    cursor.execute("show create table {};".format(table[0]))
    meta_info = cursor.fetchall()
    createtable.append(meta_info[0][1]+'\n')

file_name = 'table.txt'

//保存到当前路径下
with open(file_name,'w') as file_obj:
     file_obj.writelines(createtable)
# print("Database {} dump successful!".format(database))
db.close()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值