执行命令root@master:/opt/module/cdh/hbase-1.3.1/bin# ./hbase shell
进入客户端。
执行help
得到结果:
hbase(main):001:0> help
HBase Shell, version 1.3.1, r930b9a55528fe45d8edce7af42fef2d35e77677a, Thu Apr 6 19:36:54 PDT 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, splitormerge_enabled, splitormerge_switch, 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, get_peer_config, list_peer_configs, 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, delete_table_snapshots, list_snapshots, list_table_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):002:0>
最重要的是:
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: dml
Commands: append, count, delete, deleteall, get, get_counter, get_splits, incr, put, scan, truncate, truncate_preserve
执行命令:create 'stu','info1','info2'
,创建了一个stu
表,有两个列簇。
执行命令:describe 'stu'
这里面的VERSIONS
是1 表示数据版本号只能存放一个版本。只能返回一个数据。这个是可以修改的,可以定义存放的版本数。比如是2,则留下数据的两个版本号。
修改的最小单元是列簇。执行命令:alter 'stu',{NAME=>'info1',VERSIONS=>3}
改表结构还是ddl操作,修改的是元数据。
查看表结构:
要想删除表,需要先disable
该表,然后才能drop
表。
执行命令:disable 'student'
删除表:drop 'student'
这样就能删除成功了。
并且创建表必须要有至少一个列族,否则会创建失败。
插入命令:put '表','rowkey','列名','value值'
示例:put 'stu','1001','info1:name','zhanglaing'
scan
的示例有很多:
scan
既可以整表扫描,也可以使用过滤条件等等
scan
查询示例:
scan
可以直接查询表,但是get
不可以,get
需要指定表名
和rowkey
get
示例:
操作示例:
对stu
表插入一些数据,并做一些查询操作:
hbase(main):007:0> put 'stu','1001','info1:sex','male'
0 row(s) in 0.1020 seconds
hbase(main):008:0> put 'stu','1001','info2:addr','shanghai'
0 row(s) in 0.0170 seconds
hbase(main):009:0> put 'stu','1002','info1:phone','1231231'
0 row(s) in 0.0360 seconds
hbase(main):010:0> put 'stu','1002','info2:addr','beijing'
0 row(s) in 0.0070 seconds
hbase(main):011:0> scan 'stu'
ROW COLUMN+CELL
1001 column=info1:name, timestamp=1577015138401, value=zhanglaing
1001 column=info1:sex, timestamp=1577016467811, value=male
1001 column=info2:addr, timestamp=1577016497189, value=shanghai
1002 column=info1:phone, timestamp=1577016555983, value=1231231
1002 column=info2:addr, timestamp=1577016586094, value=beijing
2 row(s) in 0.0260 seconds
hbase(main):012:0> get 'stu','1001','info1'
COLUMN CELL
info1:name timestamp=1577015138401, value=zhanglaing
info1:sex timestamp=1577016467811, value=male
1 row(s) in 0.0260 seconds
hbase(main):013:0> get 'stu','1001'
COLUMN CELL
info1:name timestamp=1577015138401, value=zhanglaing
info1:sex timestamp=1577016467811, value=male
info2:addr timestamp=1577016497189, value=shanghai
1 row(s) in 0.0090 seconds
hbase(main):014:0> get 'stu','1001','info1:name'
COLUMN CELL
info1:name timestamp=1577015138401, value=zhanglaing
1 row(s) in 0.0670 seconds
hbase(main):015:0>
呢么如果我们使用scan
来做这些操作:
hbase(main):001:0> scan 'stu'
ROW COLUMN+CELL
1001 column=info1:name, timestamp=1577017030315, value=lisi
1001 column=info1:sex, timestamp=1577016467811, value=male
1001 column=info2:addr, timestamp=1577016497189, value=shanghai
1002 column=info1:phone, timestamp=1577016555983, value=1231231
1002 column=info2:addr, timestamp=1577016586094, value=beijing
2 row(s) in 0.4540 seconds
hbase(main):002:0> scan 'stu',{COLUMNS => ['info1:name', 'info1:sex']}
ROW COLUMN+CELL
1001 column=info1:name, timestamp=1577017030315, value=lisi
1001 column=info1:sex, timestamp=1577016467811, value=male
1 row(s) in 0.0650 seconds
hbase(main):003:0> scan 'stu',{COLUMNS=>'info1:name'}
ROW COLUMN+CELL
1001 column=info1:name, timestamp=1577017030315, value=lisi
1 row(s) in 0.0130 seconds
hbase(main):004:0> scan 'stu',{STARTROW=>'1001'}
ROW COLUMN+CELL
1001 column=info1:name, timestamp=1577017030315, value=lisi
1001 column=info1:sex, timestamp=1577016467811, value=male
1001 column=info2:addr, timestamp=1577016497189, value=shanghai
1002 column=info1:phone, timestamp=1577016555983, value=1231231
1002 column=info2:addr, timestamp=1577016586094, value=beijing
2 row(s) in 0.0450 seconds
hbase(main):005:0> scan 'stu',{STARTROW=>'1001',STOPROW=>'1002'}
ROW COLUMN+CELL
1001 column=info1:name, timestamp=1577017030315, value=lisi
1001 column=info1:sex, timestamp=1577016467811, value=male
1001 column=info2:addr, timestamp=1577016497189, value=shanghai
1 row(s) in 0.0310 seconds
hbase(main):006:0>
这里注意一下,rowkey
是按照字典排序
的,当我们插入10010
时,你会发现他会在1001的后面
HBase是没有UPDATE
这种命令,但是可以通过put
通过添加新数据去覆盖旧数据。
修改示例:
呢么以前的旧数据在哪里?其实还在Region Servers
的内存中,可以通过scan
显示出来:
scan 'stu',{RAW=>true,VERSIONS=>10}
表示显示全量数据,并且显示版本号在10个以内的数据。
显示多个版本也可以使用get
命令:
get 'stu','1001',{COLUMN=>'info1:name',VERSIONS=>3}
在我们查询数据的时候,只显示时间戳最大的数据。
还有一点,这里的版本VERSIONS
是指HBase会存储的最大的版本数,但是如果设置的默认存储版本为1,呢么这里的VERSIONS
设置多少都只能显示一个。所以这里的数字要小于等于设置的版本数,即:alter 'stu',{NAME=>'info',VERSIONS=>3}
这里的数字3。(我写的是3,可以使其他数字)。
delete
删除示例:(三个参数)
删除操作其实也是在插入新数据,只不过插入的不是value而是type类型。
操作示例:
扫描一下整个表:
我们删除1001,info1:name
,
可以看到我们已经没有这条数据了,但是在全量显示中是怎么显示的呢?
1001 column=info1:name, timestamp=1577021426252, type=DeleteColumn
没错,在后面插入了一个type=DeleteColumn
的 数据,当我们扫描整张表的时候获取最大的时间戳,得到了这一条数据,但是是个删除标记,所以不会显示该列数据。
验证:(添加一条数据,但是时间戳在删除的时间戳小几秒)
执行delete 'stu','1001','info1'
,在命令行是没有效果的,但是在API中是可以删除的。
如果想删除一整行数据,可以使用deleteall
操作示例deleteall 'stu','1002'
:
如果需要清楚整个表,呢么就使用truncate
操作实例truncate 'stu'
:
先禁掉表,再删除表。
无论是增删改查使用的最重要的就是时间戳
!!!