简单架构
部分架构
HBase开启
1、启动hadoop
验证
2、启动zookeeper
需要在在三台中分别启动
zkServer.sh start
zkServer.sh status
3、启动hbase集群 , 需要在master上执行
4、验证hbase
通过 hbase shell 进入到hbase的命令行
NoSQL:
理解: NOT ONLY SQL non-relational(非关系型数据库)
HBASE SHELL 学习:
1、进入HBASE
命令: hbase shell
2、help 查看Hbase shell 支持命令:
Group name: general
Commands: processlist, 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, list_regions, 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
3、general
查看版本:
version
查看HBASE当前状态:
status
4、ddl
5、namespace
1.create_namespace 创建命名空间
hbase> create_namespace 'ns1'
hbase> create_namespace 'ns1', {'PROPERTY_NAME'=>'PROPERTY_VALUE'}
create_namespace 'mydb1'
create_namespace 'mydb2',{'create_time' => '2022-04-25'}
2.list_namespace 查看当前命名空间
样例:list_namespace
结果:
default -- 用户默认使用的命名空间
hbase -- hbase中存放的是HBase内置的表
3.describe_namespace 查看命名空间的详细信息
hbase> describe_namespace 'ns1'
describe_namespace 'mydb1'
4.alter_namespace 修改命名空间
To add/modify a property:
hbase> alter_namespace 'ns1', {METHOD => 'set', 'PROPERTY_NAME' => 'PROPERTY_VALUE'}
To delete a property:
hbase> alter_namespace 'ns1', {METHOD => 'unset', NAME=>'PROPERTY_NAME'}
样例:
增加author配置并且修改create_time配置
alter_namespace 'mydb2',{METHOD => 'set', 'author'=> 'act', 'create_time' => '2022-4-26'}
alter_namespace 'mydb2',{'METHOD' => 'set', 'author'=> 'act', 'create_time' => '2022-4-27'}
删除create_time配置
alter_namespace 'mydb2',{METHOD => 'unset', NAME=>'create_time'}
5.drop_namespace 删除空的命名空间
drop_namespace 'mydb2'
6.list_namespace_tables 查看指定命名空间下的表
list_namespace_tables 'default'
6、DDL-表操作
1.create 创建表
t1:表示表名称
f1:表示列族bersion
ns1:表示命名空间
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'
样例:
默认命名空间下创建mytb1表,列族为info
create 'mytb1', 'info'
在db1命名空间下创建表
create_namespace 'db1'
create 'db1:mytb1', 'info1'
创建表时指定多个列族
create 'db1:mytb2', 'info1', 'info2', 'info3'
创建表并指定列族版本
create 'db1:mytb4',{NAME => 'info1',VERSIONS => 3}
2.describe 查看表的具体信息
样例:
describe 'db1:mytb2'
表中的列族信息中可以看出,每个列族都维护自己的一个版本信息,一个列族对应一个store
describe 'db1:mytb4'
desc 'db1:mytb4'
3.alter 修改表
help 'alter'
样例:
1.修改列族版本号
alter 't1', NAME => 'f1', VERSIONS => 5
alter 'db1:mytb4', NAME => 'info1', VERSIONS => 4
2.删除列族
alter 'ns1:t1', 'delete' => 'f1'
alter 'db1:mytb2','delete' => 'info3'
alter 'ns1:t1', NAME => 'f1', METHOD => 'delete'
alter 'db1:mytb2', NAME => 'info2', METHOD => 'delete'
4.删除表
help 'drop'
注意: 使用drop操作时先需要执行disable
样例:
1.disable 'db1:mytb2'
2.drop 'db1:mytb2'
5.关闭及启用表
1.关闭表操作:
disable 'db1:mytb1'
desc 'db1:mytb1'
2.启用表操作:
enable 'db1:mytb1'
desc 'db1:mytb1'
7、DML
r1 表示 rowKey
c1 表示 列名称
1.put 上传及修改数据操作
help 'put'
hbase> put 'ns1:t1', 'r1', 'c1', 'value'
hbase> put 't1', 'r1', 'c1', 'value'
hbase> put 't1', 'r1', 'c1', 'value', ts1
hbase> put 't1', 'r1', 'c1', 'value', {ATTRIBUTES=>{'mykey'=>'myvalue'}}
hbase> put 't1', 'r1', 'c1', 'value', ts1, {ATTRIBUTES=>{'mykey'=>'myvalue'}}
hbase> put 't1', 'r1', 'c1', 'value', ts1, {VISIBILITY=>'PRIVATE|SECRET'}
样例:
create 'db1:stu', 'info'
put 'db1:stu', '1001', 'info:name', 'zhangsan'
注:修改数据可以直接用put上传最新数据进行替换
2.get 获取一行数据操作
hbase> t.get 'r1'
hbase> t.get 'r1', {TIMERANGE => [ts1, ts2]}
hbase> t.get 'r1', {COLUMN => 'c1'}
hbase> t.get 'r1', {COLUMN => ['c1', 'c2', 'c3']}
hbase> t.get 'r1', {COLUMN => 'c1', TIMESTAMP => ts1}
hbase> t.get 'r1', {COLUMN => 'c1', TIMERANGE => [ts1, ts2], VERSIONS => 4}
hbase> t.get 'r1', {COLUMN => 'c1', TIMESTAMP => ts1, VERSIONS => 4}
hbase> t.get 'r1', {FILTER => "ValueFilter(=, 'binary:abc')"}
hbase> t.get 'r1', 'c1'
hbase> t.get 'r1', 'c1', 'c2'
hbase> t.get 'r1', ['c1', 'c2']
hbase> t.get 'r1', {CONSISTENCY => 'TIMELINE'}
hbase> t.get 'r1', {CONSISTENCY => 'TIMELINE', REGION_REPLICA_ID => 1}
get 'db1:stu', '1001'
put 'db1:stu', '1001', 'info:age', '12'
get 'db1:stu', '1001'
put 'db1:stu', '1001', 'info:age', '13'
获取指定ROWKEY中某一列数据
get 'db1:stu', '1001', 'info:age'
获取指定RowKey下的多列数据
get 'db1:stu', '1001', 'info:age','info:name'
3.scan 获取表中所有数据
put 'db1:stu', '1001', 'info:name', 'zhangsan'
put 'db1:stu', '1001', 'info:age', '13'
put 'db1:stu', '1001', 'info:clazz', 'clazz1'
put 'db1:stu', '1002', 'info:name', 'lisi'
put 'db1:stu', '1002', 'info:age', '13'
put 'db1:stu', '1002', 'info:clazz', 'clazz2'
put 'db1:stu', '1003', 'info:name', 'wangwu'
put 'db1:stu', '1003', 'info:age', '13'
put 'db1:stu', '1003', 'info:clazz', 'clazz3'
-- scan 'db1:stu'
put 'db1:stu', '1011', 'info:name', 'lisi'
put 'db1:stu', '1011', 'info:age', '13'
put 'db1:stu', '1011', 'info:clazz', 'clazz2'
put 'db1:stu', '1004', 'info:name', 'zhaoliu'
put 'db1:stu', '1004', 'info:age', '13'
put 'db1:stu', '1004', 'info:clazz', 'clazz2'
put 'db1:stu', '1020', 'info:name', 'zhaoliu'
put 'db1:stu', '1020', 'info:age', '13'
put 'db1:stu', '1020', 'info:clazz', 'clazz2'
put 'db1:stu', '10010', 'info:name', 'zhaoliu'
put 'db1:stu', '10010', 'info:age', '13'
put 'db1:stu', '10010', 'info:clazz', 'clazz2'
put 'db1:stu', '1001$', 'info:name', 'zhaoliu'
put 'db1:stu', '1001$', 'info:age', '13'
put 'db1:stu', '1001$', 'info:clazz', 'clazz2'
ROWKEY是根据字典排序,并且排序的依据是按位比较ASCII码表
scan 'ns1:t1', {COLUMNS => ['c1', 'c2'], LIMIT => 10, STARTROW => 'xyz'}
scan 'db1:stu', {limit => 3}
按ROWKEY范围进行取值 左闭右开 取值包含STARTROW 不包含 STOPROW
scan 'db1:stu', {STARTROW => '1001',STOPROW => '1004'}
根据ASCII码表取
scan 'db1:stu', {STARTROW => '1001!',STOPROW => '1001~'}
查看表中所有数据并展示所有版本信息
scan 'db1:stu' ,{RAW=>true,VERSIONS=>10}
4.list 查看所有表
5.count 统计表中数据量
count 'db1:stu'
6.删除列
delete 'db1:stu','1001','info:age','1650941469177'
7.删除一个rowkey
deleteall 'db1:stu','1001'
8.清空表
truncate 'db1:stu'