http://hbase.apache.org/book.html#quickstart
Procedure: Use HBase For the First Time Connect to HBase
进入HBase Shell
[root@node1 ~]# hbase shell
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/hbase-1.2.6/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/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> version
1.2.6, rUnknown, Mon May 29 02:25:32 CDT 2017
hbase(main):002:0>
查看状态
hbase(main):002:0> status
1 active master, 1 backup masters, 3 servers, 0 dead, 0.6667 average load
hbase(main):003:0>
查看当前用户
hbase(main):003:0> whoami
root (auth:SIMPLE)
groups: root
hbase(main):004:0>
查看帮助
hbase(main):004: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(main):005:0>
(1)查看create帮助
hbase(main):005:0> create
ERROR: wrong number of arguments (0 for 1)
Here is some help for this command:
Creates a table. Pass a table name, and a set of column family
specifications (at least one), and, optionally, table configuration.
Column specification can be a simple string (name), or a dictionary
(dictionaries are described below in main help output), necessarily
including NAME attribute.
Examples:
Create a table with namespace=ns1 and table qualifier=t1
hbase> create 'ns1:t1', {NAME => 'f1', VERSIONS => 5}
Create a table with namespace=default and table qualifier=t1
hbase> create 't1', {NAME => 'f1'}, {NAME => 'f2'}, {NAME => 'f3'}
hbase> # The above 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', {NAME => 'f1', CONFIGURATION => {'hbase.hstore.blockingStoreFiles' => '10'}}
Table configuration options can be put at the end.
Examples:
hbase> create 'ns1:t1', 'f1', SPLITS => ['10', '20', '30', '40']
hbase> create 't1', 'f1', SPLITS => ['10', '20', '30', '40']
hbase> create 't1', 'f1', SPLITS_FILE => 'splits.txt', OWNER => 'johndoe'
hbase> create 't1', {NAME => 'f1', VERSIONS => 5}, METADATA => { 'mykey' => 'myvalue' }
hbase> # Optionally pre-split the table into NUMREGIONS, using
hbase> # SPLITALGO ("HexStringSplit", "UniformSplit" or classname)
hbase> create 't1', 'f1', {NUMREGIONS => 15, SPLITALGO => 'HexStringSplit'}
hbase> create 't1', 'f1', {NUMREGIONS => 15, SPLITALGO => 'HexStringSplit', REGION_REPLICATION => 2, CONFIGURATION => {'hbase.hregion.scan.loadColumnFamiliesOnDemand' => 'true'}}
hbase> create 't1', {NAME => 'f1', DFS_REPLICATION => 1}
You can also keep around a reference to the created table:
hbase> t1 = create 't1', 'f1'
Which gives you a reference to the table named 't1', on which you can then
call methods.
hbase(main):006:0>
(2)创建表 语法:create ‘表名’, ‘列簇’
hbase(main):007:0> create 't1' ,'cf1','cf2'
0 row(s) in 4.5550 seconds
=> Hbase::Table - t1
hbase(main):008:0>
(3)列举表
hbase(main):008:0> list
TABLE
t1
1 row(s) in 0.0570 seconds
=> ["t1"]
hbase(main):009:0>
(4)查看表结构
hbase(main):009:0> desc 't1'
Table t1 is ENABLED
t1
COLUMN FAMILIES DESCRIPTION
{NAME => 'cf1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSI
ONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
{NAME => 'cf2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSI
ONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
2 row(s) in 0.3030 seconds
hbase(main):010:0>
(5)表是否存在
hbase(main):010:0> exists 't1'
Table t1 does exist
0 row(s) in 0.5200 seconds
hbase(main):011:0>
(1)添加数据 添加4行
hbase(main):010:0> put 't1','r1','cf1:name','aa'
0 row(s) in 0.1800 seconds
hbase(main):011:0> put 't1','r2','cf2:age','22'
0 row(s) in 0.0620 seconds
hbase(main):012:0> put 't1','r3','cf1:name','bb'
0 row(s) in 0.0230 seconds
hbase(main):013:0> put 't1','r4','cf2:age','23'
0 row(s) in 0.0190 seconds
hbase(main):014:0>
添加1行
hbase(main):014:0> put 't1','rk1','cf1:name','bb'
0 row(s) in 0.0210 seconds
hbase(main):015:0> put 't1','rk1','cf1:age','23'
0 row(s) in 0.0220 seconds
hbase(main):016:0> put 't1','rk1','cf2:cid','1001'
0 row(s) in 0.0220 seconds
hbase(main):017:0> put 't1','rk1','cf2:dept','bigdata'
0 row(s) in 0.0210 seconds
hbase(main):018:0>
(2)查询
hbase(main):018:0> get 't1','rk1'
COLUMN CELL
cf1:age timestamp=1501251921190, value=23
cf1:name timestamp=1501251900543, value=bb
cf2:cid timestamp=1501251958895, value=1001
cf2:dept timestamp=1501252040732, value=bigdata
4 row(s) in 0.1210 seconds
hbase(main):019:0> get 't1','rk1','cf1:name'
COLUMN CELL
cf1:name timestamp=1501251900543, value=bb
1 row(s) in 0.0440 seconds
hbase(main):020:0> get 't1','rk1','cf2:dept'
COLUMN CELL
cf2:dept timestamp=1501252040732, value=bigdata
1 row(s) in 0.0200 seconds
hbase(main):021:0>
(3)扫描表
hbase(main):021:0> scan 't1'
ROW COLUMN+CELL
r1 column=cf1:name, timestamp=1501251794870, value=aa
r2 column=cf2:age, timestamp=1501251806541, value=22
r3 column=cf1:name, timestamp=1501251815736, value=bb
r4 column=cf2:age, timestamp=1501251824433, value=23
rk1 column=cf1:age, timestamp=1501251921190, value=23
rk1 column=cf1:name, timestamp=1501251900543, value=bb
rk1 column=cf2:cid, timestamp=1501251958895, value=1001
rk1 column=cf2:dept, timestamp=1501252040732, value=bigdata
5 row(s) in 0.0900 seconds
hbase(main):022:0>
(4)删除数据
删除整行数据
hbase(main):014:0> deleteall 't1','r1'
0 row(s) in 0.1620 seconds
hbase(main):015:0> scan 't1'
ROW COLUMN+CELL
r2 column=cf2:age, timestamp=1501251806541, value=22
r3 column=cf1:name, timestamp=1501251815736, value=bb
r4 column=cf2:age, timestamp=1501251824433, value=23
rk1 column=cf1:age, timestamp=1501251921190, value=23
rk1 column=cf1:name, timestamp=1501251900543, value=bb
rk1 column=cf2:cid, timestamp=1501251958895, value=1001
rk1 column=cf2:dept, timestamp=1501252040732, value=bigdata
4 row(s) in 0.2190 seconds
hbase(main):016:0>
删除指定行的列
hbase(main):016:0> delete 't1','rk1','cf2:dept'
0 row(s) in 0.0260 seconds
hbase(main):017:0> get 't1','rk1'
COLUMN CELL
cf1:age timestamp=1501251921190, value=23
cf1:name timestamp=1501251900543, value=bb
cf2:cid timestamp=1501251958895, value=1001
3 row(s) in 0.0840 seconds
hbase(main):018:0>
(5)删除表
hbase(main):018:0> create 't2','info'
0 row(s) in 2.4840 seconds
=> Hbase::Table - t2
hbase(main):019:0> list
TABLE
t1
t2
2 row(s) in 0.0250 seconds
=> ["t1", "t2"]
hbase(main):020:0>
分两步,首先disable,然后drop
hbase(main):020:0> disable 't2'
0 row(s) in 2.2960 seconds
hbase(main):021:0> drop 't2'
0 row(s) in 1.3080 seconds
hbase(main):022:0> list
TABLE
t1
1 row(s) in 0.0130 seconds
=> ["t1"]
hbase(main):023:0>
(6)截断表
hbase(main):023:0> count 't1'
4 row(s) in 0.0670 seconds
=> 4
hbase(main):024:0> truncate 't1'
Truncating 't1' table (it may take a while):
- Disabling table...
- Truncating table...
0 row(s) in 4.2030 seconds
hbase(main):025:0> count 't1'
0 row(s) in 0.1620 seconds
=> 0
hbase(main):026:0>
(7)修改表结构
先disable后enable
hbase(main):026:0> disable 't1'
0 row(s) in 2.3110 seconds
hbase(main):027:0> alter 't1',READONLY
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 1.9830 seconds
hbase(main):028:0> enable 't1'
0 row(s) in 1.3060 seconds
hbase(main):029:0>
关系数据库系统中,命名空间namespace是表的逻辑分组,同一组中的表有类似的用途。
hbase的表也有命名空间的管理方式,命名空间的概念为即将到来的多租户特性打下基础:
(1)缺省命名空间 HBase系统默认定义了两个缺省的namespace
可以通过list_namespace命令查看命名空间
hbase(main):029:0> list_namespace
NAMESPACE
default
hbase
2 row(s) in 0.2170 seconds
hbase(main):030:0>
(2)创建namespace
hbase(main):030:0> create_namespace 'mydb'
0 row(s) in 0.1070 seconds
hbase(main):031:0> list_namespace
NAMESPACE
default
hbase
mydb
3 row(s) in 0.0400 seconds
hbase(main):032:0>
(3)查看namespace结构
hbase(main):032:0> describe_namespace 'mydb'
DESCRIPTION
{NAME => 'mydb'}
1 row(s) in 0.0330 seconds
hbase(main):033:0>
(4)在指定namespace下创建表
hbase(main):033:0> create 'mydb:test',{NAME => 'f1', VERSIONS => 5}
0 row(s) in 1.2490 seconds
=> Hbase::Table - mydb:test
hbase(main):034:0>
hbase(main):034:0> list
TABLE
mydb:test
t1
2 row(s) in 0.0390 seconds
=> ["mydb:test", "t1"]
hbase(main):035:0>
(5)添加数据
hbase(main):035:0> put 'mydb:test','rk1','f1:name','abc'
0 row(s) in 0.1310 seconds
hbase(main):036:0> put 'mydb:test','rk1','f1:age','2'
0 row(s) in 0.0220 seconds
hbase(main):037:0> scan 'mydb:test'
ROW COLUMN+CELL
rk1 column=f1:age, timestamp=1501335244278, value=2
rk1 column=f1:name, timestamp=1501335234503, value=abc
1 row(s) in 0.0970 seconds
hbase(main):038:0>