当前位置:   article > 正文

Hbase基本操作

hbase基本操作

目录

HBASE 基本操作

hbase shell:进入hbase shell环境

status命令:查看集群状态

version:查看版本信息

create:创建表

drop 删除表

list:查看所有表

desc :查看表结构

exists :查看表是否存在

表的启动和禁用

列簇的增删改

插入数据

删除第2行的数据

Hbase是Big Table的开源java版本,是建立在HDFS之上,提供高可靠性,高性能,列存储,可伸缩,实时读写的NoSql的数据库系统。

 

Hbase仅能通过逐渐(row key)和主键的range来检索数据,仅支持单行事务

Hbase主要用来存储结构化和半结构化的松散数据

Hbase查询数据功能很简单,不支持join等复杂操作,不支持复杂的事务,从技术上来说,Hbase更像是一个【数据存储】而不是数据库。

Hbase中支持的数据类型“byte[ ]”

hbase和hadoop一样,Hbase目标主要依靠横向扩展,通过不断增阿基廉价的商用服务器,来增加存储和处理能力,例如,把集群从10个节点扩展到20个节点,存储能力和处理能力都会加倍。

Hbase中的表一般有这样的特点:

        大:一个表可以有上十亿行,上百万列

        面向列:面向列(族)的存储和权限控制,列(族)独立检索

        稀疏:对于为空(null)的列,并不占用存储空间,因此,表可以设计的非常稀疏

HBASE 基本操作

hbase shell:进入hbase shell环境

  1. [hadoop@vm2 ~]$ hbase shell
  2. HBase Shell
  3. Use "help" to get list of supported commands.
  4. Use "exit" to quit this interactive shell.
  5. For Reference, please visit: http://hbase.apache.org/2.0/book.html#shell
  6. Version 2.4.8, rf844d09157d9dce6c54fcd53975b7a45865ee9ac, Wed Oct 27 08:48:57 PDT 2021
  7. Took 0.0016 seconds

status命令:查看集群状态

  1. hbase:001:0> status
  2. 1 active master, 1 backup masters, 3 servers, 0 dead, 0.6667 average load
  3. Took 0.7874 seconds

version:查看版本信息

  1. hbase:002:0> version
  2. 2.4.8, rf844d09157d9dce6c54fcd53975b7a45865ee9ac, Wed Oct 27 08:48:57 PDT 2021
  3. Took 0.0018 seconds

create:创建表

命令格式1:create ‘表名’,‘列簇名1’,‘列簇名2’…

Hbase创建表的时候不需要创建所有列簇,对于空(null)的列,并不占用存储空间,因此表可以设计的非常稀疏

  1. # 创建一个名为test的表,包含 hbaseinfo 和 specialinfo 两个列簇
  2. hbase:004:0> create 'test','hbaseinfo','specialinfo'
  3. Created table test
  4. Took 1.3269 seconds
  5. => Hbase::Table - test

drop 删除表

  1. # 删除表之前需要先禁用表
  2. hbase:005:0> disable 'test'
  3. Took 1.0000 seconds # 删除表
  4. hbase:006:0> drop 'test'
  5. Took 0.7162 seconds

list:查看所有表

  1. hbase:008:0> list
  2. TABLE
  3. test
  4. 1 row(s)
  5. Took 0.0131 seconds
  6. => ["test"]

desc :查看表结构

  1. hbase:010:0> desc 'test'
  2. Table test is ENABLED
  3. test
  4. COLUMN FAMILIES DESCRIPTION
  5. {NAME => 'hbaseinfo', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCOD
  6. ING => 'NONE', COMPRESSION => 'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATI
  7. ON_SCOPE => '0'}
  8. {NAME => 'specialinfo', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENC
  9. ODING => 'NONE', COMPRESSION => 'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICA
  10. TION_SCOPE => '0'}
  11. 2 row(s)
  12. Quota is disabled
  13. Took 0.1308 seconds

exists :查看表是否存在

  1. hbase:011:0> exists 'test'
  2. Table test does exist
  3. Took 0.0172 seconds
  4. => true

表的启动和禁用

  1. # 禁用表
  2. hbase:013:0> disable 'test'
  3. Took 0.6758 seconds # 检查表是否被禁用
  4. hbase:014:0> is_disabled 'test'
  5. true
  6. Took 0.0222 seconds
  7. => true
  8. # 启用表
  9. hbase:015:0> enable 'test'
  10. Took 0.6949 seconds # 检查表是否被启用
  11. hbase:016:0> is_enabled 'test'
  12. true
  13. Took 0.0168 seconds
  14. => true

列簇的增删改

  1. # 添加列簇
  2. hbase:018:0> alter 'test','name'
  3. Updating all regions with the new schema...
  4. 1/1 regions updated.
  5. Done.
  6. Took 2.0527 seconds
  7. # 删除列簇
  8. hbase:019:0> alter 'test',{NAME=>'specialinfo',METHOD=>'delete'}
  9. Updating all regions with the new schema...
  10. 1/1 regions updated.
  11. Done.
  12. Took 1.8760 seconds
  13. # 更改列簇存储版本的限制
  14. # 默认情况下列簇只储存一个版本数据,如果需要存储多个版本数据,需要修改列簇属性
  15. hbase:021:0> alter 'test',{NAME=>'hbaseinfo',VERSIONS=>3}
  16. Updating all regions with the new schema...
  17. 1/1 regions updated.
  18. Done.
  19. Took 1.8611 seconds

插入数据

命令格式:put '表名',‘行键','列簇名','列名','值',[时间戳]

  1. hbase:022:0> put 'test','1','hbaseinfo:name','wang'
  2. Took 0.2073 seconds
  3. hbase:023:0> put 'test','1','hbaseinfo:age','20'
  4. Took 0.0137 seconds
  5. hbase:024:0> put 'test','1','hbaseinfo:birthday','2000-01-01'
  6. Took 0.0106 seconds
  7. hbase:025:0> put 'test','1','hbaseinfo:location','Boston'
  8. Took 0.0191 seconds

获取指定行,指定行中的列簇,列的信息

  1. # 获取第1行所有列的数据信息
  2. hbase:026:0> get 'test','1'
  3. COLUMN CELL
  4. hbaseinfo:age timestamp=2023-04-13T02:49:42.913, value=20
  5. hbaseinfo:birthday timestamp=2023-04-13T02:50:07.241, value=2000-01-01
  6. hbaseinfo:location timestamp=2023-04-13T02:52:06.107, value=Boston
  7. hbaseinfo:name timestamp=2023-04-13T02:49:11.233, value=wang
  8. 1 row(s)
  9. Took 0.0808 seconds
  10. # 获取第1行 hbaseinfo 列族里的数据信息
  11. hbase:027:0> get 'test','1','hbaseinfo'
  12. COLUMN CELL
  13. hbaseinfo:age timestamp=2023-04-13T02:49:42.913, value=20
  14. hbaseinfo:birthday timestamp=2023-04-13T02:50:07.241, value=2000-01-01
  15. hbaseinfo:location timestamp=2023-04-13T02:52:06.107, value=Boston
  16. hbaseinfo:name timestamp=2023-04-13T02:49:11.233, value=wang
  17. 1 row(s)
  18. Took 0.0422 seconds
  19. # 获取第1行中,hbaseinfo中的name列数据。
  20. hbase:028:0> get 'test','1','hbaseinfo:name'
  21. COLUMN CELL
  22. hbaseinfo:name timestamp=2023-04-13T02:49:11.233, value=wang
  23. 1 row(s)
  24. Took 0.0215 seconds

删除第2行的数据

delete 'test','2' 

删除第2行指定列的数据

delete 'test','2','hbaseinfo:name'

命名空间

  1. # 创建命名空间
  2. hbase:029:0> create_namespace 'wang'
  3. Took 0.4548 seconds # 列出所有命名空间
  4. hbase:030:0> list_namespace
  5. NAMESPACE
  6. default
  7. hbase
  8. wang
  9. 3 row(s)
  10. Took 0.0567 seconds # 在指定命名空间下面创建表
  11. hbase:031:0> create 'wang:company','abt'
  12. Created table wang:company
  13. Took 1.1369 seconds
  14. => Hbase::Table - wang:company

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/443893
推荐阅读
相关标签
  

闽ICP备14008679号