国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

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

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

瀏覽:71日期: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行數(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)文章:
主站蜘蛛池模板: 阜平县| 洱源县| 阳新县| 临夏市| 石家庄市| 襄汾县| 乐昌市| 会宁县| 秭归县| 清远市| 从江县| 铜鼓县| 象山县| 加查县| 平舆县| 阳原县| 盈江县| 台南县| 永城市| 昭苏县| 呼玛县| 抚州市| 岳西县| 金乡县| 锦屏县| 武宣县| 江安县| 雷波县| 思茅市| 正宁县| 台山市| 游戏| 山丹县| 抚远县| 赞皇县| 祁门县| 威海市| 长沙县| 通海县| 呼图壁县| 镇沅|