HBase学习笔记(三)———《HBase简单shell操作命令》

HBase简单shell操作命令

1、Hbase Shell 练习

$ /bin/hbase shell 
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 0.94.27, rfb434617716493eac82b55180b0bbd653beb90bf, Thu Mar 19 06:17:55 UTC 2015
hbase(main):001:0>



输入help 然后 按回车键,可以看到一列shell命令。这里的帮助很详细,要注意的是表名,行和列需要加引号。

hbase(main):001:0> help
HBase Shell, version 0.94.27, rfb434617716493eac82b55180b0bbd653beb90bf, Thu Mar 19 06:17:55 UTC 2015
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, 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, truncate_preserve

  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, list_replicated_tables, remove_peer, start_replication, stop_replication

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

  Group name: security
  Commands: grant, revoke, user_permission

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/docs/current/book.html
hbase(main):002:0>

以网上的一个学生成绩表的例子来演示hbase的用法。


这里grad对于表来说是一个只有它自己的列族,course对于表来说是一个有两个列的列族,这个列族由两个列组成math和art,当然我们可以根据我们的需要在course中建立更多的列族,如computer,physics等相应的列添加入course列族。

create命令用法:

create 'table_name', 'column_family_name1', 'column_family_name2', ....

其中,table_name指表名,column_family_name1为第一个列族名,column_family_name2为第二个列族名,可以加多个列族名

create创建一个名为 scores的表,这个表有两个列族(column family),分别为grad和courese,操作如下:

hbase(main):002:0> create 'scores', 'grade', 'course'
0 row(s) in 4.1860 seconds

=> Hbase::Table - scores

list命令用法:

1、list

列出所有的表

2、list 'table_name'

列出表名为table_name的表

list列出所有的表来检查创建情况,操作如下:

hbase(main):003:0> list
TABLE
scores
1 row(s) in 0.0090 seconds

=> ["scores"]

put命令比较简单,只有一种用法:

put 'table_name', 'row_value', 'column_name', 'value', timestamp

其中,table_name指表名,row_value指行键值,column_name指列名(具体到某个列族下的某个列),value指单元格的值,timestamp指时间戳,一般省略

按设计的表结构用put插入值,操作如下:

hbase(main):004:0> put 'scores', 'Tom', 'grade', '5'
0 row(s) in 0.0770 seconds

hbase(main):005:0> put 'scores', 'Tom', 'course:math', '97'
0 row(s) in 0.0040 seconds

hbase(main):006:0> put 'scores', 'Tom', 'course:art', '87'
0 row(s) in 0.0040 seconds

hbase(main):007:0> put 'scores', 'Jim', 'grade', '4'
0 row(s) in 0.0050 seconds

hbase(main):008:0> put 'scores', 'Jim', 'course:math', '89'
0 row(s) in 0.0100 seconds

hbase(main):009:0> put 'scores', 'Jim', 'course:art', '80'
0 row(s) in 0.0090 seconds

scan命令用法:

1、scan 'table_name'

扫描表名为'table_name'的表

scan扫描表名为scores的表,操作如下:

hbase(main):010:0> scan 'scores'
ROW                   COLUMN+CELL
 Jim                  column=course:art, timestamp=1429439736957, value=80
 Jim                  column=course:math, timestamp=1429439727558, value=89
 Jim                  column=grade:, timestamp=1429439693821, value=4
 Tom                  column=course:art, timestamp=1429439680468, value=87
 Tom                  column=course:math, timestamp=1429439671589, value=97
 Tom                  column=grade:, timestamp=1429439628765, value=5
2 row(s) in 0.0190 seconds

2、scan 'table_name',{COLUMNS => 'column_name',LIMIT => 10, STARTROW => 'row_value'}

扫描表名为'table_name'的表,列名为column_name的,行键值以row_value开始的10条数据

scan扫描表名为scores的表,列名为course:math的数据,操作如下:

hbase(main):011:0> scan 'scores', {COLUMNS => 'course:math'}
ROW                     COLUMN+CELL
 Jim                    column=course:math, timestamp=1429439727558, value=89
 Tom                    column=course:math, timestamp=1429439671589, value=97
2 row(s) in 0.0070 seconds

3、scan 'table_name',{COLUMNS => ['column_name1','column_name2'],LIMIT => 10, STARTROW => 'row_value'}

扫描表名为'table_name'的表,列名为column_name1和column_name2的,行键值以row_value开始的10条数据

scan扫描表名为scores的表,操作如下:

hbase(main):008:0> scan 'scores', {COLUMNS => ['course:math','course:art']}
ROW                     COLUMN+CELL
 Jim                    column=course:art, timestamp=1429439736957, value=80
 Jim                    column=course:math, timestamp=1429439727558, value=89
 Tom                    column=course:art, timestamp=1429439680468, value=87
 Tom                    column=course:math, timestamp=1429439671589, value=97
2 row(s) in 0.0080 seconds

4、scan 'table_name',{COLUMNS => ['column_name1','column_name2'],LIMIT => 10, STARTROW => 'row_value', VERSIONS => 'n'}

scan操作默认只扫描最新版本的数据,如果想扫描多版本的数据,操作如下:

hbase(main):009:0> scan 'scores', {COLUMNS => 'course:math', VERSIONS => 2]}
ROW COLUMN+CELL
Jim column=course:math, timestamp=1429439736957, value=80
Jim column=course:math, timestamp=1429439727558, value=89
Tom column=course:math, timestamp=1429439680468, value=87
Tom column=course:math, timestamp=1429439671589, value=97
2 row(s) in 0.0080 seconds


get查询表名为scores,行键值为Jim的数据,操作如下:

hbase(main):011:0> get 'scores', 'Jim'
COLUMN                CELL
 course:art           timestamp=1429439736957, value=80
 course:math          timestamp=1429439727558, value=89
 grade:               timestamp=1429439693821, value=4
3 row(s) in 0.0160 seconds

get命令用法:

1、get 'table_name', 'row_value'

查询表名为table_name,行键值为row_value的数据

get查询表名为scores,行键值为Jim的数据,操作如下:

hbase(main):011:0> get 'scores', 'Jim'
COLUMN                CELL
 course:art           timestamp=1429439736957, value=80
 course:math          timestamp=1429439727558, value=89
 grade:               timestamp=1429439693821, value=4
3 row(s) in 0.0160 seconds

2、get 'table_name', 'row_value',{COLUMN => 'column_name'}

      get 'table_name', 'row_value','column_name'

上述两种方法相同,查询表名为table_name,行键值为row_value,列名为column_name的数据

get查询表名为scores,行键值为Jim,列名为course:art的数据,操作如下:

hbase(main):012:0> get 'scores', 'Jim', {COLUMN => 'course:art'}
COLUMN                CELL
 course:art           timestamp=1429439736957, value=80
1 row(s) in 0.0040 seconds

3、get 'table_name', 'row_value', {COLUMN => ['column_name1', 'column_name2']}

      get 'table_name', 'row_value',  ['column_name1', 'column_name2']

      get 'table_name', 'row_value',  'column_name1', 'column_name2'

上述三种方法相同,查询表名为table_name,行键值为row_value,列名为column_name1和column_name2的数据

get查询表名为scores,行键值为Jim,列名为course:art和course:math的数据,操作如下:

hbase(main):013:0> get 'scores', 'Jim', 'course:art','course:math'
COLUMN                CELL
 course:art           timestamp=1429439736957, value=80
1 row(s) in 0.0040 seconds


delete命令用法:

1、delete 'table_name', 'row_value',{COLUMN => 'column_name'}

      delete 'table_name', 'row_value','column_name'

delete删除表名为scores,行键值为Jim,列名为course:math的单元格,操作如下:

hbase(main):009:0> delete 'scores', 'Jim', 'course:math'
0 row(s) in 0.0340 seconds

检查一下数据是否被删除,操作如下:

hbase(main):010:0> scan 'scores'
ROW                     COLUMN+CELL
 Jim                    column=course:art, timestamp=1429439736957, value=80
 Jim                    column=grade:, timestamp=1429439693821, value=4
 Tom                    column=course:art, timestamp=1429439680468, value=87
 Tom                    column=course:math, timestamp=1429439671589, value=97</span>


检查一下数据是否被删除,操作如下:

hbase(main):010:0> scan 'scores'
ROW                     COLUMN+CELL
 Jim                    column=course:art, timestamp=1429439736957, value=80
 Jim                    column=grade:, timestamp=1429439693821, value=4
 Tom                    column=course:art, timestamp=1429439680468, value=87
 Tom                    column=course:math, timestamp=1429439671589, value=97
deleteall命令用法:

deleteall 'table_name', 'row_value'

删除表名为table_name,行键值为row_value的整行数据

deleteall删除表名为scores,行键值为Tom的整行数据,操作如下:

hbase(main):018:0> deleteall 'scores', 'Tom'
0 row(s) in 0.0040 seconds

检查一下数据是否被删除,操作如下:

hbase(main):019:0> scan 'scores'
ROW                     COLUMN+CELL
 Jim                    column=course:art, timestamp=1429439736957, value=80
 Jim                    column=grade:, timestamp=1429439693821, value=4
1 row(s) in 0.0110 seconds


disable命令用法:

disable 'table_name'

禁用表名为'table_name'的表

disable先禁用表名为scores的表,操作如下:

hbase(main):020:0> disable 'scores'
0 row(s) in 1.2230 seconds

alter命令用法:

1、alter 'table_name',NAME => 'column_family_name'

2、alter  'table_name', NAME => 'column_family_name',METHOD => 'delete'

      删除表名为'table_name', 列族名为'column_family_name'的列族

删除表名为scores,列族名为grade的列族,操作如下:

hbase(main):025:0> alter 'scores', NAME => 'grade', METHOD => 'delete'
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 2.1000 seconds

count命令操作:

count 'table_name'

统计表名为‘table_name'的行数

hbase(main):027:0> count 'scores'
1 row(s) in 0.0200 seconds

=> 1

drop命令操作:

drop 'table_name'

删除表名为table_name的表。PS:要先用disable命令停用表后才可以删除

 drop删除表,操作如下:

hbase(main):035:0> drop 'scores'
0 row(s) in 0.1470 seconds

关闭shell

hbase(main):013:0> exit

5、停止Hbase Shell

运行停止脚本来停止HBase.

$ ./bin/stop-hbase.sh
stopping hbase..............
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章