hbasethriftpython
⑴ python連接hive,怎麼安裝thrifthive
HiveServer2的啟動
啟動HiveServer2
HiveServer2的啟動十分簡便:
$ $HIVE_HOME/bin/hiveserver2
或者
$ $HIVE_HOME/bin/hive --service hiveserver2
默認情況下,HiverServer2的Thrift監聽埠是10000,其WEB UI埠是10002。可通過http://localhost:10002來查看HiveServer2的Web UI界面,這里顯示了Hive的一些基本信息。如果Web界面不能查看,則說明HiveServer2沒有成功運行。
使用beeline測試客戶端連接
HiveServer2成功運行後,我們可以使用Hive提供的客戶端工具beeline連接HiveServer2。
$ $HIVE_HOME/bin/beeline
beeline > !connect jdbc:hive2://localhost:10000
如果成功登錄將出現如下的命令提示符,此時可以編寫HQL語句。
0: jdbc:hive2://localhost:10000>
報錯:User: xxx is not allowed to impersonate anonymous
在beeline使用!connect連接HiveServer2時可能會出現如下錯誤信息:
Caused by: org.apache.hadoop.ipc.RemoteException:
User: xxx is not allowed to impersonate anonymous
這里的xxx是我的操作系統用戶名稱。這個問題的解決方法是在hadoop的core-size.xml文件中添加xxx用戶代理配置:
<property> <name>hadoop.proxyuser.xxx.groups</name> <value>*</value></property><property> <name>hadoop.proxyuser.xxx.hosts</name> <value>*</value></property>
重啟HDFS後,再用beeline連接HiveServer2即可成功連接。
常用配置
HiveServer2的配置可以參考官方文檔《Setting Up HiveServer2》
這里列舉一些hive-site.xml的常用配置:
hive.server2.thrift.port:監聽的TCP埠號。默認為10000。
hive.server2.thrift.bind.host:TCP介面的綁定主機。
hive.server2.authentication:身份驗證方式。默認為NONE(使用 plain SASL),即不進行驗證檢查。可選項還有NOSASL, KERBEROS, LDAP, PAM and CUSTOM.
hive.server2.enable.doAs:是否以模擬身份執行查詢處理。默認為true。
Python客戶端連接HiveServer2
python中用於連接HiveServer2的客戶端有3個:pyhs2,pyhive,impyla。官網的示例採用的是pyhs2,但pyhs2的官網已聲明不再提供支持,建議使用impyla和pyhive。我們這里使用的是impyla。
impyla的安裝
impyla必須的依賴包括:
six
bit_array
thriftpy(python2.x則是thrift)
sasl
thrift_sasl
- from impala.dbapi import connect
- conn = connect(host='127.0.0.1', port=10000, database='default', auth_mechanism='PLAIN')
- cur = conn.cursor()
- cur.execute('SHOW DATABASES')print(cur.fetchall())
- cur.execute('SHOW Tables')print(cur.fetchall())
為了支持Hive還需要以下兩個包:
可在Python PI中下載impyla及其依賴包的源碼。
impyla示例
以下是使用impyla連接HiveServer2的示例:
⑵ 如何在python中訪問hbase的數據
python訪問hbase需要額外的庫,一般用thrift。使用thrift調用hbase,由於篇幅限制在這里不能說的很詳細。
請網路Phthonthrift或pythonhbase自行查閱相關資料。
下面是一個例子僅供參考
#coding:utf-8
fromthriftimportThrift
fromthrift.transportimportTSocket
fromthrift.transportimportTTransport
fromthrift.protocolimportTBinaryProtocol
fromhbaseimportHbase
fromhbase.ttypesimport*
importcsv
defclient_conn():
transport=TSocket.TSocket('hostname,like:localhost',port)
transport=TTransport.TBufferedTransport(transport)
protocol=TBinaryProtocol.TBinaryProtocol(transport)
client=Hbase.Client(protocol)
transport.open()
returnclient
if__name__=="__main__":
client=client_conn()
result=client.getRow("tablename","rowname")
data_simple=[]
fork,vinresult[0].columns.items():#.keys()
data_simple.append((v.timestamp,v.value))
writer.writerows(data)
csvfile.close()
csvfile_simple=open("data_xy_simple.csv","wb")
writer_simple=csv.writer(csvfile_simple)
writer_simple.writerow(["timestamp","value"])
writer_simple.writerows(data_simple)
csvfile_simple.close()
⑶ 如何使用python在hbase里進行模糊查詢
注意:正則的寫法可能不對,保證能過濾出數據,但是可能不會嚴格匹配,正則問題請自己解決;
#導入thrift和habse包
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
from hbase.ttypes import *
#此處可以修改地址和埠
host = '192.168.1.1'
#默認埠為9090
port = 9090
#要查詢的表名
table = 'table_name'
#定義一個過濾器,此為關鍵步驟
filter = "RowFilter(=,'regexstring:.3333.')" #此行原創:)
# Make socket
transport = TSocket.TSocket(host, port)
# Buffering is critical. Raw sockets are very slow
# 還可以用TFramedTransport,也是高效傳輸方式
transport = TTransport.TBufferedTransport(transport)
# Wrap in a protocol
#傳輸協議和傳輸過程是分離的,可以支持多協議
protocol = TBinaryProtocol.TBinaryProtocol(transport)
#客戶端代表一個用戶
client = Hbase.Client(protocol)
#打開連接
try:
transport.open()
scan.filterString=filter
scanner = client.scannerOpenWithScan(table, scan)
except Exception:
finally:
client.scannerClose(scan)
transport.close()
連接代碼網上一搜一大堆,非原創,來源已不可考,非本人研究成果;
關鍵就是這個:"RowFilter(=,'regexstring:.3333.')"
這個過濾器要寫對,hbase有十幾種內置的過濾器方法,有幾種比較運算符和比較器,上面這個是正則方式,即'regexstring:.3333.';
過濾器整個雙引號裡面的內容會通過thrift傳給hbase服務端處理,下劃線這部分正則要支持java的正則要求不然會報錯,過濾器的用法官網有,網上也有些資料,但是坑比較多,這幾天各種被坑//包括官方坑
Apache HBase
⑷ python可以把爬蟲的數據寫入hbase么
在已經安裝了HBase服務的伺服器中,已經自動安裝了HBase的Thrift的肆並橋腳本,路徑為:/usr/lib/hbase/include/thrift
。
需要使用這個腳本生蔽陪成基於Python語言的HBase的Thrift腳本,具體命令如下:
thrift
--gen
py
hbase2.thrift
命令執行成功後會生成名為gen-py的目錄,其中包含了python版本的HBase包。
主要文件介紹如下:
l
Hbase.py
中定義了一些HbaseClient可以使用的方法
l
ttypes.py中定義了HbaseClient傳輸的數據類裂猛型
將生成的HBase包放入項目代碼或者放入Python環境的依賴包目錄中即可調用。
⑸ 如何在Python中訪問HBase的數據
Python連接HBase時需要先載入Thrift和HBase的相關包,之後創建與HBase的連接並進行後續操作,具體代碼如下:
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from thrift.transport.TSocket import TSocket
from thrift.transport.TTransport import TBufferedTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
from hbase.ttypes import *
import pymongo
import hashlib
import time
from datetime import datetime
class HBaseOperator():
def __init__(self):
self.host = "ip_address"
self.port = 9090
self.transport = TBufferedTransport(TSocket(self.host, self.port))
self.transport.open()
self.protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
self.client = Hbase.Client(self.protocol)
def __del__(self):
self.transport.close()
def getAllTablesInfo(self):
#get table info
listTables = self.client.getTableNames()
print "="*40
print "Show all tables information...."
for tableName in listTables:
print "TableName:" + tableName
print " "
listColumns = self.client.getColumnDescriptors(tableName)
print listColumns
print " "
listTableRegions = self.client.getTableRegions(tableName)
print listTableRegions
print "+"*40
⑹ hbase的特點
hbase的特點:高可靠性、高性能、面向列、可伸縮的。
HBase – Hadoop Database,是一個高可靠性、高性能、面向列、可伸縮的分布式存儲系統,利用HBase技術可在廉價PC Server上搭建起大規模結構化存儲集群。
HBase是Apache的Hadoop項目的子項目。HBase不同於一般的關系資料庫,它是一個適合於非結構化數據存儲的資料庫。另一個不同的是HBase基於列的而不是基於行的模式。

(6)hbasethriftpython擴展閱讀
訪問介面:
1. Native Java API,最常規和高效的訪問方式,適合Hadoop MapRece Job並行批處理HBase表數據
2. HBase Shell,HBase的命令行工具,最簡單的介面,適合HBase管理使用
3. Thrift Gateway,利用Thrift序列化技術,支持C++,PHP,Python等多種語言,適合其他異構系統在線訪問HBase表數據
4. REST Gateway,支持REST 風格的Http API訪問HBase, 解除了語言限制
5. Pig,可以使用Pig Latin流式編程語言來操作HBase中的數據,和Hive類似,本質最終也是編譯成MapRece Job來處理HBase表數據,適合做數據統計。
