Hbase配置之-单机模式

HBase 的运行模式包括单机、伪分布式和分布式三种。 单机模式使用本地文件系统,所有进程运行在一个 JVM 上,单机模式一 般只用于测试,HBase 需要结合 Hadoop 才能展现出其分布式存储的能力。

HBase单机模式是不需要自己额外再安装hadoop 和 zookeeper

1.关闭防火墙、关闭 SELinux

[root@localhost Desktop]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

Jun 10 17:12:07 localhost.localdomain systemd[1]: Starting firewalld - dynami...
Jun 10 17:12:08 localhost.localdomain systemd[1]: Started firewalld - dynamic...
Jun 10 17:20:36 localhost.localdomain systemd[1]: Stopping firewalld - dynami...
Jun 10 17:20:37 localhost.localdomain systemd[1]: Stopped firewalld - dynamic...
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost Desktop]# 
[root@localhost Desktop]# 
[root@localhost Desktop]# sestatus
SELinux status:                 disabled
[root@localhost Desktop]# 

2 配置JDK

[root@localhost Desktop]# which java
/export/servers/jdk1.8.0_311/bin/java

3 解压hbase

[root@localhost servers]# tar -xzvf /export/software/hbase-1.2.6-bin.tar.gz -C /export/servers/
[root@localhost servers]# cd /export/servers/
[root@localhost servers]# ls -ll
total 8
drwxr-xr-x. 9 root  root  4096 Aug 17  2016 hadoop-2.7.3
drwxr-xr-x  7 root  root   150 Jun 10 17:51 hbase-1.2.6
drwxr-xr-x. 8 10143 10143 4096 Sep 27  2021 jdk1.8.0_311

4 配置hbase环境变量

[root@localhost servers]# vi /etc/profile
#在这里插入代码片
export JAVA_HOME=/export/servers/jdk1.8.0_311
export HADOOP_HOME=/export/servers/hadoop-2.7.3
export HBASE_HOME=/export/servers/hbase-1.2.6
export PATH=$PATH:$JAVA_HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$HBASE_HOME/bin

5.配置hbase

5.1 $HBASE_HOME/conf/hbase-env.sh

由于HBase依赖JAVA_HOME环境变量,所以要编辑$HBASE_HOME/conf/hbase-env.sh文件,并取消注释以#export JAVA_HOME =开头的行,然后将其设置为Java安装路径。

[root@localhost conf]# vi $HBASE_HOME/conf/hbase-env.sh
#取消JAVA_HOME的注释,并设置JAVA_HOME
export JAVA_HOME=/export/servers/jdk1.8.0_311

hbase 默认使用内置 zookeeper

# Tell HBase whether it should manage it's own instance of Zookeeper or not.
# export HBASE_MANAGES_ZK=true

语句【export HBASE_MANAGES_ZK=true】表示采用HBase自带的ZooKeeper管理。如果想用外部ZooKeeper管理HBase,可以自行安装、配置ZooKeeper,再把该句删除。

5.2 $HBASE_HOME/conf/hbase-site.xml

在本地文件系统上指定HBase和ZooKeeper写入数据的目录并确认一些风险。默认情况下,在/export/data下创建一个新目录habase。默认位置/tmp,但是许多服务器配置为在重新引导时删除/ tmp的内容,因此您应该将数据存储在其他位置。

[root@localhost conf]# vi $HBASE_HOME/conf/hbase-site.xml
<configuration>
  <!-- hbase存放数据目录 -->
  <property>
    <name>hbase.rootdir</name>
    <value>file:///export/data/hbase/hbase</value>
  </property>

  <!-- ZooKeeper数据文件路径 -->
  <property>
    <name>hbase.zookeeper.property.dataDir</name>
    <value>/export/data/hbase/zookeeper</value>
  </property>
</configuration>

6.启动HBASE

6.1 启动

[root@localhost conf]# start-hbase.sh
starting master, logging to /export/servers/hbase-1.2.6/logs/hbase-root-master-localhost.localdomain.out
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=128m; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=128m; support was removed in 8.0

6.2 查看进程

[root@localhost conf]# jps
6211 Jps
6138 HMaster

HMaster进程就是HBase的主进程。HMaster进程启动表明HBase单机模式启动成功。

6.3 访问HBase的web界面

http://192.168.121.150:16010/master-status

在这里插入图片描述

6.4 关闭

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

7.常用HBase shell基本操作

7.1启动HBase Shell命令行

[root@localhost conf]# start-hbase.sh
starting master, logging to /export/servers/hbase-1.2.6/logs/hbase-root-master-localhost.localdomain.out
[root@localhost conf]# hbase shell
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/export/servers/hbase-1.2.6/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/export/servers/hadoop-2.7.3/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 1.2.6, rUnknown, Mon May 29 02:25:32 CDT 2017

hbase(main):001:0> 

hbase(main):001:0>中的001用来统计用户输入的行数,该数字会随着用户输入的行数自增

7.2 查看HBase shell状态

hbase(main):001:0> status
1 active master, 0 backup masters, 1 servers, 0 dead, 2.0000 average load

通过结果可知,目前有1个活跃的主节点,0个备份的节点,1个服务节点。

7.3 查看版本信息

hbase(main):001:0> version
1.2.6, rUnknown, Mon May 29 02:25:32 CDT 2017

7.4 查看用户

hbase(main):002:0> whoami
root (auth:SIMPLE)
    groups: root

7.5 查看帮助

hbase(main):003:0> help
HBase Shell, version 1.2.6, rUnknown, Mon May 29 02:25:32 CDT 2017
Type 'help "COMMAND"', (e.g. 'help "get"' -- the quotes are necessary) for help on a specific command.
Commands are grouped. Type 'help "COMMAND_GROUP"', (e.g. 'help "general"') for help on a command group.

COMMAND GROUPS:
  Group name: general
  Commands: status, table_help, version, whoami

  Group name: ddl
  Commands: alter, alter_async, alter_status, create, describe, disable, disable_all, drop, drop_all, enable, enable_all, exists, get_table, is_disabled, is_enabled, list, locate_region, show_filters

  Group name: namespace
  Commands: alter_namespace, create_namespace, describe_namespace, drop_namespace, list_namespace, list_namespace_tables

  Group name: dml
  Commands: append, count, delete, deleteall, get, get_counter, get_splits, incr, put, scan, truncate, truncate_preserve

  Group name: tools
  Commands: assign, balance_switch, balancer, balancer_enabled, catalogjanitor_enabled, catalogjanitor_run, catalogjanitor_switch, close_region, compact, compact_rs, flush, major_compact, merge_region, move, normalize, normalizer_enabled, normalizer_switch, split, trace, unassign, wal_roll, zk_dump

  Group name: replication
  Commands: add_peer, append_peer_tableCFs, disable_peer, disable_table_replication, enable_peer, enable_table_replication, list_peers, list_replicated_tables, remove_peer, remove_peer_tableCFs, set_peer_tableCFs, show_peer_tableCFs

  Group name: snapshots
  Commands: clone_snapshot, delete_all_snapshot, delete_snapshot, list_snapshots, restore_snapshot, snapshot

  Group name: configuration
  Commands: update_all_config, update_config

  Group name: quotas
  Commands: list_quotas, set_quota

  Group name: security
  Commands: grant, list_security_capabilities, revoke, user_permission

  Group name: procedures
  Commands: abort_procedure, list_procedures

  Group name: visibility labels
  Commands: add_labels, clear_auths, get_auths, list_labels, set_auths, set_visibility

SHELL USAGE:
Quote all names in HBase Shell such as table and column names.  Commas delimit
command parameters.  Type <RETURN> after entering a command to run it.
Dictionaries of configuration used in the creation and alteration of tables are
Ruby Hashes. They look like this:

  {
    
    'key1' => 'value1', 'key2' => 'value2', ...}

and are opened and closed with curley-braces.  Key/values are delimited by the
'=>' character combination.  Usually keys are predefined constants such as
NAME, VERSIONS, COMPRESSION, etc.  Constants do not need to be quoted.  Type
'Object.constants' to see a (messy) list of all constants in the environment.

If you are using binary keys or values and need to enter them in the shell, use
double-quote'd hexadecimal representation. For example:

  hbase> get 't1', "key\x03\x3f\xcd"
  hbase> get 't1', "key\003\023\011"
  hbase> put 't1', "test\xef\xff", 'f1:', "\x01\x33\x40"

The HBase shell is the (J)Ruby IRB with the above HBase-specific commands added.
For more on the HBase Shell, see http://hbase.apache.org/book.html

在返回的帮助信息中,列出了HBase Shell的所有命令。通过help 'command’命令可以查看command的详细用法。

hbase(main):002:0> help 'status'
Show cluster status. Can be 'summary', 'simple', 'detailed', or 'replication'. The
default is 'summary'. Examples:

  hbase> status
  hbase> status 'simple'
  hbase> status 'summary'
  hbase> status 'detailed'
  hbase> status 'replication'
  hbase> status 'replication', 'source'
  hbase> status 'replication', 'sink'
hbase(main):003:0> help 'version'
Output this HBase version
hbase(main):004:0> status 'simple'
active master:  localhost:37695 1654912011953
0 backup masters
1 live servers
    localhost:36999 1654912013866
        requestsPerSecond=0.0, numberOfOnlineRegions=2, usedHeapMB=15, maxHeapMB=237, numberOfStores=2, numberOfStorefiles=2, storefileUncompressedSizeMB=0, storefileSizeMB=0, memstoreSizeMB=0, storefileIndexSizeMB=0, readRequestsCount=10, writeRequestsCount=1, rootIndexSizeKB=0, totalStaticIndexSizeKB=0, totalStaticBloomSizeKB=0, totalCompactingKVs=13, currentCompactedKVs=13, compactionProgressPct=1.0, coprocessors=[MultiRowMutationEndpoint]
0 dead servers
Aggregate load: 0, regions: 2

hbase(main):005:0> status 'summary'
1 active master, 0 backup masters, 1 servers, 0 dead, 2.0000 average load

7.6 退出HBase shell命令行

hbase(main):002:0> quit
#或者
hbase(main):002:0> exit

8.常用的Namespace操作

8.1查看所有的Namespace

hbase(main):002:0>list_namespace


hbase(main):001:0> list_namespace
NAMESPACE                                                                                                             
default                                                                                                               
hbase                                                                                                                 
2 row(s) in 0.3540 seconds

由返回结果可知,HBase默认定义两个Namespace:hbase和default。hbase包括两个系统表namespace和meta。default主要存放用户建表时未指定Namespace的表。

8.2 创建Namespace ns(ns为Namespace的名称)

hbase(main):002:0>create_namespace 'ns'

hbase(main):002:0> create_namespace 'ns'
0 row(s) in 0.0570 seconds
hbase(main):014:0> list_namespace
NAMESPACE                                                                                                             
default                                                                                                               
hbase                                                                                                                 
ns                                                                                                                    
3 row(s) in 0.0220 seconds

8.3查看指定Namespace下的所有表

hbase(main):022:0> list_namespace_tables 'hbase'
TABLE                                                                                                                 
meta                                                                                                                  
namespace                                                                                                             
2 row(s) in 0.0390 seconds

8.4 删除namespace ns

hbase(main):028:0> list_namespace
NAMESPACE                                                                                                             
default                                                                                                               
hbase                                                                                                                 
ns                                                                                                                    
3 row(s) in 0.0260 seconds

hbase(main):029:0> drop_namespace 'ns'
0 row(s) in 0.0610 seconds

hbase(main):030:0> list_namespace
NAMESPACE                                                                                                             
default                                                                                                               
hbase                                                                                                                                                                                                                                  
2 row(s) in 0.0260 seconds

9 HBase中表的操作

9.1.1 创建表时指定表名和列族

语法:
create ‘namespace名称:表名称’ ‘列族名称1’,‘列族名称2’…

在Namespace ns下创建表t1,并添加3个列族f1、f2、f3

hbase(main):001:0> create_namespace 'ns'
0 row(s) in 0.4080 seconds

hbase(main):002:0> create 'ns:t1', 'f1','f2','f3'
0 row(s) in 1.5270 seconds

=> Hbase::Table - ns:t1

9.1.2 创建表时使用default

语法:
create ‘表名称’ ‘列族名称1’,‘列族名称2’…

hbase(main):013:0* create 't1', 'f1','f2','f3'
0 row(s) in 1.2350 seconds

=> Hbase::Table - t1

9.2 查看指定的表

9.2.1 查看所有的表

=> ["ns:t1f1", "t1f1"]
hbase(main):019:0> list
TABLE                                                                                                                 
ns:t1                                                                                                               
t1                                                                                                                 
2 row(s) in 0.0140 seconds

=> ["ns:t1", "t1"]

9.2.2 查看指定的表是否存在

hbase(main):034:0> list 't1'
TABLE                                                                                                                 
t1                                                                                                                    
1 row(s) in 0.0070 seconds

=> ["t1"]
hbase(main):035:0> exists 't1'
Table t1 does exist                                                                                                   
0 row(s) in 0.0250 seconds

9.3 查看表的描述

hbase(main):046:0> desc 't1'
Table t1 is ENABLED                                                                                                   
t1                                                                                                                    
COLUMN FAMILIES DESCRIPTION                                                                                           
{
    
    NAME => 'f1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_
ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '
65536', REPLICATION_SCOPE => '0'}                                                                                     
{
    
    NAME => 'f2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_
ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '
65536', REPLICATION_SCOPE => '0'}                                                                                     
{
    
    NAME => 'f3', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_
ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '
65536', REPLICATION_SCOPE => '0'}                                                                                     
3 row(s) in 0.0620 seconds



hbase(main):050:0> desc 'ns:t1'
Table ns:t1 is ENABLED                                                                                                
ns:t1                                                                                                                 
COLUMN FAMILIES DESCRIPTION                                                                                           
{
    
    NAME => 'f1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_
ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '
65536', REPLICATION_SCOPE => '0'}                                                                                     
{
    
    NAME => 'f2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_
ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '
65536', REPLICATION_SCOPE => '0'}                                                                                     
{
    
    NAME => 'f3', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_
ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '
65536', REPLICATION_SCOPE => '0'}                                                                                     
3 row(s) in 0.0570 seconds
字段 解释
NAME => ‘f1’ 列族的名称是f1
BLOOMFILTER => ‘ROW’ 布隆过滤器的类型是ROW
VERSIONS => ‘1’ 保存的版本数是1
IN_MEMORY => ‘false’ IN_MEMORY 常驻Cache
KEEP_DELETED_CELLS => ‘FALSE’ 是否保留被删除的Cell
DATA_BLOCK_ENCODING => ‘NONE’ 数据块编码为None,表示不使用数据块编码
TTL => ‘FOREVER’ 存在时间值,hbase在达到时间后删除行
COMPRESSION => ‘NONE’ 压缩算法,为None表示不使用压缩算法
MIN_VERSIONS => ‘0’ 最小版本为0,表示改版本已经被禁用
BLOCKCACHE => ‘true’ 数据块的缓存属性
REPLICATION_SCOPE => ‘0’ 是否复制列表,0表示禁用1表示启用
列族属性讲解

9.4启用表

1.新建表完成后,表默认处于启用状态。

hbase(main):002:0> enable 't1'
0 row(s) in 0.2270 seconds

2.判断指定表是否被启用

hbase(main):002:0> is_disabled 't1'
false                                                                                                                 
0 row(s) in 0.0210 seconds

hbase(main):003:0> is_enabled 't1'
true                                                                                                                  
0 row(s) in 0.0850 seconds

3.禁用表

hbase(main):001:0> disable 't1'
0 row(s) in 3.0850 seconds

hbase(main):002:0> is_enabled 't1'
false                                                                                                                 
0 row(s) in 0.0500 seconds

9.5 向表中添加列族

hbase(main):013:0> alter 't1','f4'
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 1.9560 seconds

hbase(main):014:0> desc 't1'
Table t1 is ENABLED                                                                                                   
t1                                                                                                                    
COLUMN FAMILIES DESCRIPTION                                                                                           
{
    
    NAME => 'f1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_
ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '
65536', REPLICATION_SCOPE => '0'}                                                                                     
{
    
    NAME => 'f2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_
ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '
65536', REPLICATION_SCOPE => '0'}                                                                                     
{
    
    NAME => 'f3', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_
ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '
65536', REPLICATION_SCOPE => '0'}                                                                                     
{
    
    NAME => 'f4', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_
ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '
65536', REPLICATION_SCOPE => '0'}                                                                                     
4 row(s) in 0.0440 seconds

9.6 删除列族

#一般需要先disable
disable 't1'


hbase(main):038:0> alter 't1', {
    
    NAME => 'f4', METHOD => 'delete'}
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 2.0170 seconds

hbase(main):039:0> desc 't1'
Table t1 is ENABLED                                                                                                   
t1                                                                                                                    
COLUMN FAMILIES DESCRIPTION                                                                                           
{
    
    NAME => 'f1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_
ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '
65536', REPLICATION_SCOPE => '0'}                                                                                     
{
    
    NAME => 'f2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_
ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '
65536', REPLICATION_SCOPE => '0'}                                                                                     
{
    
    NAME => 'f3', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_
ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '
65536', REPLICATION_SCOPE => '0'}                                                                                     
3 row(s) in 0.0330 seconds

10.表中数据的增删改查

数据的添加、删除、修改、查询(增删改查)
在这里插入图片描述

10.1 向表中添加或更新数据

语法:put ‘表名称’,‘Rowkey名称’, ‘列族名称:列名称’,‘值’

hbase(main):063:0> put 't1','row1','f1:id','1'
0 row(s) in 0.0270 seconds

hbase(main):061:0> put 't1','row1','f1:name','tom'
0 row(s) in 0.0200 seconds

hbase(main):062:0> put 't1','row1','f1:age','21'
0 row(s) in 0.0180 seconds

hbase(main):063:0> put 't1','row1','f2:id','2'
0 row(s) in 0.0270 seconds

hbase(main):064:0> put 't1','row1','f2:name','jack'
0 row(s) in 0.0170 seconds

hbase(main):065:0> put 't1','row1','f2:age','22'
0 row(s) in 0.0060 seconds

hbase(main):066:0> put 't1','row2','f1:city','shanghai'
0 row(s) in 0.0130 seconds

hbase(main):067:0> put 't1','row3','f1:country','china'
0 row(s) in 0.0110 seconds

10.2 更新表中的数据

hbase(main):094:0> put 't1','row2','f1:city','beijing'
0 row(s) in 0.2670 seconds

10.3 查看表中的数据

语法:scan ‘表名称’,{COLUMNS =>[‘列族名:列名’,…],LIMIT=>行数}

10.3.1查看指定表中的所有数据

hbase(main):097:0> scan 't1'
ROW                            COLUMN+CELL                                                                            
 row1                          column=f1:age, timestamp=1654962083217, value=21                                       
 row1                          column=f1:id, timestamp=1654962066766, value=1                                         
 row1                          column=f1:name, timestamp=1654962075296, value=tom                                     
 row1                          column=f2:age, timestamp=1654962105355, value=22                                       
 row1                          column=f2:id, timestamp=1654962105184, value=2                                         
 row1                          column=f2:name, timestamp=1654962105203, value=jack                                    
 row2                          column=f1:city, timestamp=1654962243269, value=beijing                                 
 row3                          column=f1:country, timestamp=1654962083426, value=china                                
3 row(s) in 0.0860 seconds

10.3.2查看指定表的指定列的所有数据

hbase(main):112:0> scan 't1',{
    
    COLUMNS => ['f1:id','f2:id']}
ROW                            COLUMN+CELL                                                                            
 row1                          column=f1:id, timestamp=1654962066766, value=1                                         
 row1                          column=f2:id, timestamp=1654962105184, value=2                                         
1 row(s) in 0.0480 seconds

10.3.3查看指定表的指定列的前n行数据

hbase(main):118:0> scan 't1',{
    
    COLUMNS => ['f1:id','f1:name','f1:age','f1:city','f1:country'], LIMIT => 2}
ROW                            COLUMN+CELL                                                                            
 row1                          column=f1:age, timestamp=1654962083217, value=21                                       
 row1                          column=f1:id, timestamp=1654962066766, value=1                                         
 row1                          column=f1:name, timestamp=1654962075296, value=tom                                     
 row2                          column=f1:city, timestamp=1654962243269, value=beijing                                 
2 row(s) in 0.0280 seconds

10.3.3 获取表中指定Rowkey下的数据

语法:get ‘表名称’,‘Rowkey’

hbase(main):127:0> get 't1','row2'
COLUMN                         CELL                                                                                   
 f1:city                       timestamp=1654962243269, value=beijing                                                 
1 row(s) in 0.0140 seconds

hbase(main):128:0> get 't1','row1'
COLUMN                         CELL                                                                                   
 f1:age                        timestamp=1654962083217, value=21                                                      
 f1:id                         timestamp=1654962066766, value=1                                                       
 f1:name                       timestamp=1654962075296, value=tom                                                     
 f2:age                        timestamp=1654962105355, value=22                                                      
 f2:id                         timestamp=1654962105184, value=2                                                       
 f2:name                       timestamp=1654962105203, value=jack                                                    
6 row(s) in 0.0340 seconds

10.3.4获取指定表的指定Rowkey的指定列族下所有数据

语法: get ‘表名称’, ‘rowkey’, ‘列族名称’

hbase(main):143:0> get 't1', 'row1', 'f1'
COLUMN                         CELL                                                                                   
 f1:age                        timestamp=1654962083217, value=21                                                      
 f1:id                         timestamp=1654962066766, value=1                                                       
 f1:name                       timestamp=1654962075296, value=tom                                                     
3 row(s) in 0.0190 seconds

hbase(main):144:0> get 't1', 'row1', 'f2'
COLUMN                         CELL                                                                                   
 f2:age                        timestamp=1654962105355, value=22                                                      
 f2:id                         timestamp=1654962105184, value=2                                                       
 f2:name                       timestamp=1654962105203, value=jack                                                    
3 row(s) in 0.0100 seconds

10.3.5获取指定表的指定Rowkey的指定列的数据

语法:get ‘表名称’, ‘rowkey’, ‘列名称’

hbase(main):161:0> get 't1', 'row1', 'f2:name'
COLUMN                         CELL                                                                                   
 f2:name                       timestamp=1654962105203, value=jack                                                    
1 row(s) in 0.0120 seconds

hbase(main):162:0> get 't1', 'row1', 'f1:age'
COLUMN                         CELL                                                                                   
 f1:age                        timestamp=1654962083217, value=21                                                      
1 row(s) in 0.0070 seconds

10.4 统计表中的总行数

hbase(main):172:0> count 't1'
3 row(s) in 0.0510 seconds

=> 3

10.5 删除数据

10.5.1删除一个单元格的数据

delete ‘表名称’, ‘rowkey’, ‘列名称’,‘时间戳’

hbase(main):177:0> delete 't1','row1','f1:age'
0 row(s) in 0.0300 seconds

hbase(main):180:0> scan 't1'
ROW                            COLUMN+CELL                                                                            
 row1                          column=f1:id, timestamp=1654962066766, value=1                                         
 row1                          column=f1:name, timestamp=1654962075296, value=tom                                     
 row1                          column=f2:age, timestamp=1654962105355, value=22                                       
 row1                          column=f2:id, timestamp=1654962105184, value=2                                         
 row1                          column=f2:name, timestamp=1654962105203, value=jack                                    
 row2                          column=f1:city, timestamp=1654962243269, value=beijing                                 
 row3                          column=f1:country, timestamp=1654962083426, value=china                                
3 row(s) in 0.0220 seconds

10.5.2 删除指定列的数据

delete ‘表名称’, ‘rowkey’, ‘列名称’

hbase(main):003:0> delete 't1','row1','f1:name'
0 row(s) in 0.0100 seconds

hbase(main):001:0> scan 't1'
ROW                            COLUMN+CELL                                                                            
 row1                          column=f1:id, timestamp=1654962066766, value=1                                         
 row1                          column=f2:age, timestamp=1654962105355, value=22                                       
 row1                          column=f2:id, timestamp=1654962105184, value=2                                         
 row1                          column=f2:name, timestamp=1654962105203, value=jack                                    
 row2                          column=f1:city, timestamp=1654962243269, value=beijing                                 
 row3                          column=f1:country, timestamp=1654962083426, value=china                                
3 row(s) in 0.5370 seconds

10.5.3 删除指定行下的所有数据

hbase(main):013:0> deleteall 't1','row2'
0 row(s) in 0.0700 seconds

hbase(main):014:0> scan 't1'
ROW                            COLUMN+CELL                                                                            
 row1                          column=f1:id, timestamp=1654962066766, value=1                                         
 row1                          column=f2:age, timestamp=1654962105355, value=22                                       
 row1                          column=f2:id, timestamp=1654962105184, value=2                                         
 row1                          column=f2:name, timestamp=1654962105203, value=jack                                    
 row3                          column=f1:country, timestamp=1654962083426, value=china                                
2 row(s) in 0.0270 seconds

10.5.4 删除表中所有的数据

hbase(main):031:0> truncate 't1'
Truncating 't1' table (it may take a while):
 - Disabling table...
 - Truncating table...
0 row(s) in 3.5940 seconds
hbase(main):038:0> is_enabled 't1'
true                                                                                                                  
0 row(s) in 0.0180 seconds

10 HBase中多版本的获取方法

10.1 在shell端创建一个Hbase表

hbase(main):042:0> create 't2','f1'
0 row(s) in 1.2590 seconds

=> Hbase::Table - t2

10.2 查看表结构

hbase(main):047:0> describe 't2'
Table t2 is ENABLED                                                                                                   
t2                                                                                                                    
COLUMN FAMILIES DESCRIPTION                                                                                           
{
    
    NAME => 'f1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_
ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '
65536', REPLICATION_SCOPE => '0'}                                                                                     
1 row(s) in 0.0470 seconds

从上面的表结构中,我们可以看到,VERSIONS为1,也就是说,默认情况只会存取一个版本的列数据,当再次插入的时候,后面的值会覆盖前面的值。

10.3 修改表结构

修改表结构,让Hbase表支持存储3个VERSIONS的版本列数据

hbase(main):056:0> alter 't2',{
    
    NAME=>'f1',VERSIONS=>3}
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 1.9170 seconds

再次查看表结构:

hbase(main):062:0> desc 't2'
Table t2 is ENABLED                                                                                                   
t2                                                                                                                    
COLUMN FAMILIES DESCRIPTION                                                                                           
{
    
    NAME => 'f1', BLOOMFILTER => 'ROW', VERSIONS => '3', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_
ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '
65536', REPLICATION_SCOPE => '0'}                                                                                     
1 row(s) in 0.0250 seconds

我们会发现VERSIONS已经修改成了3.

4、插入3行数据

hbase(main):071:0>  put 't2','rowkey1','f1:name','tom'
0 row(s) in 0.0240 seconds

hbase(main):075:0> put 't2','rowkey1','f1:name','jack'
0 row(s) in 0.0310 seconds

hbase(main):076:0> put 't2','rowkey1','f1:name','lily'
0 row(s) in 0.0150 seconds

hbase(main):077:0> get 't2','rowkey1','f1:name'
COLUMN                         CELL                                                                                   
 f1:name                       timestamp=1654964444338, value=lily                                                    
1 row(s) in 0.0240 seconds

hbase(main):090:0> scan 't2'
ROW                            COLUMN+CELL                                                                            
 rowkey1                       column=f1:name, timestamp=1654964444338, value=lily                                    
1 row(s) in 0.0480 seconds

从上面可以看出,插入了3行数据到表中,并且3行数据的rowkey一致,然后使用get命令来获取这一行数据,发现只返回了最新的一行数据。

5、获取多行数据方法
hbase(main):095:0> get 't2','rowkey1',{
    
    COLUMN=>'f1:name',VERSIONS=>3}
COLUMN                         CELL                                                                                   
 f1:name                       timestamp=1654964444338, value=lily                                                    
 f1:name                       timestamp=1654964436752, value=jack                                                    
 f1:name                       timestamp=1654964406100, value=tom                                                     
3 row(s) in 0.0180 seconds

hbase(main):096:0> get 't2','rowkey1',{
    
    COLUMN=>'f1:name',VERSIONS=>2}
COLUMN                         CELL                                                                                   
 f1:name                       timestamp=1654964444338, value=lily                                                    
 f1:name                       timestamp=1654964436752, value=jack                                                    
2 row(s) in 0.0150 seconds

从上面的测试结果中,可以看出,一次性获取了个版本的数据。

问题

HBase启动遇到VM warning ignoring option PermSize=128m support was removed in 8.0

根据hbase-env.sh配置文件,当使用JDK8+版本时将相关信息注释掉即可

[root@localhost conf]# vi $HBASE_HOME/conf/hbase-env.sh
# Configure PermSize. Only needed in JDK7. You can safely remove it for JDK8+
#export HBASE_MASTER_OPTS="$HBASE_MASTER_OPTS -XX:PermSize=128m -XX:MaxPermSize=128m"
#export HBASE_REGIONSERVER_OPTS="$HBASE_REGIONSERVER_OPTS -XX:PermSize=128m -XX:MaxPermSize=128m"


猜你喜欢

转载自blog.csdn.net/yandao/article/details/125230905