Hbase入门操作详解(单机模式)

本文详细介绍了如何在单机环境下配置、启动、操作HBase,包括创建表、建表管理、插入数据、查询数据、删除表及数据、修改表等核心功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.配置Hbase

1.单机配置hbase-site.xml

[root@master1 ~]# vim /etc/hbase/conf/hbase-site.xml

添加:

<configuration>
        <property>
                <name>hbase.rootdir</name>
                <value>file:///tmp</value>
        </property>

        <property>
                <name>hbase.cluster.distributed</name>
                <value>false</value>
        </property>

        <property>
                <name>hbase.zookeeper.property.clientPort</name>
                <value>2182</value>
        </property>

</configuration>

2.修改环境变量

(1)

[root@master1 ~]# vim /etc/init.d/hbase-master +55

添加:

export HBASE_HOME="/usr/hdp/current/hbase-master/../hbase"

或者:

export HBASE_HOME="/usr/hdp/2.6.3.0-235/hbase"

(2)

[root@master1 ~]# vim /etc/hbase/conf/hbase-env.sh

添加:

export HBASE_PID_DIR=/var/run/hbase
export HBASE_LOG_DIR=/var/log/hbase

二.启动单机服务器

1.启动服务器

[root@master1 ~]# /etc/init.d/hbase-master start
Starting HBase master daemon (hbase-master):               [  确定  ]
starting master, logging to /var/log/hbase/hbase-hbase-master-master1.out

2.关闭服务器

[root@master1 ~]# /etc/init.d/hbase-master stop
Stopping HBase master daemon (hbase-master):               [  确定  ]
stopping master.

3.连接服务器

[root@master1 ~]# hbase shell

显示:

[root@master1 ~]# hbase shell
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 1.1.2.2.6.3.0-235, r6f982c8e667e7a18451fcbc12c5e2758b178ec78, Mon Oct 30 02:42:02 UTC 2017

hbase(main):001:0> 

三.建表

举栗子:

1.创建表:create ‘表名’,‘列族’

hbase(main):001:0> create 'test','cf'
0 row(s) in 11.7320 seconds

=> Hbase::Table - test

2.表的管理/列表查看表:list

hbase(main):002:0> list 'test'
TABLE                                                                                      
test                                                                                       
1 row(s) in 0.1060 seconds

=> ["test"]

3.查看表结构:describe  ‘表名’

hbase(main):003:0> describe 'test'
Table test is ENABLED                                                                      
test                                                                                       
COLUMN FAMILIES DESCRIPTION                                                                
{NAME => 'cf', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CE
LLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN
_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}    
1 row(s) in 3.8210 seconds

4.插入数据:put ‘表名’,‘rowkey’,‘列族:列’,‘值’

hbase(main):004:0> put 'test','row1','cf:a','wugenqiang'
0 row(s) in 1.7430 seconds

hbase(main):005:0> put 'test','row1','cf:b','menglinlin'
0 row(s) in 0.0500 seconds

hbase(main):006:0> put 'test','row2','cf:b','marry'
0 row(s) in 0.0450 seconds

hbase(main):007:0> put 'test','row3','cf:c','marry me'
0 row(s) in 0.0160 seconds

5.查询数据

(1)全局扫描:scan '表名' 

hbase(main):008:0> scan 'test'
ROW                     COLUMN+CELL                                                        
 row1                   column=cf:a, timestamp=1532048846590, value=wugenqiang             
 row1                   column=cf:b, timestamp=1532049031503, value=menglinlin             
 row2                   column=cf:b, timestamp=1532049063608, value=marry                  
 row3                   column=cf:c, timestamp=1532049086522, value=marry me               
3 row(s) in 0.0860 seconds

(2)查询某行数据:get ‘表名’,‘rowkey’

hbase(main):009:0> get 'test','row3'
COLUMN                  CELL                                                               
 cf:c                   timestamp=1532049086522, value=marry me                            
1 row(s) in 0.1000 seconds

(3)查询表中数据行数:count ‘表名’

hbase(main):010:0> count 'test'
3 row(s) in 0.0720 seconds

=> 3

6.删除表

(1)先将表禁用:disable ‘表名’ (修改表同样需要先禁用)

hbase(main):012:0> disable 'wugenqiang'
0 row(s) in 2.5400 seconds

(2)删除表:drop ‘表名’

hbase(main):013:0> drop 'wugenqiang'
0 row(s) in 3.5240 seconds

(3)验证:

hbase(main):016:0> list
TABLE                                                                                      
test                                                                                       
wugenqiang                                                                                 
2 row(s) in 0.0120 seconds

=> ["test", "wugenqiang"]

hbase(main):012:0> disable 'wugenqiang'
0 row(s) in 2.5400 seconds

hbase(main):013:0> drop 'wugenqiang'
0 row(s) in 3.5240 seconds

hbase(main):014:0> list
TABLE                                                                                      
test                                                                                       
1 row(s) in 0.0220 seconds

=> ["test"]

(3)删除行中的某列:delete ‘表名’,‘rowkey’,‘列族:列’

hbase(main):019:0> scan 'test'
ROW                     COLUMN+CELL                                                        
 row1                   column=cf:a, timestamp=1532048846590, value=wugenqiang             
 row1                   column=cf:b, timestamp=1532049031503, value=menglinlin             
 row2                   column=cf:b, timestamp=1532049063608, value=marry                  
 row3                   column=cf:c, timestamp=1532049086522, value=marry me               
3 row(s) in 0.0630 seconds

hbase(main):020:0> delete 'test','row2','cf:b'
0 row(s) in 0.5470 seconds

hbase(main):021:0> scan 'test'
ROW                     COLUMN+CELL                                                        
 row1                   column=cf:a, timestamp=1532048846590, value=wugenqiang             
 row1                   column=cf:b, timestamp=1532049031503, value=menglinlin             
 row3                   column=cf:c, timestamp=1532049086522, value=marry me               
2 row(s) in 0.0330 seconds

(4)删除行:deleteall ‘表名’,‘rowkey’

hbase(main):023:0> deleteall 'test','row1'
0 row(s) in 0.0140 seconds

hbase(main):024:0> scan 'test'
ROW                     COLUMN+CELL                                                        
 row3                   column=cf:c, timestamp=1532049086522, value=marry me               
1 row(s) in 0.0610 seconds

(5)删除表中所有数据:truncate ‘表名’

hbase(main):028:0> truncate 'test'
Truncating 'test' table (it may take a while):
 - Disabling table...
 - Truncating table...
0 row(s) in 3.4770 seconds

hbase(main):029:0> scan 'test'
ROW                     COLUMN+CELL                                                        
0 row(s) in 0.4310 seconds

7.修改表:

(1)添加列族:alter ‘表名’,NAME=>'second'

hbase(main):031:0> alter 'test',NAME=>'second'
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 3.3390 seconds

验证:

hbase(main):037:0> describe 'test'
Table test is ENABLED                                                                      
test                                                                                       
COLUMN FAMILIES DESCRIPTION                                                                
{NAME => 'cf', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CE
LLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN
_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}    
{NAME => 'second', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETE
D_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE',
 MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
2 row(s) in 0.0390 seconds

(2)删除列族:alter ‘表名’,NAME=>'second',METHOD=>'delete'

hbase(main):033:0> alter 'test',NAME=>'second',METHOD=>'delete'
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 2.3340 seconds

验证:

enable ‘tablename’ 
启用emp表并验证表是否被启动。

hbase(main):034:0> enable 'test'
0 row(s) in 0.0430 seconds

hbase(main):035:0> describe 'test'
Table test is ENABLED                                                                      
test                                                                                       
COLUMN FAMILIES DESCRIPTION                                                                
{NAME => 'cf', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CE
LLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN
_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}    
1 row(s) in 0.0330 seconds

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WuGenQiang

谢谢你的喜欢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值