博主
258
258
258
258
专辑

第四节 HBase Shell 的操作

亮子 2023-05-25 02:13:56 6322 0 0 0

1、基本操作

1)进入 HBase 客户端命令行

[hbase]$ bin/hbase shell

2)查看帮助命令

能够展示 HBase 中所有能使用的命令,主要使用的命令有 namespace 命令空间相关,DDL 创建修改表格,DML 写入读取数据。

hbase:001:0> help

2、namespace

1)创建命名空间

使用特定的 help 语法能够查看命令如何使用。

hbase:002:0> help 'create_namespace'

2)创建命名空间 bigdata

hbase:003:0> create_namespace 'bigdata'

3)查看所有的命名空间

hbase:004:0> list_namespace

3、DDL

1)创建表

在 bigdata 命名空间中创建表格 student,两个列族。info 列族数据维护的版本数为 5 个,如果不写默认版本数为 1。

hbase:005:0> create 'bigdata:student', {NAME => 'info', VERSIONS => 5}, {NAME => 'msg'}

如果创建表格只有一个列族,没有列族属性,可以简写。
如果不写命名空间,使用默认的命名空间 default。

hbase:009:0> create 'student1','info'

2)查看表

查看表有两个命令:list 和 describe
list:查看所有的表名

hbase:013:0> list

describe:查看一个表的详情

hbase:014:0> describe 'student1'

3)修改表

表名创建时写的所有和列族相关的信息,都可以后续通过 alter 修改,包括增加删除列族。

(1)增加列族和修改信息都使用覆盖的方法

hbase:015:0> alter 'student1', {NAME => 'f1', VERSIONS => 3}

(2)删除信息使用特殊的语法

hbase:015:0> alter 'student1', NAME => 'f1', METHOD => 'delete'
hbase:016:0> alter 'student1', 'delete' => 'f1'

4)删除表

shell 中删除表格,需要先将表格状态设置为不可用。

hbase:017:0> disable 'student1'
hbase:018:0> drop 'student1'

4 DML

1)写入数据

在 HBase 中如果想要写入数据,只能添加结构中最底层的 cell。可以手动写入时间戳指定 cell 的版本,推荐不写默认使用当前的系统时间。

hbase:019:0> put 'bigdata:student','1001','info:name','zhangsan'
hbase:020:0> put 'bigdata:student','1001','info:name','lisi'
hbase:021:0> put 'bigdata:student','1001','info:age','18'

如果重复写入相同 rowKey,相同列的数据,会写入多个版本进行覆盖。

2)读取数据

读取数据的方法有两个:get 和 scan。
get 最大范围是一行数据,也可以进行列的过滤,读取数据的结果为多行 cell。

hbase:022:0> get 'bigdata:student','1001'
hbase:023:0> get 'bigdata:student','1001' , {COLUMN => 'info:name'}

也可以修改读取 cell 的版本数,默认读取一个。最多能够读取当前列族设置的维护版本数。

hbase:024:0>get 'bigdata:student','1001' , {COLUMN => 'info:name', VERSIONS => 6}

scan 是扫描数据,能够读取多行数据,不建议扫描过多的数据,推荐使用 startRow 和 stopRow 来控制读取的数据,默认范围左闭右开。

hbase:025:0> scan 'bigdata:student',{STARTROW => '1001',STOPROW => '1002'}

实际开发中使用 shell 的机会不多,所有丰富的使用方法到 API 中介绍。

3)删除数据

删除数据的方法有两个:delete 和 deleteall。
delete 表示删除一个版本的数据,即为 1 个 cell,不填写版本默认删除最新的一个版本。

hbase:026:0> delete 'bigdata:student','1001','info:name'

deleteall 表示删除所有版本的数据,即为当前行当前列的多个 cell。(执行命令会标记数据为要删除,不会直接将数据彻底删除,删除数据只在特定时期清理磁盘时进行)

hbase:027:0> deleteall 'bigdata:student','1001','info:name'