python導(dǎo)出hive數(shù)據(jù)表的schema實例代碼-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比安順網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式安順網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋安順地區(qū)。費用合理售后完善,10多年實體公司更值得信賴。

為了避免運營提出無窮無盡的查詢需求,我們決定將有查詢價值的數(shù)據(jù)從mysql導(dǎo)入hive中,讓他們使用HUE這個開源工具進(jìn)行查詢。想必他們對表結(jié)構(gòu)不甚了解,還需要為之提供一個表結(jié)構(gòu)說明,于是編寫了一個腳本,從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() 

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

當(dāng)前題目:python導(dǎo)出hive數(shù)據(jù)表的schema實例代碼-創(chuàng)新互聯(lián)
標(biāo)題來源:http://muchs.cn/article16/coiodg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、手機(jī)網(wǎng)站建設(shè)搜索引擎優(yōu)化、App設(shè)計、ChatGPT營銷型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)