python使用happybase操作Hbase及Hbase在linux系统下的安装

安装Hbase

安装请前往Hbase官方网站,下载版本根据需求自行选择。bin和src文件都需要下载并解压

1
tar zxvf hbase-x.x.x-bin.tar.gz

点击并拖拽以移动

部署jdk并设置java环境

自行下载jdk并解压到、usr/java中

root用户下

1
vi .bash_profile

点击并拖拽以移动

写入以下内容

1
2
3
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

点击并拖拽以移动

执行该文件

1
source .bash_profile

点击并拖拽以移动

使用命令java -version检查是否安装成功

启动和停止HBase

hbase-x.x.x/bin目录下,sh start-hbase.sh”或“./start-hbase.sh”命令启动HBase

出现了以下内容表示启动成功

1
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

安装依赖

1
sudo apt-get install automake bison flex g++ git libboost1.55 libevent-dev libssl-dev libtool make pkg-config

点击并拖拽以移动

下载thrift

编译安装

1
2
3
4
5
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

点击并拖拽以移动

启动服务

1
bin/hbase-daemon.sh start thrift

点击并拖拽以移动

使用jps命令检查服务是否都已启动

1
2
3
4
5
root@ubuntu:~/Hbase/hbase-1.4.2# jps
7393 Jps
4549 ThriftServer
7190 HMaster
2575 Main

点击并拖拽以移动

安装happybase

所在虚拟环境中

1
pip install happybase

点击并拖拽以移动

具体其他用法请参考happybase文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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'}
"""

点击并拖拽以移动