hbase 2.1 环境搭建--独立模式 Standalone Mode

文章目录

  • 概述
  • jdk
  • hbase 配置
  • 启动
  • 使用`hbase shell`命令行操作hbase
    • 新建表
    • 新增记录
    • 查看记录
    • 删除记录
    • 启用禁用表
    • 删除表
    • 退出shell
  • 停止HBase

概述

本节介绍hbase的2.1版本的搭建过程。
hbase有三种模式:

  • 独立模式
  • 伪分布式
  • 完全分布式

本文讲述独立模式

参考:hbase官方文档

jdk

环境配置:
centos 1511
hbase-2.1.5
jdk8(hbase2以上的版本要求jdk8)

安装步骤:

  • 下载jdk-8u131-linux-x64.tar.gz,上传到Linux服务器。
  • tar -zxf jdk-8u131-linux-x64.tar.gz -C /usr/java
  • 配置环境变量
vi /etc/profile


添加以下内容:

JAVA_HOME=/usr/java/jdk1.8.0_131
JRE_HOME=/usr/java/jdk1.8.0_131/jre
CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JRE_HOME CLASS_PATH PATH

输入source /etc/profile生效。

hbase 配置

  • 下载hbase-2.1.5-bin.tar.gz,上传到Linux服务器,解压到/opt目录下。然后进入conf目录
cd /opt/hbase-2.1.5/conf
  • 编辑hbase-env.sh文件,设置java_home
# The java implementation to use.  Java 1.8+ required.
export JAVA_HOME=/usr/java/jdk1.8.0_131
  • 编辑hbase-site.xml,配置hbase和zk数据存放的目录。

hbase-site.xml是hbase的主配置文件。我们这里只是指定zk和hbase文件存放的目录。默认情况下会存储在/tmp目录下,很多服务器在重启以后,/tmp目录的内容会被删除。


  
    hbase.rootdir
    file:///opt/hbase-2.1.5/data/hbase
  
  
    hbase.zookeeper.property.dataDir
    /opt/hbase-2.1.5/data/zk
  
  
    hbase.unsafe.stream.capability.enforce
    false
    
      Controls whether HBase will check for stream capabilities (hflush/hsync).

      Disable this if you intend to run on LocalFileSystem, denoted by a rootdir
      with the 'file://' scheme, but be mindful of the NOTE below.

      WARNING: Setting this to false blinds you to potential data loss and
      inconsistent system state in the event of process and/or node failures. If
      HBase is complaining of an inability to use hsync or hflush it's most
      likely not a false positive.
    
  

上述目录不需要我们手动创建,hbase会自动创建。注意 : hbase.rootdir 这里我们使用的是file://开头的文件路径描述,表示使用的是本地文件系统。要在现有的HDFS实例上安装HBase,请将hbase.rootdir设置为指向您实例上的目录:例如,hdfs://namenode.example.org:8020/hbase。这个我后续会介绍。

启动

  • 执行start-hbase.sh命令启动服务
[root@localhost bin]# cd /opt/hbase-2.1.5/bin
[root@localhost bin]# ./start-hbase.sh 
running master, logging to /opt/hbase-2.1.5/bin/../logs/hbase-root-master-localhost.localdomain.out

启动成功会提示running master

  • hbase shell命令连接hbase
[root@localhost bin]# ./hbase shell
2019-07-09 01:12:20,464 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.1.5, r0ba7cc01a13dbb7fec8e6c5d599fc8b4936bae61, Tue May 28 22:25:02 UTC 2019
Took 0.0018 seconds                                                                                                                                                                                                                                                        
hbase(main):001:0> status
1 active master, 0 backup masters, 1 servers, 0 dead, 2.0000 average load

status检查状态。可以看到只有一个master,0个从服务器

看一下应用进程:

[root@localhost bin]# jps
50723 HMaster
51003 Main
66831 Jps

在独立模式下,HBase在单个JVM中运行所有守护进程,即HMaster,单个HRegionServer和ZooKeeper守护进程。
请注意,表名、行、列都必须用引号字符括起来。

使用hbase shell命令行操作hbase

新建表

语法:create

, {NAME => , VERSIONS => }
例:创建表user,有两个列族(column family),分别是 user,role,且保存历史版本数为3。则建表语句为:

hbase(main):031:0> create 'user',{NAME => 'user', VERSIONS => 3},{NAME => 'role', VERSIONS => 3}
Created table user
Took 0.7562 seconds                                                                                                                                                                                                                                                        
=> Hbase::Table - user

还有简单写法,这里新建一张表,表名是test,列族名称是cf (表名称和ColumnFamily名称是必须指定的)。并且使用list命令查看有哪些表。

hbase(main):002:0> create 'test', 'cf'
Created table test
Took 0.8278 seconds                                                                                                                                                                                                                                                        
=> Hbase::Table - test
hbase(main):003:0> list 'test'
TABLE                                                                                                                                                                                                                                                                      
test                                                                                                                                                                                                                                                                       
1 row(s)
Took 0.0310 seconds                                                                                                                                                                                                                                                        
=> ["test"]
hbase(main):005:0> list
TABLE                                                                                                                                                                                                                                                                      
test                                                                                                                                                                                                                                                                       
1 row(s)
Took 0.0104 seconds                                                                                                                                                                                                                                                        
=> ["test"]
hbase(main):006: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 => 'false', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', REPLICATION_SCOPE => '0', BLOOMFILT
ER => 'ROW', CACHE_INDEX_ON_WRITE => 'false', IN_MEMORY => 'false', CACHE_BLOOMS_ON_WRITE => 'false', PREFETCH_BLOCKS_ON_OPEN => 'false', COMPRESSION => 'NONE', BLOCKCACHE => 'true', BLOCKSIZE => '65536'}                                                               
1 row(s)
Took 0.1471 seconds     

新增记录

语法:put

,,,,

hbase(main):007:0> put 'test', 'row1', 'cf:a', 'value1'
Took 0.0983 seconds                                                                                                                                                                                                                                                        
hbase(main):008:0> put 'test', 'row2', 'cf:b', 'value2'
Took 0.0151 seconds                                                                                                                                                                                                                                                        
hbase(main):009:0> put 'test', 'row3', 'cf:c', 'value3'
Took 0.0137 seconds

查看记录

scan :扫描记录
语法:scan

, {COLUMNS => [ ,.... ], LIMIT => num}

hbase(main):010:0> scan 'test'
ROW                                                                 COLUMN+CELL                                                                                                                                                                                            
 row1                                                               column=cf:a, timestamp=1562660115488, value=value1                                                                                                                                                     
 row2                                                               column=cf:b, timestamp=1562660120094, value=value2                                                                                                                                                     
 row3                                                               column=cf:c, timestamp=1562660123872, value=value3                                                                                                                                                     
3 row(s)
Took 0.0426 seconds


hbase(main):016:0> scan 'test',{LIMIT=>2}
ROW                                                                 COLUMN+CELL                                                                                                                                                                                            
 row1                                                               column=cf:a, timestamp=1562660115488, value=value1                                                                                                                                                     
 row2                                                               column=cf:b, timestamp=1562660120094, value=value2                                                                                                                                                     
2 row(s)
Took 0.0280 seconds

查找记录get
语法:get

,,[,....]

hbase(main):017:0> get 'test', 'row2'
COLUMN                                                              CELL                                                                                                                                                                                                   
 cf:b                                                               timestamp=1562660120094, value=value2                                                                                                                                                                  
1 row(s)
Took 0.0204 seconds

hbase(main):022:0> get 'test', 'row1', ['cf:a']
COLUMN                                                              CELL                                                                                                                                                                                                   
 cf:a                                                               timestamp=1562660115488, value=value1                                                                                                                                                                  
1 row(s)
Took 0.0069 seconds                                                                                                                                                                                                                                                        
hbase(main):023:0> get 'test', 'row1', ['cf:a1']
COLUMN                                                              CELL                                                                                                                                                                                                   
0 row(s)
Took 0.0049 seconds

删除记录

delete
语法:delete

, , , ,必须指定列名

hbase(main):038:0> scan 'test'
ROW                                                                 COLUMN+CELL                                                                                                                                                                                            
 row1                                                               column=cf:a, timestamp=1562660115488, value=value1                                                                                                                                                     
 row2                                                               column=cf:b, timestamp=1562660120094, value=value2                                                                                                                                                     
 row3                                                               column=cf:c, timestamp=1562660123872, value=value3                                                                                                                                                     
3 row(s)
Took 0.0144 seconds                                                                                                                                                                                                                                                        
hbase(main):039:0> delete 'test' , 'row2', 'cf:b'
Took 0.0125 seconds                                                                                                                                                                                                                                                        
hbase(main):040:0> scan 'test'
ROW                                                                 COLUMN+CELL                                                                                                                                                                                            
 row1                                                               column=cf:a, timestamp=1562660115488, value=value1                                                                                                                                                     
 row3                                                               column=cf:c, timestamp=1562660123872, value=value3                                                                                                                                                     
2 row(s)
Took 0.0148 seconds

启用禁用表

disable 命令

hbase(main):011:0> disable 'test'
Took 0.8021 seconds                                                                                                                                                                                                                                                        
hbase(main):012:0> scan 'test'
ROW                                                                 COLUMN+CELL                                                                                                                                                                                            
org.apache.hadoop.hbase.TableNotEnabledException: test is disabled.
	at org.apache.hadoop.hbase.client.ConnectionImplementation.relocateRegion(ConnectionImplementation.java:732)
	at org.apache.hadoop.hbase.client.RpcRetryingCallerWithReadReplicas.getRegionLocations(RpcRetryingCallerWithReadReplicas.java:328)
	at org.apache.hadoop.hbase.client.ScannerCallable.prepare(ScannerCallable.java:139)
	at org.apache.hadoop.hbase.client.ScannerCallableWithReplicas$RetryingRPC.prepare(ScannerCallableWithReplicas.java:399)
	at org.apache.hadoop.hbase.client.RpcRetryingCallerImpl.callWithRetries(RpcRetryingCallerImpl.java:105)
	at org.apache.hadoop.hbase.client.ResultBoundedCompletionService$QueueingFuture.run(ResultBoundedCompletionService.java:80)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:748)

ERROR: Table test is disabled!

For usage try 'help "scan"'

Took 0.3767 seconds                                                                                                                                                                                                                                                        
hbase(main):013:0> enable 'test'
Took 0.7543 seconds                                                                                                                                                                                                                                                        
hbase(main):014:0> scan 'test'
ROW                                                                 COLUMN+CELL                                                                                                                                                                                            
 row1                                                               column=cf:a, timestamp=1562660115488, value=value1                                                                                                                                                     
 row2                                                               column=cf:b, timestamp=1562660120094, value=value2                                                                                                                                                     
 row3                                                               column=cf:c, timestamp=1562660123872, value=value3                                                                                                                                                     
3 row(s)
Took 0.0449 seconds

删除表

想删除drop表,必须要先禁用disable

hbase(main):041:0> drop 'test'

ERROR: Table test is enabled. Disable it first.

For usage try 'help "drop"'

Took 0.0128 seconds                                                                                                                                                                                                                                                        
hbase(main):042:0> disable 'test'
Took 0.4629 seconds                                                                                                                                                                                                                                                        
hbase(main):043:0> drop 'test'
Took 0.5456 seconds

退出shell

要退出HBase Shell并断开与群集的连接,使用quit命令。
注意,这只是退出了shell,HBase仍然在后台运行。

停止HBase

使用stop-hbase.sh命令停止hbase服务。这可能会需要几分钟时间。

[root@localhost bin]# ./stop-hbase.sh 
stopping hbase..........................................................................

你可能感兴趣的:(hbase)