python导出蜂巢数据表的模式实例代码

  

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

  

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

        #=utf - 8编码   进口pyhs2   从xlwt进口*      hiveconn=pyhs2.connect(主机=' 10.46.77.120 ',   端口=10000,   authMechanism=捌皆?   用户=' hadoop ',   数据库=' hibiscus_data ',   )      def create_excel ():   sql=跋允颈怼?   表=[]   光标hiveconn.cursor ():   cursor.execute (sql)   res=cursor.fetch ()   在res表:   tables.append(表[0])      tableinfo=[]   表的表:   tableinfo.append (get_column_info(表)      create_excel_ex (tableinfo)      def create_excel_ex (tableinfo):   w=工作簿()   表=w.add_sheet (u '表结构的)   行=0   在tableinfo信息:   行=write_tale_info(信息、表、行)   w.save (“hive_schema.xls”)      def write_tale_info (tableinfo、表、行):   打印行   sheet.write_merge(行、行0 2 tableinfo[‘表’])      行+=1   sheet.write(行,0,u '名称”)   sheet.write(行1 u '类型的)   sheet.write(行2 u '解释”)   行+=1      字段=tableinfo(“字段”)   字段的字段:   sheet.write(行,0,字段(“名字”))   sheet.write(行1场['类型'])   行+=1      返回行+ 1         def get_column_info(表):   sql=' desc{表}’.format(表=表)   信息={“表”:表,“字段”:[]}   光标hiveconn.cursor ():   cursor.execute (sql)   res=cursor.fetch ()   res的项目:   如果项目[0]==?   打破   信息(“字段”).append({“名称”:项目[0],“类型”:项目[1]})      返回信息      if __name__==癬_main__”:   create_excel ()      

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

  

  

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

python导出蜂巢数据表的模式实例代码