python2.7.12操作Hbase
前置條件:您已經安裝好Hbase、python2.7
題外話:最好自己安裝個虛擬環境,以下操作都是在虛擬環境中的
(ma) hadoop@master:/usr/local/pycharm/bin$ sudo pip install thrift
[sudo] password for hadoop:
The directory '/home/hadoop/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/hadoop/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting thrift
? Downloading thrift-0.10.0.zip (87kB)
??? 100% |████████████████████████████████| 92kB 415kB/s
Requirement already satisfied: six>=1.7.2 in /usr/local/lib/python2.7/dist-packages (from thrift)
Installing collected packages: thrift
? Running setup.py install for thrift ... done
Successfully installed thrift-0.10.0
?
(ma) hadoop@master:/usr/local/pycharm/bin$ sudo pip install hbase-thrift
[sudo] password for hadoop:
The directory '/home/hadoop/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/hadoop/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting hbase-thrift
? Downloading hbase-thrift-0.20.4.tar.gz
Requirement already satisfied: Thrift in /usr/local/lib/python2.7/dist-packages (from hbase-thrift)
Requirement already satisfied: six>=1.7.2 in /usr/local/lib/python2.7/dist-packages (from Thrift->hbase-thrift)
Installing collected packages: hbase-thrift
? Running setup.py install for hbase-thrift ... done
Successfully installed hbase-thrift-0.20.4
Hbase的bin目錄下啟動bin/./hbase-daemon.sh start thrift
hadoop@master:/opt/Hadoop/hbase-1.3.1/bin$ ./hbase-daemon.sh start thrift
啟動pycharm
注意在虛擬環境中啟動,其它環境中有可能程序運行不了。
(ma) hadoop@master:/usr/local/pycharm/bin$ ./pycharm.sh
參考文檔:http://www.cnblogs.com/hitandrew/archive/2013/01/21/2870419.html,此文檔中有的例子運行有問題
創建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 *
transport = TSocket.TSocket('localhost', 9090);
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport);
client = Hbase.Client(protocol)
transport.open()
contents = ColumnDescriptor(name='cf:', maxVersions=1)
client.createTable('test', [contents])
print client.getTableNames()
輸出內容:
/usr/bin/python2.7 /home/py/PycharmProjects/ThirdTest/testThrift.py
['member', 'test']
Process finished with exit code 0
在hbase shell中用list查看有剛才創建的test.
插入數據:
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 *
transport = TSocket.TSocket('localhost', 9090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hbase.Client(protocol)
transport.open()
row = 'row-key1'
mutations = [Mutation(column="cf:a", value="1")]
client.mutateRow('test', row, mutations)
在hbase shell中用scan 'test'查看有剛才創建的test.
hbase(main):001:0> scan 'test'
ROW?????????????????? COLUMN+CELL????????????????????????????????????????????? ?
?row-key1???????????? column=cf:a, timestamp=1506406128150, value=1??????????? ?
1 row(s) in 0.3570 seconds
獲取一行數據:
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 *
transport = TSocket.TSocket('localhost', 9090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hbase.Client(protocol)
transport.open()
tableName = 'test'
rowKey = 'row-key1'
result = client.getRow(tableName, rowKey)
print result
for r in result:
??? print 'the row is ' , r.row
??? print 'the values is ' , r.columns.get('cf:a').value
輸出內容:
/usr/bin/python2.7 /home/py/PycharmProjects/ThirdTest/getOneRow.py
[TRowResult(columns={'cf:a': TCell(timestamp=1506406612641, value='2')}, row='row-key1')]
the row is? row-key1
the values is? 2
查詢多行:
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 *
transport = TSocket.TSocket('localhost', 9090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hbase.Client(protocol)
transport.open()
tableName = 'test'
id = client.scannerOpenWithStop(tableName,'','','')
result2 = client.scannerGetList(id, 10)
print result2
輸出內容:
/usr/bin/python2.7 /home/py/PycharmProjects/ThirdTest/getMultiRow.py
[TRowResult(columns={'cf:a': TCell(timestamp=1506406612641, value='2')}, row='row-key1'), TRowResult(columns={'cf:a': TCell(timestamp=1506406650902, value='2')}, row='row-key2')]
轉載于:https://www.cnblogs.com/herosoft/p/8134173.html
總結
以上是生活随笔為你收集整理的python2.7.12操作Hbase的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 于芝涛正式出任海信集团总裁 董事会和经营
- 下一篇: 新型人工肌肉问世:能软能硬,还能自我感知