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

2024-06-02 16:38

本文主要是介绍获取impala下所有的数据库建表语句,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

本博文介绍三种方法,推荐使用第三种,前两种都是尝试。

方法一:

现在的导出还是有缺陷的,导出的文件中还是存在其他不必要的信息

复制代码

#!/bin/bash
##获取数据库
databases=$(hive -e "show databases; exit;") 
for database in $databases;
do        
#获取hive建表语句        
tables=$(hive -e "use $database; show tables;")       for table in $tables;        do                             echo "--=========== db: $database , table: $table ===========" >> $database.sqlecho "$(hive -e "use $database;show create table $table;");" >> $database.sqldone
done

复制代码

还没有找到其他方法。有其他解决方法,可以Mark一下我

方法二:

20191108今天有想出来一个方法,有点繁杂,但也是可以的,用impala-shell

1.先准备一个文件(tables_name.txt),我们会读这个文件

[root@bigdata zw]# more tables_name.txt 
show create table cdata.c01_bill_distr_stat
show create table cdata.c01_bill_distr_stat_temp1
show create table cdata.c01_bill_pro_bal
show create table cdata.c01_bill_repay_stat
show create table cdata.c01_bill_repay_stat_temp1

2.一个小脚本

复制代码

#!/usr/bin/python
# -*- coding:utf-8 -*-import time,sys
import os
reload(sys)
sys.setdefaultencoding("utf8")file=open("tables_name.txt")
send_file = file.readlines()
for i in send_file:os_cmd1 = "impala-shell -q 'use cdata' "os_cmd2 = "impala-shell -q '"+ i.strip('\n') +"'"os.system(os_cmd2)
file.close()

复制代码

都放在一个目录下,运行python脚本,这时候,日志会打印到屏幕上,需要获取屏幕上的日志内容即可。

我用的xshell工具

 

 

 

这个时候,所有的日志都会打印到文件中(bigdata_2019-11-08_17-20-11),可以找到自己想要的内容。

方法三: 

复制代码

#!/usr/bin/env python
#-*- coding:utf8 -*-
# 从mysql中提取hive建表语句
import os,sys
import fileinput
import datetime
import mysql.connectorreload(sys)
sys.setdefaultencoding("utf8")def hive_create_table():conn = mysql.connector.connect(host="192.168.xxx.xxx",user='hive',passwd='123456',database='hive',charset='utf8')mycursor = conn.cursor()# 获取DB_IDselect_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 in result_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 in result_table_names :fo.write("\nCREATE TABLE "+DBname +'.`'+str(table_name).split('\'')[1]+"`(\n")# 根据表名获取SD_IDselect_table_SD_ID = "select SD_ID from TBLS where tbl_name='"+str(table_name).split('\'')[1]+"' and DB_ID="+DB_ID+";"print(select_table_SD_ID)mycursor.execute(select_table_SD_ID)result_SD_ID = mycursor.fetchone()print(result_SD_ID )# 根据SD_ID获取CD_IDSD_ID=str(result_SD_ID)[1:].split(',')[0]select_table_CD_ID = "select CD_ID from SDS where SD_ID="+str(result_SD_ID)[1:].split(',')[0]+";"print(select_table_CD_ID)mycursor.execute(select_table_CD_ID)result_CD_ID = mycursor.fetchone()print(result_CD_ID)        # 根据CD_ID获取表的列CD_ID=str(result_CD_ID)[1:].split(',')[0]select_table_COLUMN_NAME = "select COLUMN_NAME,TYPE_NAME,COMMENT from COLUMNS_V2 where CD_ID="+str(result_CD_ID)[1:].split(',')[0]+" order by INTEGER_IDX;"print(select_table_COLUMN_NAME)mycursor.execute(select_table_COLUMN_NAME)result_COLUMN_NAME = mycursor.fetchall()print(result_COLUMN_NAME)        index=0for col,col_type,col_name in result_COLUMN_NAME:print(col)print(col_type)print(col_name)print(len(result_COLUMN_NAME) )# 写入表的列和列的类型到文件if col_name is None:fo.write("  `"+str(col)+"`  "+str(col_type))else:fo.write("  `"+str(col)+"`  "+str(col_type) + " COMMENT '" + str(col_name) + "'")if index < len(result_COLUMN_NAME)-1:index = index + 1fo.write(",\n")elif index == len(result_COLUMN_NAME)-1:fo.write("\n)")# 根据表名获取TBL_IDselect_table_SD_ID = "select TBL_ID from TBLS where tbl_name='"+str(table_name).split('\'')[1]+"' and DB_ID="+DB_ID+";"print(select_table_SD_ID)mycursor.execute(select_table_SD_ID)result_TBL_ID = mycursor.fetchone()print(result_TBL_ID)# 根据TBL_ID获取分区信息select_table_PKEY_NAME_TYPE = "select PKEY_NAME,PKEY_TYPE,PKEY_COMMENT from PARTITION_KEYS where TBL_ID="+str(result_TBL_ID)[1:].split(',')[0]+" order by INTEGER_IDX;"print(select_table_PKEY_NAME_TYPE)mycursor.execute(select_table_PKEY_NAME_TYPE)result_PKEY_NAME_TYPE = mycursor.fetchall()print(result_PKEY_NAME_TYPE)if len(result_PKEY_NAME_TYPE) > 0:fo.write("\nPARTITIONED BY (\n")else :fo.write("\n")i=0for pkey_name,pkey_type,PKEY_COMMENT in result_PKEY_NAME_TYPE:if str(PKEY_COMMENT) is None:fo.write("  `"+str(pkey_name)+"`  "+str(pkey_type))else:fo.write("  `"+str(pkey_name)+"`  "+str(pkey_type) + " COMMENT '" + str(PKEY_COMMENT) + "'\n")if i < len(result_PKEY_NAME_TYPE)- 1:i = i + 1fo.write(",")elif i == len(result_PKEY_NAME_TYPE) - 1:fo.write(")\n")# 根据表TBL_ID 获得中文名称select_PARAM_VALUE01 = "select PARAM_VALUE from TABLE_PARAMS  WHERE TBL_ID=( select TBL_ID from TBLS where tbl_name='"+str(table_name).split('\'')[1]+"' and DB_ID="+DB_ID+") and PARAM_KEY='comment';"print(select_PARAM_VALUE01)mycursor.execute(select_PARAM_VALUE01)result_PARAM_VALUE01 = mycursor.fetchone()print result_PARAM_VALUE01if result_PARAM_VALUE01 is None:print '未设置表名'elif not result_PARAM_VALUE01[0]:print '表名为空'else:fo.write("COMMENT '" + str(result_PARAM_VALUE01[0]) +"' \n" )# 根据SD_ID和CD_ID获取SERDE_IDselect_SERDE_ID = "select SERDE_ID from SDS where SD_ID="+SD_ID+" and CD_ID="+CD_ID+";"print(select_SERDE_ID)mycursor.execute(select_SERDE_ID)result_SERDE_ID = mycursor.fetchone()print(result_SERDE_ID)# 根据SERDE_ID获取PARAM_VALUE(列分隔符)select_PARAM_VALUE = "select PARAM_VALUE from SERDE_PARAMS where SERDE_ID="+str(result_SERDE_ID)[1:].split(",")[0]+" and PARAM_KEY='field.delim';"print(select_PARAM_VALUE)mycursor.execute(select_PARAM_VALUE)result_PARAM_VALUE = mycursor.fetchone()print(result_PARAM_VALUE)if result_PARAM_VALUE is not None:fo.write("ROW FORMAT DELIMITED\n")fo.write("FIELDS TERMINATED BY '"+str(result_PARAM_VALUE).split('\'')[1]+"'\n")# 根据SERDE_ID获取PARAM_VALUE(行分隔符)select_PARAM_HNAG = "select PARAM_VALUE from SERDE_PARAMS where SERDE_ID="+str(result_SERDE_ID)[1:].split(",")[0]+" and PARAM_KEY='line.delim';"print(select_PARAM_HNAG)mycursor.execute(select_PARAM_HNAG)RESULT_PARAM_HNAG = mycursor.fetchone()print(RESULT_PARAM_HNAG)if RESULT_PARAM_HNAG is not None:fo.write("LINES TERMINATED BY '"+str(RESULT_PARAM_HNAG).split('\'')[1]+"'\n")# 根据SD_ID和CD_ID获取输入输出格式select_table_STORE_FORMAT = "select INPUT_FORMAT from SDS where SD_ID="+SD_ID+" and CD_ID="+CD_ID+";"print(select_table_STORE_FORMAT)mycursor.execute(select_table_STORE_FORMAT)result_table_STORE_FORMAT= mycursor.fetchall()print(result_table_STORE_FORMAT)for store_format in result_table_STORE_FORMAT:if "org.apache.hadoop.hive.ql.io.orc.OrcInputFormat" in str(store_format):fo.write("STORED AS ORC;\n")elif "org.apache.hadoop.mapred.TextInputFormat" in str(store_format):fo.write("STORED AS TEXTFILE;\n")elif "org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat" in str(store_format):fo.write("STORED AS PARQUET;\n")elif "org.apache.kudu.mapreduce.KuduTableInputFormat" in str(store_format):fo.write("STORED AS KuduTable;\n")else :fo.write("STORED AS null;\n")fo.close()
hive_create_table()

复制代码

直接生成建表脚本的SQL文件。可以直接运行建表

这篇关于获取impala下所有的数据库建表语句的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1024510

相关文章

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

Python pip下载包及所有依赖到指定文件夹的步骤说明

《Pythonpip下载包及所有依赖到指定文件夹的步骤说明》为了方便开发和部署,我们常常需要将Python项目所依赖的第三方包导出到本地文件夹中,:本文主要介绍Pythonpip下载包及所有依... 目录步骤说明命令格式示例参数说明离线安装方法注意事项总结要使用pip下载包及其所有依赖到指定文件夹,请按照以

SpringBoot服务获取Pod当前IP的两种方案

《SpringBoot服务获取Pod当前IP的两种方案》在Kubernetes集群中,SpringBoot服务获取Pod当前IP的方案主要有两种,通过环境变量注入或通过Java代码动态获取网络接口IP... 目录方案一:通过 Kubernetes Downward API 注入环境变量原理步骤方案二:通过

使用Python实现获取屏幕像素颜色值

《使用Python实现获取屏幕像素颜色值》这篇文章主要为大家详细介绍了如何使用Python实现获取屏幕像素颜色值,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、一个小工具,按住F10键,颜色值会跟着显示。完整代码import tkinter as tkimport pyau

python获取cmd环境变量值的实现代码

《python获取cmd环境变量值的实现代码》:本文主要介绍在Python中获取命令行(cmd)环境变量的值,可以使用标准库中的os模块,需要的朋友可以参考下... 前言全局说明在执行py过程中,总要使用到系统环境变量一、说明1.1 环境:Windows 11 家庭版 24H2 26100.4061

Druid连接池实现自定义数据库密码加解密功能

《Druid连接池实现自定义数据库密码加解密功能》在现代应用开发中,数据安全是至关重要的,本文将介绍如何在​​Druid​​连接池中实现自定义的数据库密码加解密功能,有需要的小伙伴可以参考一下... 目录1. 环境准备2. 密码加密算法的选择3. 自定义 ​​DruidDataSource​​ 的密码解密3

Maven项目中集成数据库文档生成工具的操作步骤

《Maven项目中集成数据库文档生成工具的操作步骤》在Maven项目中,可以通过集成数据库文档生成工具来自动生成数据库文档,本文为大家整理了使用screw-maven-plugin(推荐)的完... 目录1. 添加插件配置到 pom.XML2. 配置数据库信息3. 执行生成命令4. 高级配置选项5. 注意事

Mybatis Plus JSqlParser解析sql语句及JSqlParser安装步骤

《MybatisPlusJSqlParser解析sql语句及JSqlParser安装步骤》JSqlParser是一个用于解析SQL语句的Java库,它可以将SQL语句解析为一个Java对象树,允许... 目录【一】jsqlParser 是什么【二】JSqlParser 的安装步骤【三】使用场景【1】sql语