午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

使用python客戶端訪問impala的操作方式

瀏覽:7日期:2022-07-31 18:16:25

因需要將impala僅僅作為數(shù)據(jù)源使用,而python有較好的數(shù)據(jù)分析函數(shù),所以需要使用python客戶端來獲取impala中的表數(shù)據(jù),這里的測試環(huán)境是:

操作系統(tǒng):win7 (linux下也可行)

python 2.7

大數(shù)據(jù)環(huán)境:centos6.6

CDH版本:CDH5.4.1

impala 2.1.2 port:21050

1、安裝Python package

pip install impyla

2、python客戶端與impala交互

2.1 連接impala

>>> from impala.dbapi import connect>>> conn = connect(host=’my.impala.host’, port=21050)>>> cur = conn.cursor()

注意:這里要確保端口設(shè)置為HS2服務(wù),而不是Beeswax服務(wù)。在Cloudera的管理集群中,HS2的默認(rèn)端口是21050。 (Beeswax默認(rèn)端口21000)

2.2 對impala執(zhí)行SQL查詢

>>> cur.execute(’SHOW TABLES’)>>> cur.fetchall()[(’defect_code_dim’,), (’gxzl_ca_materialinfo’,), (’gxzl_cg_materialinfo’,), (’gxzl_defect2’,), (’gxzl_defects’,), (’gxzl_defects_hd’,), (’gxzl_fx_class’,), (’gxzl_fx_leftmidright’,), (’gxzl_fx_topandbot’,), (’gxzl_jiejing_2cc_slab’,), (’gxzl_kgx_drw’,), (’gxzl_kgx_drw_tmp’,), (’gxzl_rz_materialinfo’,), (’gxzl_sdbase_defects’,), (’gxzl_test’,), (’new_table’,), (’ouye_transactionlog’,), (’ouye_userinfo’,), (’simple_test’,), (’t0’,), (’t_100m_hdfs’,), (’t_100m_test’,), (’t_10m_hdfs’,), (’target1’,), (’target2’,), (’target3’,), (’test’,), (’tianchi_mobile_recommend_train_full’,), (’tianchi_mobile_recommend_train_item’,), (’tianchi_mobile_recommend_train_user’,), (’tianchi_mobile_recommend_train_useritem’,)]>>> cur.execute(’SELECT * FROM test’)>>> cur.description[(’id’, ’DOUBLE’, None, None, None, None, None), (’name’, ’STRING’, None, None, None, None, None), (’value’, ’STRING’, None, None, None, None, None)]>>> cur.fetchall()[(1.0, ’tom’, ’f’), (2.0, ’jerry’, ’t’)]>>>

注意:從服務(wù)器上獲取數(shù)據(jù)會刪除緩存,所以第二個.fetchall()返回一個空列表。

>>> cur.fetchall()[(1.0, ’tom’, ’f’), (2.0, ’jerry’, ’t’)]>>> cur.fetchall()[]>>>

2.3 遍歷查詢結(jié)果

>>> cur.execute(’SELECT * FROM test’)>>> for row in cur: print row[1] == 1.0FalseFalse

注:python的角標(biāo)是以0開始。以上仍是以緩存方式來獲取數(shù)據(jù)。

如果你的數(shù)據(jù)集較小可以使用這種方式;如果你需要存儲大量的數(shù)據(jù)集,你可以用CREATE TABLE AS SELECT語句把它寫入HDFS。

2.4 將查詢結(jié)果轉(zhuǎn)化為python中的pandas DataFrames

除了遍歷結(jié)果以外,還可以把結(jié)果轉(zhuǎn)化成pandas的數(shù)據(jù)框?qū)ο螅员氵M(jìn)行數(shù)據(jù)分析:

>>> from impala.dbapi import connect>>> conn = connect(host=’my.impala.host’, port=21050)>>> cur = conn.cursor()>>> from impala.util import as_pandas>>> cur.execute(’SELECT * FROM test’)>>> df = as_pandas(cur)>>> type(df)<class ’pandas.core.frame.DataFrame’>>>> df id name value0 1 tom f1 2 jerry t>>>

注:前提是python中安裝了pandas,使用pip install pandas在線安裝,安裝過程中可能會提示:Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall.bat). Get it from http://aka.ms/vcpython27

只要按照提示說的的去下載一個VC就可以了。這樣就安裝好了pandas。

以上這篇使用python客戶端訪問impala的操作方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 浪漫樱花在线观看高清动漫 | 成人av不卡 | 日韩一区在线播放 | 午夜影院免费看 | 五月综合在线 | 欧美二区在线观看 | 麻豆精品一区二区三区 | 国产精成人品免费观看 | 日韩欧美色图 | 国产日韩精品视频 | 精品视频99 | 制服.丝袜.亚洲.中文.综合懂色 | 亚洲国产一区二区三区 | 日本在线播放视频 | 欧洲精品一区二区 | 综合中文字幕 | 久久精品伊人 | 五十路在线视频 | 日韩综合激情 | 欧美成人二区 | av高清一区二区 | 久草手机在线视频 | 日产精品久久久 | 中文字幕专区 | 成人xx视频| 久久国产美女视频 | 亚洲视频一二三区 | 一区二区三区福利视频 | 国产精品一区二区不卡 | 一级大毛片| 久久久午夜影院 | 中文字幕视频免费 | 久久综合免费视频 | 蜜臀久久99精品久久久久久 | 日日射天天射 | 狠狠操网址 | 国产高清一区二区 | 日韩中出| 亚洲免费看黄 | 男人的伸进里面免费网站 | 国产又黄又爽又色 |