利用Python調(diào)用HBASE的 需要安裝thrift hbase-thrift
10年積累的成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有孝感免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
啟動(dòng)hbase的thrift服務(wù):bin/hbase-daemon.sh start thrift 默認(rèn)端口是9090
MySQL 到hbase的數(shù)據(jù)同步:
1、put
2、Importtsv
3、編寫(xiě)MapReduce Job導(dǎo)入
4、sqoop
簡(jiǎn)單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()
##創(chuàng)建表
#client.createTable('name2',[ColumnDescriptor(name="user_id:",maxVersions=1),ColumnDescriptor(name="user_name"),])
#寫(xiě)入數(shù)據(jù)
client.mutateRow('name2','a1',[Mutation(column='user_id:1',value="1")])
client.mutateRow('ca_record','1',[Mutation(column='user_id:0',value='5')])
##獲取數(shù)據(jù)
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"])) #當(dāng)row key為空取第一個(gè)
print "------"
print client.getColumnDescriptors('t3')
#在自己開(kāi)發(fā)環(huán)境 只要安裝好thrift 及hbase-thrift的包,在import的時(shí)候不會(huì)出問(wèn)題