HBase 为用户提供了一个非常方便的使用方式, 我们称之为“HBase Shell”。
HBase Shell 提供了大多数的 HBase 命令, 通过 HBase Shell 用户可以方便地创建、删除及修改表, 还可以向表中添加数据、列出表中的相关信息等。
备注:写错HBase Shell命令时用键盘上的“Delete”进行向前删除,“Backspace”向后删除。
在启动 HBase 之后,用户可以通过下面的命令进入 HBase Shell 之中,命令如下所示:
[root@masterhbase0941]# bin/hbase shell
HBase Shell; enter'help<RETURN>' for list of supported commands.
Type"exit<RETURN>" to leave the HBase Shell
Version 0.94.1,r1365210, Tue Jul 24 18:40:10 UTC 2012
hbase(main):001:0>
常用的HBase Shell 命令如下表 1.1-1 所示:
下面是一些常见命令的说明,在hbase shell中输入help,会显示命令行支持的命令列表,通过help ‘命令名’可以显示命令的详细说明及示例:
hbase(main):017:0>help
HBase Shell, version 0.94.1, r1365210, Tue Jul 24 18:40:10UTC 2012
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 acommand group.
COMMAND GROUPS:
Group name: general
Commands: status,version, whoami
Group name: ddl
Commands: alter,alter_async, alter_status, create, describe, disable, disable_all, drop,drop_all, enable, enable_all, exists, is_disabled, is_enabled, list,show_filters
Group name: dml
Commands: count,delete, deleteall, get, get_counter, incr, put, scan, truncate
Group name: tools
Commands: assign,balance_switch, balancer, close_region, compact, flush, hlog_roll,major_compact, move, split, unassign, zk_dump
Group name:replication
Commands: add_peer,disable_peer, enable_peer, list_peers, remove_peer, start_replication,stop_replication
Group name:security
Commands: grant,revoke, user_permission
SHELL USAGE:
Quote all names in HBase Shell such as table and columnnames. Commas delimit
command parameters. Type <RETURN> after entering a command to run it.
Dictionaries of configuration used in the creation andalteration 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 constantsin the environment.
If you are using binary keys or values and need to enterthem 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 aboveHBase-specific commands added.
For more on the HBase Shell, see http://hbase.apache.org/docs/current/book.html
hbase(main):018:0>
hbase(main):001:0>help 'create'
Create table; pass table name, a dictionary ofspecifications per
column family, and optionally a dictionary of tableconfiguration.
Dictionaries are described below in the GENERAL NOTESsection.
Examples:
hbase> create't1', {NAME => 'f1', VERSIONS => 5}
hbase> create't1', {NAME => 'f1'}, {NAME => 'f2'}, {NAME => 'f3'}
hbase> # Theabove in shorthand would be the following:
hbase> create't1', 'f1', 'f2', 'f3'
hbase> create't1', {NAME => 'f1', VERSIONS => 1, TTL => 2592000, BLOCKCACHE =>true}
hbase> create't1', 'f1', {SPLITS => ['10', '20', '30', '40']}
hbase> create't1', 'f1', {SPLITS_FILE => 'splits.txt'}
hbase> #Optionally pre-split the table into NUMREGIONS, using
hbase> #SPLITALGO ("HexStringSplit", "UniformSplit" or classname)
hbase> create't1', 'f1', {NUMREGIONS => 15, SPLITALGO => 'HexStringSplit'}
hbase(main):002:0>
一、一般操作
1.查询服务器状态
hbase(main):024:0>status
3 servers, 0 dead,1.0000 average load
2.查询hbase版本
hbase(main):001:0> version
0.94.1, r1365210, Tue Jul 24 18:40:10 UTC 2012
3.查看用户身份
hbase(main):002:0> whoami
root
二、DDL操作
1.创建一个表
hbase(main):011:0>create'member','member_id','address','info'
0 row(s) in 1.2210seconds
2.获得表的描述
hbase(main):012:0>list
TABLE
member
1 row(s) in 0.0160seconds
hbase(main):006:0>describe 'member'
DESCRIPTION ENABLED
{NAME => 'member', FAMILIES => [{NAME=>'address', BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0',true
VERSIONS => '3', COMPRESSION => 'NONE',TTL=> '2147483647', BLOCKSIZE => '65536', IN_MEMORY =>'fa
lse', BLOCKCACHE => 'true'}, {NAME =>'info',BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0',VERSI
ONS => '3', COMPRESSION => 'NONE', TTL=>'2147483647', BLOCKSIZE => '65536', IN_MEMORY =>'false',
BLOCKCACHE =>'true'}]}
1 row(s) in 0.0230seconds
3.删除一个列族,alter,disable,enable
我们之前建了3个列族,但是发现member_id这个列族是多余的,因为他就是主键,所以我们要将其删除。
hbase(main):003:0>alter'member',{NAME=>'member_id',METHOD=>'delete'}
ERROR: Table memberis enabled. Disable it first beforealtering.
报错,删除列族的时候必须先将表给disable掉。
hbase(main):004:0>disable'member'
0 row(s) in 2.0390seconds
hbase(main):005:0>alter'member',{NAME=>'member_id',METHOD=>'delete'}
0 row(s) in 0.0560seconds
hbase(main):006:0>describe 'member'
DESCRIPTION ENABLED
{NAME => 'member', FAMILIES => [{NAME=>'address', BLOOMFILTER => 'NONE', REPLICATION_SCOPE =>'0',false
VERSIONS => '3', COMPRESSION => 'NONE',TTL=> '2147483647', BLOCKSIZE => '65536', IN_MEMORY =>'fa
lse', BLOCKCACHE => 'true'}, {NAME =>'info',BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0',VERSI
ONS => '3', COMPRESSION => 'NONE', TTL=>'2147483647', BLOCKSIZE => '65536', IN_MEMORY =>'false',
BLOCKCACHE =>'true'}]}
1 row(s) in 0.0230seconds
该列族已经删除,我们继续将表enable
hbase(main):008:0> enable 'member'
0 row(s) in 2.0420seconds
4.列出所有的表
hbase(main):028:0>list
TABLE
member
temp_table
2 row(s) in 0.0150seconds
5.drop一个表
hbase(main):029:0>disable 'temp_table'
0 row(s) in 2.0590seconds
hbase(main):030:0>drop 'temp_table'
0 row(s) in 1.1070seconds
6.查询表是否存在
hbase(main):021:0>exists 'member'
Table memberdoesexist
0 row(s) in 0.1610seconds
7.判断表是否enable
hbase(main):034:0>is_enabled 'member'
true
0 row(s) in 0.0110seconds
8.判断表是否disable
hbase(main):032:0>is_disabled 'member'
false
0 row(s) in 0.0110seconds
三、DML操作
1.插入几条记录
put'member','scutshuxue','info:age','24'
put'member','scutshuxue','info:birthday','1987-06-17'
put'member','scutshuxue','info:company','alibaba'
put'member','scutshuxue','address:contry','china'
put'member','scutshuxue','address:province','zhejiang'
put'member','scutshuxue','address:city','hangzhou'
put'member','xiaofeng','info:birthday','1987-4-17'
put'member','xiaofeng','info:favorite','movie'
put'member','xiaofeng','info:company','alibaba'
put'member','xiaofeng','address:contry','china'
put'member','xiaofeng','address:province','guangdong'
put'member','xiaofeng','address:city','jieyang'
put'member','xiaofeng','address:town','xianqiao'
2.获取一条数据
获取一个id的所有数据
hbase(main):001:0>get 'member','scutshuxue'
COLUMN CELL
address:city timestamp=1321586240244,value=hangzhou
address:contry timestamp=1321586239126,value=china
address:province timestamp=1321586239197,value=zhejiang
info:age timestamp=1321586238965,value=24
info:birthday timestamp=1321586239015,value=1987-06-17
info:company timestamp=1321586239071,value=alibaba
6 row(s) in 0.4720seconds
获取一个id,一个列族的所有数据
hbase(main):002:0>get 'member','scutshuxue','info'
COLUMN CELL
info:age timestamp=1321586238965,value=24
info:birthday timestamp=1321586239015,value=1987-06-17
info:company timestamp=1321586239071,value=alibaba
3 row(s) in 0.0210seconds
获取一个id,一个列族中一个列的所有数据
hbase(main):002:0>get'member','scutshuxue','info:age'
COLUMN CELL
info:age timestamp=1321586238965,value=24
1 row(s) in 0.0320seconds
3.更新一条记录
将scutshuxue的年龄改成99
hbase(main):004:0>put 'member','scutshuxue','info:age','99'
0 row(s) in 0.0210seconds
hbase(main):005:0>get'member','scutshuxue','info:age'
COLUMN CELL
info:age timestamp=1321586571843,value=99
1 row(s) in 0.0180seconds
通过timestamp来获取两个版本的数据
hbase(main):010:0>get'member','scutshuxue',{COLUMN=>'info:age',TIMESTAMP=>1321586238965}
COLUMN CELL
info:age timestamp=1321586238965,value=24
1 row(s) in 0.0140seconds
hbase(main):011:0>get'member','scutshuxue',{COLUMN=>'info:age',TIMESTAMP=>1321586571843}
COLUMN CELL
info:age timestamp=1321586571843,value=99
1 row(s) in 0.0180seconds
4.全表扫描
hbase(main):013:0>scan 'member'
ROW COLUMN+CELL
scutshuxue column=address:city,timestamp=1321586240244, value=hangzhou
scutshuxue column=address:contry,timestamp=1321586239126,value=china
scutshuxue column=address:province,timestamp=1321586239197, value=zhejiang
scutshuxue column=info:age,timestamp=1321586571843, value=99
scutshuxue column=info:birthday,timestamp=1321586239015,value=1987-06-17
scutshuxue column=info:company,timestamp=1321586239071, value=alibaba
temp column=info:age,timestamp=1321589609775,value=59
xiaofeng column=address:city,timestamp=1321586248400, value=jieyang
xiaofeng column=address:contry,timestamp=1321586248316, value=china
xiaofeng column=address:province,timestamp=1321586248355,value=guangdong
xiaofeng column=address:town,timestamp=1321586249564, value=xianqiao
xiaofeng column=info:birthday,timestamp=1321586248202,value=1987-4-17
xiaofeng column=info:company,timestamp=1321586248277, value=alibaba
xiaofeng column=info:favorite,timestamp=1321586248241, value=movie
3 row(s) in 0.0570seconds
5.删除id为temp的值的‘info:age’字段
hbase(main):016:0>delete 'member','temp','info:age'
0 row(s) in 0.0150seconds
hbase(main):018:0>get 'member','temp'
COLUMN CELL
0 row(s) in 0.0150seconds
6.删除整行
hbase(main):001:0>deleteall 'member','xiaofeng'
0 row(s) in 0.3990seconds
7.查询表中有多少行:
hbase(main):019:0>count'member'
2 row(s) in 0.0160seconds
8.给‘xiaofeng’这个id增加'info:age'字段,并使用counter实现递增
hbase(main):057:0*incr'member','xiaofeng','info:age'
COUNTER VALUE = 1
hbase(main):058:0>get'member','xiaofeng','info:age'
COLUMN CELL
info:age timestamp=1321590997648,value=\x00\x00\x00\x00\x00\x00\x00\x01
1 row(s) in 0.0140seconds
hbase(main):059:0>incr 'member','xiaofeng','info:age'
COUNTER VALUE = 2
hbase(main):060:0>get'member','xiaofeng','info:age'
COLUMN CELL
info:age timestamp=1321591025110,value=\x00\x00\x00\x00\x00\x00\x00\x02
1 row(s) in 0.0160seconds
注:incr的用法是先用incr ‘t1’, ‘r1’,‘c1’增加一列,
再用incr ‘t1’,’r1’,’c1’,1 来对这一列进行值增加。
获取当前count的值
hbase(main):069:0>get_counter'member','xiaofeng','info:age'
COUNTER VALUE = 2
9.将整张表清空:
hbase(main):035:0>truncate 'member'
Truncating 'member'table (it may take a while):
- Disabling table...
- Dropping table...
- Creating table...
0 row(s) in 4.3430seconds
可以看出,hbase是先将掉disable掉,然后drop掉后重建表来实现truncate的功能的。
附:DDL、DML和DCL的理解
1、DDL
1-1、DDL的概述
DDL(Data Definition Language 数据定义语言)用于操作对象和对象的属性,这种对象包括数据库本身,以及数据库对象,像:表、视图等等,DDL对这些对象和属性的管理和定义具体表现在Create、Drop和Alter上。特别注意:DDL操作的 “对象”的概念,”对象“包括对象及对象的属性,而且对象最小也比记录大个层次。以表举例:Create创建数据表,Alter可以更改该表的字段,Drop可以删除这个表,从这里我们可以看到,DDL所站的高度,他不会对具体的数据进行操作。
1-2、DDL的主要语句(操作)
Create语句:可以创建数据库和数据库的一些对象。
Drop语句:可以删除数据表、索引、触发程序、条件约束以及数据表的权限等。
Alter语句:修改数据表定义及属性。
1-3、DDL的操作对象(表)
1-3-1、表的概念
表的创建就是用来存放数据用的,由于我们存放的数据的不通,所以我们需要定义些数据类型,以方便管理。
1-3-2、表的属性
主键属性:主键就是主键约束,只不过起的名字不同了,主键的起名偏向于虚的(就是描述描述这件事),主键约束起名偏向于实得(就是描述操作的实施),描述的都是同一件事,主键约束就是表中的一个属性;在一个表中最多可以有一个主键;一个主键可以定义在一个或多个字段;主键使一个或多个字段的值必须唯一且不为空,这样做可以通过该字段或该组字段中的值唯一的代表一条记录。
唯一属性:一个表中只能有一个主键属性,为了方表用户,提出唯一约束;唯一约束可以定义在一个或多个字段上;唯一约束使该字段或该组字段中的值唯一,可以为空,但是,不能重复。
外键属性:又叫外键,又叫外键约束,跟主键和主键约束的关系是一样的;外键约束针对的两个表,如果表A的主关键字是表B中的字段,则该字段称为表B的外键,表A称为主表,表B称为从表,但要注意,必须要计算机要知道你是这种关系。
核查、Null和缺省属性:核查属性又叫核查约束,Null属性又叫Null约束,缺省属性又叫缺省约束;这些名称是描述一件事,描述一种情况,这件事或这张情况我们当然可以人为的那样特意做(输入数据是注意就行),但是,他们的本意是实现自动化,也就是让计算机做这件事。
(你知道为什么建立主键和唯一约束的时候,会自动的创建索引吗?而且是唯一索引,想一想索引大多在那些字段上用,以及索引的作用就会知道了。像主键约束、唯一约束、非空约束、外键约束、核查约束和缺省约束这些操作都是使表具有某些特性,所以在这里我认为他们都是表的属性。)
2、DML
2-1、DML的概述
DML(Data Manipulation Language 数据操控语言)用于操作数据库对象中包含的数据,也就是说操作的单位是记录。
2-2、DML的主要语句(操作)
Insert语句:向数据表张插入一条记录。
Delete语句:删除数据表中的一条或多条记录,也可以删除数据表中的所有记录,但是,它的操作对象仍是记录。
Update语句:用于修改已存在表中的记录的内容。
2-3、DML的操作对象——记录
2-3-1、注意
当我们对记录进行Insert、Delete和Update操作的时候,一定要注意,一定要清楚DDL对其的一些操作。
3、DCL
3-1、DCL的概述
DCL(Data Control Language 数据控制语句)的操作是数据库对象的权限,这些操作的确定使数据更加的安全。
3-2、DCL的主要语句(操作)
Grant语句:允许对象的创建者给某用户或某组或所有用户(PUBLIC)某些特定的权限。
Revoke语句:可以废除某用户或某组或所有用户访问权限
3-3、DCL的操作对象(用户)
此时的用户指的是数据库用户。