python 导出hive数据表的schema
来源:互联网 发布:数据存储的方式 编辑:程序博客网 时间:2024/05/22 05:25
为了避免运营提出无穷无尽的查询需求,我们决定将有查询价值的数据从mysql导入hive中,让他们使用HUE这个开源工具进行查询。想必他们对表结构不甚了解,还需要为之提供一个表结构说明,于是编写了一个脚本,从hive数据库中将每张表的字段即类型查询出来,代码如下:
#coding=utf-8import pyhs2from xlwt import *hiveconn = pyhs2.connect(host='10.46.77.120', port=10000, authMechanism='PLAIN', user='hadoop', database='hibiscus_data', )def create_excel(): sql = 'show tables' tables = [] with hiveconn.cursor() as cursor: cursor.execute(sql) res = cursor.fetch() for table in res: tables.append(table[0]) tableinfo = [] for table in tables: tableinfo.append(get_column_info(table)) create_excel_ex(tableinfo)def create_excel_ex(tableinfo): w = Workbook() sheet = w.add_sheet(u'表结构') row = 0 for info in tableinfo: row = write_tale_info(info,sheet,row) w.save('hive_schema.xls')def write_tale_info(tableinfo,sheet,row): print row sheet.write_merge(row,row,0,2,tableinfo['table']) row += 1 sheet.write(row,0,u'名称') sheet.write(row,1,u'类型') sheet.write(row,2,u'解释') row += 1 fields = tableinfo['fields'] for field in fields: sheet.write(row,0,field['name']) sheet.write(row,1,field['type']) row += 1 return row + 1 def get_column_info(table): sql = 'desc {table}'.format(table=table) info = {'table':table,'fields':[]} with hiveconn.cursor() as cursor: cursor.execute(sql) res = cursor.fetch() for item in res: if item[0] == '': break info['fields'].append({'name':item[0],'type':item[1]}) return infoif __name__ == '__main__': create_excel()其实,我们的hive数据库将所有的元数据存储在了mysql当中,分析这些元数据也可以获得表结构信息。
0 0
- python 导出hive数据表的schema
- HIVE数据表的导入导出
- hive数据表结构导出到文件
- [Sqoop]将Hive数据表导出到Mysql
- hive的元数据表清单
- hive相关的元数据表
- 开启hive数据表的update delete
- 开启hive数据表的update delete
- Hive的元数据表结构详解
- Hive的元数据表结构详解
- ADO.Net:从DataReader中获取数据表的Schema信息
- Hive笔记七:Hive的数据导出
- 数据库schema的导出和导入
- 用JSP导出ORACLE的数据表DDL
- 用AdoDataSet实现数据表的导入导出
- 导出sql server数据表的详细结构
- SQL Server 2005导出数据表的内容
- 使用EXP 导出数据表的部分数据
- 使用内存数据库/数据集以提高执行速度
- NCR(Numeric Character Reference)简单介绍
- AD10原理图库经验(方框覆盖了引脚内容)
- 系统io各个函数用法总结
- 发送手机短信获取验证码功能
- python 导出hive数据表的schema
- java关键字this
- 企业级 J2EE 框架 Core-Classic
- [Java基础笔记]读写部分
- 表达式
- poj 2506Tiling
- Nutz logoJava应用框架 Nutz
- JSP里ContentType ,charset和pageEncoding的理解与区别
- 最长回文字符串