Ubutnu 20.04 安装和使用单机版HBase

此文首发于我的Jekyll博客:zhang0peter的个人博客


最近在学大数据,于是打算在ubuntu 20.04上安装和使用HBase。

官方文档:Apache HBase ™ Reference Guide

安装Java

HBase基于Java,需要先安装Java。Java 8是推荐版本。

apt update &&apt upgrade
apt install openjdk-8-jdk

下载 HBase

选择下载版本:Index of /hbase

我选择的是最新版HBase 2.2.5

wget https://downloads.apache.org/hbase/2.2.5/hbase-2.2.5-bin.tar.gz
tar xvzf hbase-*.tar.gz
mv hbase-2.2.5 hbase
cd hbase

配置 HBase

设置JAVA_HOME环境变量:

nano conf/hbase-env.sh
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64

启动 HBase

-> # ./bin/start-hbase.sh
running master, logging to /root/hbase/bin/../logs/hbase-root-master-ubuntu-s-1vcpu-2gb-sgp1-01.out
-> # curl http://localhost:16010 
<!--
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
-->
<meta HTTP-EQUIV="REFRESH" content="0;url=/master-status"/>

连接并测试 HBase

连接 HBase

-> # ./bin/hbase shell
2020-06-13 23:28:08,915 WARN  [main] util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
HBase Shell
Use "help" to get list of supported commands.
Use "exit" to quit this interactive shell.
For Reference, please visit: http://hbase.apache.org/2.0/book.html#shell
Version 2.2.5, rf76a601273e834267b55c0cda12474590283fd4c, 2020年 05月 21日 星期四 18:34:40 CST
Took 0.0095 seconds                                                                                                                                          
hbase(main):001:0> 

创建表

hbase(main):001:0> create 'test', 'cf'
Created table test
Took 2.6789 seconds                                                                                                                                          
=> Hbase::Table - test

列出表的信息

hbase(main):006:0> list 'test'
TABLE                                                                                                                                                        
test                                                                                                                                                         
1 row(s)
Took 0.0962 seconds                                                                                                                                          
=> ["test"]

查看表的详细信息

hbase(main):007:0> describe 'test'
Table test is ENABLED                                                                                                                                        
test                                                                                                                                                         
COLUMN FAMILIES DESCRIPTION                                                                                                                                  
{NAME => 'cf', VERSIONS => '1', EVICT_BLOCKS_ON_CLOSE => 'false', NEW_VERSION_BEHAVIOR => 'false', KEEP_DELETED_CELLS => 'FALSE', CACHE_DATA_ON_WRITE => 'fal
se', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW', CACHE_INDEX_ON_WRITE => 'false', I
N_MEMORY => 'false', CACHE_BLOOMS_ON_WRITE => 'false', PREFETCH_BLOCKS_ON_OPEN => 'false', COMPRESSION => 'NONE', BLOCKCACHE => 'true', BLOCKSIZE => '65536'}

1 row(s)

QUOTAS                                                                                                                                                       
0 row(s)
Took 0.5176 seconds    

往表中添加数据

Here, we insert three values, one at a time. The first insert is at row1, column cf:a, with a value of value1. Columns in HBase are comprised of a column family prefix, cf in this example, followed by a colon and then a column qualifier suffix, a in this case.

关于 hbase数据库存储结构的详细介绍可以看这篇文章:5分钟图解Hbase列式存储

hbase(main):008:0> put 'test', 'row1', 'cf:a', 'value1'
Took 0.1607 seconds                                                                                                                                          
hbase(main):009:0> put 'test', 'row2', 'cf:b', 'value2'
Took 0.0158 seconds                                                                                                                                          
hbase(main):010:0> put 'test', 'row3', 'cf:c', 'value3'
Took 0.0077 seconds    

获得表中所有数据:

hbase(main):011:0> scan 'test'
ROW                                      COLUMN+CELL                                                                                                         
 row1                                    column=cf:a, timestamp=1592091411493, value=value1                                                                  
 row2                                    column=cf:b, timestamp=1592091429128, value=value2                                                                  
 row3                                    column=cf:c, timestamp=1592091437841, value=value3                                                                  
3 row(s)
Took 0.0936 seconds  

获得表中单独row的数据

hbase(main):012:0> get 'test', 'row1'
COLUMN                                   CELL                                                                                                                
 cf:a                                    timestamp=1592091411493, value=value1                                                                               
1 row(s)
Took 0.0536 seconds 

删除表

hbase(main):014:0> disable 'test'
Took 1.3688 seconds                                                                                                                                          
hbase(main):015:0> drop 'test'
Took 0.4925 seconds    

关闭 HBase

hbase(main):016:0> quit
...............................
-> # ./bin/stop-hbase.sh 
stopping hbase............