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使用scrapy解析js示例
Jan 23 Python
Python学习笔记(二)基础语法
Jun 06 Python
python里将list中元素依次向前移动一位
Sep 12 Python
Python线程详解
Jun 24 Python
Python优化技巧之利用ctypes提高执行速度
Sep 11 Python
python解决网站的反爬虫策略总结
Oct 26 Python
浅谈python可视化包Bokeh
Feb 07 Python
python3+PyQt5实现自定义分数滑块部件
Apr 24 Python
Python中一个for循环循环多个变量的示例
Jul 16 Python
wxPython色环电阻计算器
Nov 18 Python
pytorch GAN伪造手写体mnist数据集方式
Jan 10 Python
matplotlib 对坐标的控制,加图例注释的操作
Apr 17 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
转换中文日期的PHP程序
2006/10/09 PHP
extjs grid设置某列背景颜色和字体颜色的实现方法
2010/09/06 Javascript
JavaScript创建类/对象的几种方式概述及实例
2013/05/06 Javascript
javascript中的startWith和endWith的几种实现方法
2013/05/07 Javascript
多种方法实现JS动态添加事件
2013/11/01 Javascript
raphael.js绘制中国地图 地图绘制方法
2014/02/12 Javascript
jQuery 实现自动填充邮箱功能(带下拉提示)
2014/10/14 Javascript
js实现简单div拖拽功能实例
2015/05/12 Javascript
JS实现图片平面旋转的方法
2016/03/01 Javascript
js剪切板应用clipboardData实例解析
2016/05/29 Javascript
Angular路由简单学习
2016/12/26 Javascript
vue-resourse将json数据输出实例
2017/03/08 Javascript
JavaScript简单实现合并两个Json对象的方法示例
2017/10/16 Javascript
vue中的计算属性的使用和vue实例的方法示例
2017/12/04 Javascript
JavaScript解析机制与闭包原理实例详解
2019/03/08 Javascript
JavaScript中的null和undefined用法解析
2019/09/30 Javascript
jQuery 查找元素操作实例小结
2019/10/02 jQuery
js+canvas实现纸牌游戏
2020/03/16 Javascript
vue 判断元素内容是否超过宽度的方式
2020/07/29 Javascript
[00:12]DAC2018 Miracle-站上中单舞台,他能否再写奇迹?
2018/04/06 DOTA
python追加元素到列表的方法
2015/07/28 Python
Pandas 数据框增、删、改、查、去重、抽样基本操作方法
2018/04/12 Python
Python读取excel中的图片完美解决方法
2018/07/27 Python
Python线程池模块ThreadPoolExecutor用法分析
2018/12/28 Python
python系列 文件操作的代码
2019/10/06 Python
python 使用递归的方式实现语义图片分割功能
2020/07/16 Python
树莓派4B安装Tensorflow的方法步骤
2020/07/16 Python
Python threading模块condition原理及运行流程详解
2020/10/05 Python
美国婴儿服装购物网站:Gerber Childrenswear
2020/05/06 全球购物
优秀技术工人先进材料
2014/02/17 职场文书
会计学毕业生求职信
2014/06/25 职场文书
党员干部群众路线个人整改措施
2014/09/18 职场文书
实习协议书范本
2014/09/25 职场文书
教学质量月活动总结
2015/05/11 职场文书
2015年“我们的节日·中秋节”活动总结
2015/07/30 职场文书
Python何绘制带有背景色块的折线图
2022/04/23 Python