1、数据库操作
1、进入 hbase 客户端、帮助命令[hui@hadoop201 conf]$ hbase shell
help
hbase:008:0> list_namespace
NAMESPACE
default
hbase
hbase 存放的是 一些元数据,一般不要做什么修改,default 是默认库
3、创建 namespace
hbase:009:0> create_namespace 'mydb' Took 0.2452 seconds hbase:014:0> list_namespace NAMESPACE default hbase mydb
创建数据库也可以设置一些属性
create_namespace 'mydb1',{'createTime'=>'2022-05-08','createBy'=>'wdh01'} hbase:001:0> describe_namespace 'mydb' DESCRIPTION {NAME => 'mydb'} Quota is disabled Took 0.8975 seconds hbase:002:0> describe_namespace 'mydb1' DESCRIPTION {NAME => 'mydb1', createBy => 'wdh01', createTime => '2022-05-08'} Quota is disabled
4、namespace 属性修改,新增
METHOD=>'set‘ 下面就修改mydb1 的时间+1天,并增加一个desc 属性为例说明
hbase:005:0> alter_namespace 'mydb1',{METHOD=>'set','createTime'=>'2022-05-09','desc'=>'hbase test'} Took 0.2257 seconds hbase:006:0> describe_namespace 'mydb1' DESCRIPTION {NAME => 'mydb1', createBy => 'wdh01', createTime => '2022-05-09', desc => 'hbase test'}
删除一个属性:METHOD=>'unset‘
alter_namespace 'mydb1',{'METHOD=>'unset',NAME=>'createBy'} hbase:003:0> describe_namespace 'mydb1' DESCRIPTION {NAME => 'mydb1', createBy => 'wdh01', createTime => '2022-05-09', desc => 'hbase test'}
5、删除 namespace
hbase:004:0> drop_namespace 'mydb1' hbase:005:0> list_namespace NAMESPACE default hbase mydb
如果库里有表,则无法直接删除库,需要先删除表,在进行drop_namespace
2、表的基础操作
1、查看 mydb 下的表
hbase:006:0> list_namespace_tables 'mydb'
2、查看所有自己创建的表
hbase:001:0> list
3、在mydb下创建表
hbase:002:0> create 'mydb:test1',{NAME=>'f1'}
创建表时若不指定库在直接在default下创建表,创建表至少指定一个列族,这里创建了一个名为f1的列族;如果创建表,不指定属性信息,可以进化这样进行创建一个表
hbase:001:0> create 'mydb:test2','f1' hbase:002:0> create 'mydb:test3','f1','f2'
4、查看表详细信息
hbase:001:0> describe 'mydb:test1' Table mydb:test1 is ENABLED mydb:test1 COLUMN FAMILIES DESCRIPTION {NAME => 'f1', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', COMPRESSION => 'NONE', TTL => ' FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'} hbase:003:0> describe 'mydb:test3' Table mydb:test3 is ENABLED mydb:test3 COLUMN FAMILIES DESCRIPTION {NAME => 'f1', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', COMPRESSION => 'NONE', TTL => ' FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'} {NAME => 'f2', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', COMPRESSION => 'NONE', TTL => ' FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
5、表信息修改:此处修改一个列族的版本号
hbase:005:0> alter 'mydb:test1',NAME=>'f1',VERSIONS=>'2' Updating all regions with the new schema... 1/1 regions updated. Done. Took 2.0358 seconds hbase:006:0> describe 'mydb:test1' Table mydb:test1 is ENABLED mydb:test1 COLUMN FAMILIES DESCRIPTION {NAME => 'f1', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', VERSIONS => '2', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', COMPRESSION => 'NONE', TTL => ' FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
修改多个值
hbase:007:0> alter 'mydb:test3',{NAME=>'f1',VERSIONS=>'2'},{NAME=>'f2',VERSIONS=>'3'} Updating all regions with the new schema... 1/1 regions updated. Done. Took 1.8225 seconds hbase:008:0> describe 'mydb:test3' Table mydb:test3 is ENABLED mydb:test3 COLUMN FAMILIES DESCRIPTION {NAME => 'f1', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', VERSIONS => '2', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', COMPRESSION => 'NONE', TTL => ' FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'} {NAME => 'f2', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', VERSIONS => '3', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', COMPRESSION => 'NONE', TTL => ' FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'} 2 row(s)
6、增加/删除列族
mydb:test2 增加列族
hbase:009:0> alter 'mydb:test2','f2' Updating all regions with the new schema... 1/1 regions updated. hbase:001:0> describe 'mydb:test2' Table mydb:test2 is ENABLED mydb:test2 COLUMN FAMILIES DESCRIPTION {NAME => 'f1', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', COMPRESSION => 'NONE', TTL => ' FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'} {NAME => 'f2', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', COMPRESSION => 'NONE', TTL => ' FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
mydb:test3 删除列族
hbase:002:0> alter 'mydb:test3','delete'=>'f2' Updating all regions with the new schema... 1/1 regions updated. Done. Took 1.8970 seconds hbase:003:0> hbase:004:0> describe 'mydb:test3' Table mydb:test3 is ENABLED mydb:test3 COLUMN FAMILIES DESCRIPTION {NAME => 'f1', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', VERSIONS => '2', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', COMPRESSION => 'NONE', TTL => ' FOREVER
7、删除表
删除表需要先进行disabkle,在进行drop
hbase:005:0> disable 'mydb:test3' hbase:001:0> drop 'mydb:test3'
3、DML 操作
1、创建一个表:在 default 下创建,列族名称是 info
hbase:007:0> create 'stu','info'
2、插入数据
put 'stu','1001','info:name','linghc'
put 表示要插入/更新数据,stu 是表名称,1001 是 rowkey,插入数据需要指定列族和列:这里的 info是列族,name 是info 下的具体列,’linghc‘ 是这个列rowkey=1001 的值,下面在info这个列族增加两个列sex 和 age
put 'stu','1001','info:sex','man' put 'stu','1001','info:age','26'
查询
hbase:007:0> get 'stu','1001' COLUMN CELL info:age timestamp=2022-05-08T22:17:55.859, value=26 info:name timestamp=2022-05-08T22:14:20.306, value=linghc info:sex timestamp=2022-05-08T22:17:55.145, value=man
增加两行数据
put 'stu','1002','info:name','renyy' put 'stu','1002','info:sex','female' put 'stu','1002','info:age','24' put 'stu','1003','info:name','yilin' put 'stu','1003','info:sex','female' put 'stu','1003','info:age','18'
可以给相同的列族的某一个行数据增加或减少n列值是可以的,列的个数可以不一样
put 'stu','1004','info:name','dongfangb' put 'stu','1004','info:sex','female' put 'stu','1004','info:age','28' put 'stu','1004','info:party','rysj'
3、查询
hbase:020:0> get 'stu','1004' COLUMN CELL info:age timestamp=2022-05-08T22:22:08.548, value=28 info:name timestamp=2022-05-08T22:22:08.359, value=dongfangb info:party timestamp=2022-05-08T22:22:08.853, value=rysj info:sex timestamp=2022-05-08T22:22:08.477, value=female
支持按列查询
hbase:021:0> get 'stu','1004','info:name','info:age' COLUMN CELL info:age timestamp=2022-05-08T22:22:08.548, value=28 info:name timestamp=2022-05-08T22:22:08.359, value=dongfangb
4、扫描表数据
hbase:022:0> scan 'stu' ROW COLUMN+CELL 1001 column=info:age, timestamp=2022-05-08T22:17:55.859, value=26 1001 column=info:name, timestamp=2022-05-08T22:14:20.306, value=linghc 1001 column=info:sex, timestamp=2022-05-08T22:17:55.145, value=man 1002 column=info:age, timestamp=2022-05-08T22:20:06.004, value=24 1002 column=info:name, timestamp=2022-05-08T22:20:05.888, value=renyy 1002 column=info:sex, timestamp=2022-05-08T22:20:05.948, value=female 1003 column=info:age, timestamp=2022-05-08T22:20:06.500, value=18 1003 column=info:name, timestamp=2022-05-08T22:20:06.077, value=yilin 1003 column=info:sex, timestamp=2022-05-08T22:20:06.138, value=female 1004 column=info:age, timestamp=2022-05-08T22:22:08.548, value=28 1004 column=info:name, timestamp=2022-05-08T22:22:08.359, value=dongfangb 1004 column=info:party, timestamp=2022-05-08T22:22:08.853, value=rysj 1004 column=info:sex, timestamp=2022-05-08T22:22:08.477, value=female
按rowkey范围扫描 {STARTROW => '1001' ,STOPROW => '1003'} :左闭右开,此时只查询到 1002
hbase:023:0> scan 'stu',{STARTROW => '1001' ,STOPROW => '1003'} ROW COLUMN+CELL 1001 column=info:age, timestamp=2022-05-08T22:17:55.859, value=26 1001 column=info:name, timestamp=2022-05-08T22:14:20.306, value=linghc 1001 column=info:sex, timestamp=2022-05-08T22:17:55.145, value=man 1002 column=info:age, timestamp=2022-05-08T22:20:06.004, value=24 1002 column=info:name, timestamp=2022-05-08T22:20:05.888, value=renyy 1002 column=info:sex, timestamp=2022-05-08T22:20:05.948, value=female
指定STOPROW 后加!,可以实现左闭右闭
hbase:024:0> scan 'stu',{STARTROW => '1001' ,STOPROW => '1003!'} ROW COLUMN+CELL 1001 column=info:age, timestamp=2022-05-08T22:17:55.859, value=26 1001 column=info:name, timestamp=2022-05-08T22:14:20.306, value=linghc 1001 column=info:sex, timestamp=2022-05-08T22:17:55.145, value=man 1002 column=info:age, timestamp=2022-05-08T22:20:06.004, value=24 1002 column=info:name, timestamp=2022-05-08T22:20:05.888, value=renyy 1002 column=info:sex, timestamp=2022-05-08T22:20:05.948, value=female 1003 column=info:age, timestamp=2022-05-08T22:20:06.500, value=18 1003 column=info:name, timestamp=2022-05-08T22:20:06.077, value=yilin 1003 column=info:sex, timestamp=2022-05-08T22:20:06.138, value=female
5、修改数据直接put 进行覆盖就行
hbase:025:0> put 'stu','1001','info:name','linghc1' Took 0.0152 seconds hbase:026:0> get 'stu','1001' COLUMN CELL info:age timestamp=2022-05-08T22:17:55.859, value=26 info:name timestamp=2022-05-08T22:59:22.549, value=linghc1 info:sex timestamp=2022-05-08T22:17:55.145, value=man 1 row(s) Took 0.0185 seconds
修改数据后,其实只是在原来的基础上增加了一条数据,查询的时候返回了时间戳最新的一个版本,旧版本的数据还在;
hbase:001:0> scan 'stu',{RAW=>true,VERSIONS=>5} ROW COLUMN+CELL 1001 column=info:age, timestamp=2022-05-08T22:17:55.859, value=26 1001 column=info:name, timestamp=2022-05-08T22:59:22.549, value=linghc1 1001 column=info:name, timestamp=2022-05-08T22:14:20.306, value=linghc 1001 column=info:name, timestamp=2022-05-08T22:13:01.243, value=\xE4\xBB\xA4\xE7\x8B\x90\xE5\x86\xB2 1001 column=info:sex, timestamp=2022-05-08T22:17:55.145, value=man 1002 column=info:age, timestamp=2022-05-08T22:20:06.004, value=24 1002 column=info:name, timestamp=2022-05-08T22:20:05.888, value=renyy 1002 column=info:sex, timestamp=2022-05-08T22:20:05.948, value=female 1003 column=info:age, timestamp=2022-05-08T22:20:06.500, value=18 1003 column=info:name, timestamp=2022-05-08T22:20:06.077, value=yilin 1003 column=info:sex, timestamp=2022-05-08T22:20:06.138, value=female 1004 column=info:age, timestamp=2022-05-08T22:22:08.548, value=28 1004 column=info:name, timestamp=2022-05-08T22:22:08.359, value=dongfangb 1004 column=info:party, timestamp=2022-05-08T22:22:08.853, value=rysj 1004 column=info:sex, timestamp=2022-05-08T22:22:08.477, value=female
这里指定显示了包含最新版本在内的5条历史数据,可以通过时间戳查看
hbase:002:0> desc 'stu' Table stu is ENABLED stu COLUMN FAMILIES DESCRIPTION {NAME => 'info', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', COMPRESSION => 'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
VERSIONS => '1' 默认值1.,表示最终只保留最新的一个版本的数据,旧版本的数据在适当的时候会被释放。如果想要保留最新两个版本的数据,可以将某个表的 VERSIONS 设置为 2
alter 'stu',{NAME=>'info',VERSIONS=>2}
6、删除数据
hbase:006:0> delete 'stu','1001','info:name'
其实是删除了当前值得最新版本的数据
hbase:009:0> get 'stu','1001','info:name' COLUMN CELL info:name timestamp=2022-05-08T22:14:20.306, value=linghc
标注:delete
.hbase:010:0> scan 'stu',{RAW=>true,VERSIONS=>5} ROW COLUMN+CELL 1001 column=info:age, timestamp=2022-05-08T22:17:55.859, value=26 1001 column=info:name, timestamp=2022-05-08T22:59:22.549, type=Delete 1001 column=info:name, timestamp=2022-05-08T22:59:22.549, value=linghc1 1001 column=info:name, timestamp=2022-05-08T22:14:20.306, value=linghc 1001 column=info:name, timestamp=2022-05-08T22:13:01.243, value=\xE4\xBB\xA4\xE7\x8B\x90\xE5\x86\xB2 1001 column=info:sex, timestamp=2022-05-08T22:17:55.145, value=man
彻底删除数据
hbase:011:0> deleteall 'stu','1001','info:name' Took 0.0167 seconds hbase:012:0> get 'stu','1001' COLUMN CELL info:age timestamp=2022-05-08T22:17:55.859, value=26 info:sex timestamp=2022-05-08T22:17:55.145, value=man
dleteall直接将 列族的某一个列直接删了,指定rowkey进行删除整行数据
hbase:019:0> deleteall 'stu','1004'
直接删除表;数据直接彻底删除表数据,表还在,数据没了
hbase:001:0> truncate 'stu'