日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python操作hive数据库代码_python导出hive数据表的schema实例代码

發(fā)布時(shí)間:2024/8/23 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python操作hive数据库代码_python导出hive数据表的schema实例代码 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

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

#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'名稱(chēng)')

sheet.write(row,1,u'類(lèi)型')

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ù)庫(kù)將所有的元數(shù)據(jù)存儲(chǔ)在了mysql當(dāng)中,分析這些元數(shù)據(jù)也可以獲得表結(jié)構(gòu)信息。

總結(jié)

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

總結(jié)

以上是生活随笔為你收集整理的python操作hive数据库代码_python导出hive数据表的schema实例代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。