impala查询语句_获取impala下所有的数据库建表语句

该Python脚本用于从MySQL连接到Hive数据库,提取并生成Impala所有数据库的建表语句。通过执行SQL查询获取DB_ID、数据库名、表名、列信息和分区信息,并将结果写入SQL文件中。
摘要由CSDN通过智能技术生成

#!/usr/bin/env python#-*- coding:utf8 -*-#从mysql中提取hive建表语句

importos,sysimportfileinputimportdatetimeimportmysql.connector

reload(sys)

sys.setdefaultencoding("utf8")defhive_create_table():

conn= mysql.connector.connect(host="192.168.xxx.xxx",user='hive',passwd='123456',database='hive',charset='utf8')

mycursor=conn.cursor()#获取DB_ID

select_DB_ID = "select DB_ID from DBS;"mycursor.execute(select_DB_ID)

result_DB_ID=mycursor.fetchall()

fo= open("create_tab.sql", "w")for dir_DB_ID inresult_DB_ID :#获取数据库名

DB_ID = str(dir_DB_ID)[1:].split(',')[0]print(DB_ID)

select_DB_NAME= "select NAME from DBS where DB_ID="+DB_ID+";"

print(select_DB_NAME )

mycursor.execute(select_DB_NAME)

result_DB_NAME=mycursor.fetchone()

fo.write("\n===========数据库:"+str(result_DB_NAME).split('\'')[1]+"===========\n")

DBname=str(result_DB_NAME).split('\'')[1]print '数据库名字:' +DBnameprint(result_DB_NAME)#获取表名

select_table_name_sql = "select TBL_NAME from TBLS where DB_ID="+DB_ID+";"mycursor.execute(select_table_name_sql)

result_table_names=mycursor.fetchall()for table_name inresult_table_names :

fo.write("\nCREATE TAB

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值