python导出hive数据表的schema实例代码


Posted in Python onJanuary 22, 2018

本文研究的主要问题是python语言导出hive数据表的schema,分享了实现代码,具体如下。

为了避免运营提出无穷无尽的查询需求,我们决定将有查询价值的数据从mysql导入hive中,让他们使用HUE这个开源工具进行查询。想必他们对表结构不甚了解,还需要为之提供一个表结构说明,于是编写了一个脚本,从hive数据库中将每张表的字段即类型查询出来,代码如下:

#coding=utf-8 
import pyhs2 
from 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 info 
 
if __name__ == '__main__': 
  create_excel()

其实,我们的hive数据库将所有的元数据存储在了mysql当中,分析这些元数据也可以获得表结构信息。

总结

以上就是本文关于python导出hive数据表的schema实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

Python 相关文章推荐
python快速排序代码实例
Nov 21 Python
使用python的chardet库获得文件编码并修改编码
Jan 22 Python
利用numpy实现一、二维数组的拼接简单代码示例
Dec 15 Python
python控制windows剪贴板,向剪贴板中写入图片的实例
May 31 Python
Python设计模式之享元模式原理与用法实例分析
Jan 11 Python
Python 多线程其他属性以及继承Thread类详解
Aug 28 Python
Matplotlib绘制雷达图和三维图的示例代码
Jan 07 Python
Python实现计算长方形面积(带参数函数demo)
Jan 18 Python
Pytorch .pth权重文件的使用解析
Feb 14 Python
Python连接Mysql进行增删改查的示例代码
Aug 03 Python
python中xlrd模块的使用详解
Feb 01 Python
Python Pandas读取Excel日期数据的异常处理方法
Feb 28 Python
Python的SimpleHTTPServer模块用处及使用方法简介
Jan 22 #Python
一道python走迷宫算法题
Jan 22 #Python
浅谈使用Python内置函数getattr实现分发模式
Jan 22 #Python
python正则表达式及使用正则表达式的例子
Jan 22 #Python
Python深度优先算法生成迷宫
Jan 22 #Python
Python使用Tkinter实现机器人走迷宫
Jan 22 #Python
Python实现简单文本字符串处理的方法
Jan 22 #Python
You might like
Yii Framework框架获取分类下面的所有子类方法
2014/06/20 PHP
ThinkPHP模板中判断volist循环的最后一条记录的验证方法
2014/07/01 PHP
linux下编译安装memcached服务
2014/08/03 PHP
PHP读取文件的常见几种方法
2016/11/03 PHP
PHP正则+Snoopy抓取框架实现的抓取淘宝店信誉功能实例
2017/05/17 PHP
PHP简单留言本功能实现代码
2017/06/09 PHP
一些mootools的学习资源
2010/02/07 Javascript
使用jquery插件实现图片延迟加载技术详细说明
2011/03/12 Javascript
原生js实现shift/ctrl/alt按键的获取
2013/04/08 Javascript
浅谈Webpack打包优化技巧
2018/06/12 Javascript
JavaScript使用享元模式实现文件上传优化操作示例
2018/08/07 Javascript
JavaScript中this的全面解析及常见实例
2019/05/14 Javascript
使用layer弹窗提交表单时判断表单是否输入为空的例子
2019/09/26 Javascript
Echarts.js无法引入问题解决方案
2020/10/30 Javascript
[01:52]深扒TI7聊天轮盘语音出处7
2017/05/11 DOTA
零基础写python爬虫之爬虫的定义及URL构成
2014/11/04 Python
python获取标准北京时间的方法
2015/03/24 Python
Python自动发邮件脚本
2017/03/31 Python
详解Python多线程Selenium跨浏览器测试
2017/04/01 Python
python实现微信远程控制电脑
2018/02/22 Python
django将图片上传数据库后在前端显式的方法
2018/05/25 Python
详解python如何在django中为用户模型添加自定义权限
2018/10/15 Python
Python字典的核心底层原理讲解
2019/01/24 Python
Python partial函数原理及用法解析
2019/12/11 Python
使用matplotlib绘制图例标签中带有公式的图
2019/12/13 Python
python实现堆排序的实例讲解
2020/02/21 Python
如何在keras中添加自己的优化器(如adam等)
2020/06/19 Python
Bitiba意大利:在线宠物商店
2020/10/31 全球购物
大学生实习鉴定评语
2014/04/25 职场文书
多媒体编辑专业毕业生求职信
2014/06/13 职场文书
学校群众路线专项整治方案
2014/10/31 职场文书
小学安全工作总结2015
2015/05/18 职场文书
2015年青年志愿者工作总结
2015/05/20 职场文书
如何用PHP实现多线程编程
2021/05/26 PHP
PostgreSQL自动更新时间戳实例代码
2021/11/27 PostgreSQL
vue实现移动端div拖动效果
2022/03/03 Vue.js