安装Hbase
安装请前往Hbase官方网站,下载版本根据需求自行选择。bin和src文件都需要下载并解压
tar zxvf hbase-x.x.x-bin.tar.gz
部署jdk并设置java环境
自行下载jdk并解压到、usr/java中
root用户下
vi .bash_profile
写入以下内容
export JAVA_HOME=/usr/java/jdk1.8.0
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
执行该文件
source .bash_profile
使用命令java -version检查是否安装成功
启动和停止HBase
hbase-x.x.x/bin目录下,sh start-hbase.sh”或“./start-hbase.sh”命令启动HBase
出现了以下内容表示启动成功
starting master, logging to /root/zhouzx/hbase-1.0.1/bin/../logs/hbase-root-master-A10168992.out
sh stop-hbase.sh”或“./stop-hbase.sh”命令停止HBase
安装thrift
安装依赖
sudo apt-get install automake bison flex g++ git libboost1.55 libevent-dev libssl-dev libtool make pkg-config
编译安装
tar -zxvf thrift-0.11.0.tar.gz
cd thrift-0.11.0/
./configure --with-cpp --with-boost --with-python --without-csharp --with-java --without-erlang --without-perl --with-php --without-php_extension --without-ruby --without-haskell --without-go
sudo make
sudo make install
启动服务
bin/hbase-daemon.sh start thrift
使用jps命令检查服务是否都已启动
root@ubuntu:~/Hbase/hbase-1.4.2# jps
7393 Jps
4549 ThriftServer
7190 HMaster
2575 Main
安装happybase
所在虚拟环境中
pip install happybase
具体其他用法请参考happybase文档
import happybase
"""
必须先启动hbase,hbse目录bin/start-hbase.sh
要先在hbase某个节点上开启thrift服务
"""
# hbase thrift -p 9090 start
connection = happybase.Connection('localhost', autoconnect=False)
connection.open()
# families = {
# 'cf1': dict(max_versions=10),
# 'cf2': dict(max_versions=1, block_cache_enabled=False),
# 'cf3': dict(), # use defaults
# }
# connection.create_table('mytable', families)
# connection.create_table('datatest1', )
# print所有的表名
print('All tables: ', connection.tables(), '\n')
# 操作testtable表
# 这个操作是一个提前声明-我要用到这个表了-但不会提交给thrift server做操作
table = connection.table('mytable')
# table.put('row1', {'cf1:content': 'pe', 'cf1:price': '23'})
# table.put('row2', {'cf1:content': 'pe2', 'cf1:price': '232'})
# 检索某一行
row = table.row(b'row1')
print('a row:', row, '\n')
#
# right
print(row[b'cf1:content'])
print(row[b'cf1:price'])
#
# # wrong,这个是错误的,必须加b
# print(row['cf1:content'])
# print(row['cf1:price'])
#
# 显示所有列族
print('所有列族', table.families(), '\n')
#
# 输出两列
print('print two rows:')
rows = table.rows([b'row1', b'row2'])
for key, data in rows:
print(key, data)
#
# 字典输出两列
print('\n', 'print two dict rows')
rows_as_dict = dict(table.rows([b'row1', b'row2']))
print(rows_as_dict)
#
# 输入row的一个列族所有值
row = table.row(b'row2', columns=[b'cf1'])
print('\n', '输出一个列族', row)
#
# scan操作
print('\n', 'do scan')
for key, data in table.scan():
print(key, data)
"""
All tables: [b'mytable']
a row: {b'cf2:price': b'23', b'cf1:content': b'pe', b'cf1:price': b'23'}
b'pe'
b'23'
所有列族 {b'cf3': {'block_cache_enabled': False, 'max_versions': 3, 'name': b'cf3:', 'time_to_live': 2147483647, 'bloom_filter_type': b'NONE', 'in_memory': False, 'compression': b'NONE', 'bloom_filter_vector_size': 0, 'bloom_filter_nb_hashes': 0}, b'cf1': {'block_cache_enabled': False, 'max_versions': 10, 'name': b'cf1:', 'time_to_live': 2147483647, 'bloom_filter_type': b'NONE', 'in_memory': False, 'compression': b'NONE', 'bloom_filter_vector_size': 0, 'bloom_filter_nb_hashes': 0}, b'cf2': {'block_cache_enabled': False, 'max_versions': 1, 'name': b'cf2:', 'time_to_live': 2147483647, 'bloom_filter_type': b'NONE', 'in_memory': False, 'compression': b'NONE', 'bloom_filter_vector_size': 0, 'bloom_filter_nb_hashes': 0}}
print two rows:
b'row1' {b'cf2:price': b'23', b'cf1:content': b'pe', b'cf1:price': b'23'}
b'row2' {b'cf1:content': b'pe2', b'cf1:price': b'232'}
print two dict rows
{b'row1': {b'cf2:price': b'23', b'cf1:content': b'pe', b'cf1:price': b'23'}, b'row2': {b'cf1:content': b'pe2', b'cf1:price': b'232'}}
输出一个列族 {b'cf1:content': b'pe2', b'cf1:price': b'232'}
do scan
b'row1' {b'cf2:price': b'23', b'cf1:content': b'pe', b'cf1:price': b'23'}
b'row2' {b'cf1:content': b'pe2', b'cf1:price': b'232'}
"""