您好,登錄后才能下訂單哦!
利用Python調用HBASE的 需要安裝thrift hbase-thrift
啟動hbase的thrift服務:bin/hbase-daemon.sh start thrift 默認端口是9090
mysql 到hbase的數據同步:
1、put
2、Importtsv
3、編寫MapReduce Job導入
4、sqoop
簡單code:
#!/usr/bin/env python
#coding=utf-8
import sys
sys.path.append('/usr/lib/python2.6/site-packages/hbase')
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 *
import csv
from hbase.ttypes import ColumnDescriptor, Mutation, BatchMutation, TRegionInfo
from hbase.ttypes import IOError, AlreadyExists
######
def client_conn():
transport=TSocket.TSocket("172.16.10.87",9090)
transport=TTransport.TBufferedTransport(transport)
protocol=TBinaryProtocol.TBinaryProtocol(transport)
client=Hbase.Client(protocol)
transport.open()
return client
def __del__():
transport.close()
if __name__=="__main__":
client=client_conn()
#獲取表名字
print client.getTableNames()
##創建表
#client.createTable('name2',[ColumnDescriptor(name="user_id:",maxVersions=1),ColumnDescriptor(name="user_name"),])
#寫入數據
client.mutateRow('name2','a1',[Mutation(column='user_id:1',value="1")])
client.mutateRow('ca_record','1',[Mutation(column='user_id:0',value='5')])
##獲取數據
aa=client.getRow('name2','a1')
for r in aa:
print 'row',r.row
print '\br'
print 'value',r.columns.get("user_id:1").value
##刪除表
#client.disableTable("t1")
#client.deleteTable("t1")
print client.getTableNames()
###獲取表的行鍵值
#print client.scannerGet(client.scannerOpen('t2',"cmd",["a"]))
print client.scannerGet(client.scannerOpen('t2',"",["a"])) #當row key為空取第一個
print "------"
print client.getColumnDescriptors('t3')
#在自己開發環境 只要安裝好thrift 及hbase-thrift的包,在import的時候不會出問題
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。