国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python導(dǎo)出hive數(shù)據(jù)表的schema實(shí)例代碼

2020-01-04 16:03:28
字體:
供稿:網(wǎng)友

本文研究的主要問題是python/272358.html">python/284313.html">python/55539.html">python語言導(dǎo)出hive數(shù)據(jù)表的schema,分享了實(shí)現(xiàn)代碼,具體如下。

為了避免運(yùn)營提出無窮無盡的查詢需求,我們決定將有查詢價(jià)值的數(shù)據(jù)從mysql導(dǎo)入hive中,讓他們使用HUE這個(gè)開源工具進(jìn)行查詢。想必他們對表結(jié)構(gòu)不甚了解,還需要為之提供一個(gè)表結(jié)構(gòu)說明,于是編寫了一個(gè)腳本,從hive數(shù)據(jù)庫中將每張表的字段即類型查詢出來,代碼如下:

#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'表結(jié)構(gò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() 

其實(shí),我們的hive數(shù)據(jù)庫將所有的元數(shù)據(jù)存儲在了mysql當(dāng)中,分析這些元數(shù)據(jù)也可以獲得表結(jié)構(gòu)信息。

總結(jié)

以上就是本文關(guān)于python導(dǎo)出hive數(shù)據(jù)表的schema實(shí)例代碼的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!


注:相關(guān)教程知識閱讀請移步到python教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 巴楚县| 西乡县| 新乐市| 泾川县| 建平县| 宜良县| 新宾| 乐都县| 乐安县| 湟源县| 高唐县| 如东县| 新源县| 皮山县| 鹤岗市| 霸州市| 镇巴县| 广平县| 开封县| 昆明市| 萨迦县| 方山县| 金寨县| 瑞安市| 泗水县| 虞城县| 崇礼县| 漳平市| 东城区| 普安县| 枣庄市| 容城县| 嘉鱼县| 景德镇市| 古丈县| 南丹县| 泰兴市| 麻栗坡县| 奎屯市| 墨脱县| 商水县|