使用python客戶端訪問(wèn)impala的操作方式
因需要將impala僅僅作為數(shù)據(jù)源使用,而python有較好的數(shù)據(jù)分析函數(shù),所以需要使用python客戶端來(lái)獲取impala中的表數(shù)據(jù),這里的測(cè)試環(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 對(duì)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ù)會(huì)刪除緩存,所以第二個(gè).fetchall()返回一個(gè)空列表。
>>> 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開(kāi)始。以上仍是以緩存方式來(lái)獲取數(shù)據(jù)。
如果你的數(shù)據(jù)集較小可以使用這種方式;如果你需要存儲(chǔ)大量的數(shù)據(jù)集,你可以用CREATE TABLE AS SELECT語(yǔ)句把它寫(xiě)入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在線安裝,安裝過(guò)程中可能會(huì)提示:Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall.bat). Get it from http://aka.ms/vcpython27
只要按照提示說(shuō)的的去下載一個(gè)VC就可以了。這樣就安裝好了pandas。
以上這篇使用python客戶端訪問(wèn)impala的操作方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. jsp實(shí)現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫(kù)的方法2. chat.asp聊天程序的編寫(xiě)方法3. 使用css實(shí)現(xiàn)全兼容tooltip提示框4. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能5. XML入門(mén)的常見(jiàn)問(wèn)題(二)6. 得到XML文檔大小的方法7. Jsp中request的3個(gè)基礎(chǔ)實(shí)踐8. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)9. Javaweb工程運(yùn)行報(bào)錯(cuò)HTTP Status 404解決辦法10. 如何在jsp界面中插入圖片
