HBase的安装和使用

概述

HBase(Hadoop Database),是一个基于Google BigTable论文设计的高可靠性、高性能、可伸缩的分布式存储系统。

CAP

CAP原则又称CAP定理,指的是在一个分布式系统中,一致性(Consistency)、可用性(Availability)、分区容错性(Partition tolerance)。CAP 原则指的是,这三个要素最多只能同时实现两点,不可能三者兼顾。

列存储

概念要从RDBMS说起,RDBMS操作的最小单元是行级数据
即使是查询或者更新某个表中某个字段,对于RDBMS来说都是加载整行数据来进行数据的修改
因此RDBMS在处理单个数据的处理方式上,性能并不高,因为会做一些无用的IO操作。

基于列式存储的数据库支持稀疏存储

将所有IO操作特性相似的字段归为一类

底层会为HBASE的列簇做独立存储和索引

HBASE排序顺序:rowkey>列簇>列名>时间戳

官网地址: http://hbase.apache.org/

HBase是一种构建在HDFS之上的分布式、面向列的存储系统。在需要实时读写、随机访问超大规模数据集时,可 以使用HBase。

HBase 环境搭建-单机

基础环境

  • Hadoop
  • Zookeeper

安装和配置

[root@HadoopNode00 ~]# tar -zxvf hbase-1.2.4-bin.tar.gz -C /home/hbase/ # 解压至对应的目录
[root@HadoopNode00 ~]# vi .bashrc   # 配置habse 环境变量
export HBASE_HOME=/home/hbase/hbase-1.2.4
export HBASE_MANAGES_ZK=false  # 使用外部ZK
export PATH=$PATH:$HBASE_HOME/bin[root@HadoopNode00 ~]# source .bashrc    # 使环境变量生效[root@HadoopNode00 ~]# vi /home/hbase/hbase-1.2.4/conf/hbase-site.xml
<configuration><property><name>hbase.rootdir</name><value>hdfs://HadoopNode00:9000/hbase</value></property><property><name>hbase.cluster.distributed</name><value>true</value></property><property><name>hbase.zookeeper.quorum</name><value>HadoopNode00</value></property><property><name>hbase.zookeeper.property.clientPort</name><value>2181</value></property>
</configuration>
[root@HadoopNode00 ~]# vi /home/hbase/hbase-1.2.4/conf/regionservers
HadoopNode00

启动

[root@HadoopNode00 ~]# start-dfs.sh   # 首先保证hdfs启动
[root@HadoopNode00 ~]# /home/zk/zookeeper-3.4.6/bin/zkServer.sh start /home/zk/zookeeper-3.4.6/conf/zk.cfg  # 首先保证zk启动
[root@HadoopNode00 ~]# start-hbase.sh  # 直接通过指令启动
[root@HadoopNode00 ~]# jps
1699 NameNode
2052 SecondaryNameNode
40660 QuorumPeerMain
42020 Jps
1851 DataNode
41708 HRegionServer  #  健康存活
18476 NodeManager
41548 HMaster        #  健康存活
18239 ResourceManager

连接

[root@HadoopNode00 ~]# hbase shell

Web UI

http://hostname:16010

Shell 操作

常见命令
status, table_help, version, whoami

命名空间操作

alter_namespace, create_namespace, describe_namespace, drop_namespace, list_namespace, list_namespace_tables
创建命名空间

hbase(main):005:0> create_namespace 'zpark',{'user'=>'zhangsan'}
0 row(s) in 0.0560 seconds
#  需要注意的指定值为XXX 不能用等于号  而需要用‘=>’
hbase(main):006:0>  list_namespace
list_namespace          list_namespace_tables
hbase(main):006:0>  list_namespace
NAMESPACE
default
hbase
zpark
3 row(s) in 0.0470 seconds

描述命名空间

hbase(main):008:0> describe_namespace 'zpark'
DESCRIPTION
{NAME => 'zpark', user => 'zhangsan'}
1 row(s) in 0.0450 seconds

修改命名空间

hbase(main):009:0> alter_namespace 'zpark',{METHOD => 'set' ,'user'=>'lqq'}
0 row(s) in 0.0460 secondshbase(main):010:0> describe_namespace 'zpark'
DESCRIPTION
{NAME => 'zpark', user => 'lqq'}
1 row(s) in 0.0020 seconds

删除命名空间 属性

hbase(main):012:0> alter_namespace 'zpark',{METHOD => 'unset' ,NAME=>'user'}
0 row(s) in 0.0450 secondshbase(main):013:0> describe_namespace 'zpark'
DESCRIPTION
{NAME => 'zpark'}
1 row(s) in 0.0090 seconds

显示所有命名空间

hbase(main):015:0> list_namespace
NAMESPACE
default
hbase
zpark
3 row(s) in 0.0270 seconds

删除命名空间

hbase(main):016:0> drop_namespace
ERROR: wrong number of arguments (0 for 1)Here is some help for this command:
Drop the named namespace. The namespace must be empty. # 命名空间必须为空hbase(main):017:0> drop_namespace 'zpark'
0 row(s) in 0.0480 seconds

显示某个命名空间下的表

hbase(main):003:0> list_namespace_tables 'lqq'
TABLE
t_user
1 row(s) in 0.0330 seconds

DDL 数据定义语言

对命名空间(数据库)的中表进行操作
创建表

hbase(main):004:0> create 'lqq:t_user','cf1','cf2'hbase(main):003:0> list_namespace_tables 'lqq'
TABLE
t_user
1 row(s) in 0.0330 seconds

查看表详情

hbase(main):004:0> describe 'lqq:t_user'
Table lqq:t_user is ENABLED
baizhi:t_user
COLUMN FAMILIES DESCRIPTION
{NAME => 'cf1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODIN
G => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICA
TION_SCOPE => '0'}
{NAME => 'cf2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODIN
G => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICA
TION_SCOPE => '0'}
2 row(s) in 0.1400 seconds

删除表

  • 注意在删除前 需要先将 表 disable 掉
hbase(main):001:0> drop 'lqq:t_user'ERROR: Table lqq:t_user is enabled. Disable it first.Here is some help for this command:
Drop the named table. Table must first be disabled:hbase> drop 't1'hbase> drop 'ns1:t1'hbase(main):002:0> disable 'lqq:t_user'
0 row(s) in 2.5940 secondshbase(main):003:0> drop 'lqq:t_user'
0 row(s) in 1.5920 seconds

显示所有表

hbase(main):004:0> list
TABLE
0 row(s) in 0.0190 seconds

数据的CURD DML(数据管理语言)

插入(put)

# 插入一条数据 在lqq:t_user  行健为1  列簇cf1 字段名为name 值为zs
hbase(main):012:0> put 'lqq:t_user',1,'cf1:name','zs'
0 row(s) in 0.1420 seconds
hbase(main):001:0> t = get_table 'lqq:t_user'   # 做表的引用
0 row(s) in 0.0330 seconds=> Hbase::Table - lqq:t_user
# 插入一条数据 在lqq:t_user  行健为1  列簇cf1 字段名为sex 值为true
hbase(main):002:0> t.put 1,'cf1:sex','true'
0 row(s) in 0.3510 seconds
# 插入一条数据 在lqq:t_user  行健为1  列簇cf1 字段名为age 值为18
hbase(main):003:0> t.put 1,'cf1:age',18
0 row(s) in 0.0210 secondshbase(main):004:0> t.get 1
COLUMN                           CELLcf1:age                         timestamp=1572888118460, value=18cf1:name                        timestamp=1572887983788, value=zscf1:sex                         timestamp=1572888095574, value=true
3 row(s) in 0.0500 seconds

更新

# 创建一个可以多个版本的表
hbase(main):005:0> create 'lqq:t_user',{NAME=>'cf1',VERSIONS=>3},{NAME=>'cf2',VERSIONS=>3}
0 row(s) in 1.2410 seconds=> Hbase::Table - lqq:t_user
# 插入基础的数据
hbase(main):012:0> t.put 1,'cf1:name','zs'
0 row(s) in 0.0870 seconds
hbase(main):013:0> t.put 1,'cf1:age',18
0 row(s) in 0.0220 seconds
hbase(main):014:0> t.put 1,'cf1:sex',true
0 row(s) in 0.0270 seconds# 进行数据的更新
hbase(main):015:0> t.put 1,'cf1:name','zhangsan'
0 row(s) in 0.0080 seconds# 可以看到数据已经变为其他数据
hbase(main):016:0> t.get 1
COLUMN                           CELLcf1:age                         timestamp=1572888599018, value=18cf1:name                        timestamp=1572888630027, value=zhangsancf1:sex                         timestamp=1572888605427, value=true
3 row(s) in 0.0350 seconds

取值(get)

# 获取所有rowkey 为1 列簇为cf1  列名为name  最多获取三个版本的数据
hbase(main):017:0> t.get 1 ,{COLUMNS=>'cf1:name',VERSIONS=>3}
COLUMN                           CELLcf1:name                        timestamp=1572888630027, value=zhangsancf1:name                        timestamp=1572888590148, value=zs
2 row(s) in 0.0300 seconds# 根据某个时间戳进行获取
hbase(main):020:0> t.get 1 ,{COLUMNS=>'cf1:name',TIMESTAMP => 1572888590148}
COLUMN                           CELLcf1:name                        timestamp=1572888590148, value=zs
1 row(s) in 0.0170 seconds# 根据时间戳区间进行获取
hbase(main):024:0> t.get 1 ,{COLUMNS=>'cf1:name',TIMERANGE => [157288850147,1572888630030],VERSIONS => 4}
COLUMN                           CELLcf1:name                        timestamp=1572888630027, value=zhangsancf1:name                        timestamp=1572888590148, value=zs
2 row(s) in 0.0160 seconds

删除(delete/deleteall)

# 直接进行删除
hbase(main):025:0> delete 'lqq:t_user',1,'cf1:name'
0 row(s) in 0.0560 seconds
# 引用删除
hbase(main):027:0> t.delete 1,'cf1:sex'
0 row(s) in 0.0290 seconds
# 删除某个id下的所有数据
hbase(main):029:0> t.deleteall 1
0 row(s) in 0.0150 seconds# 删除某个id某个列簇某个字段所有版本的值
hbase(main):043:0> t.deleteall 1 ,'cf1:name'
0 row(s) in 0.0150 seconds

全表扫描

hbase(main):049:0> t.scan
ROW                              COLUMN+CELL1                               column=cf1:age, timestamp=1572889604496, value=181                               column=cf1:name, timestamp=1572889601629, value=zs1                               column=cf1:sex, timestamp=1572889608032, value=true
1 row(s) in 0.0240 seconds

计数(count)

hbase(main):050:0> t.count
1 row(s) in 0.0590 seconds
=> 1

追加(append)

hbase(main):051:0> t.append 1,'cf1:name','123'
0 row(s) in 0.0220 seconds
hbase(main):052:0> t.scan
ROW                              COLUMN+CELL1                               column=cf1:age, timestamp=1572889604496, value=181                               column=cf1:name, timestamp=1572889732899, value=zs1231                               column=cf1:sex, timestamp=1572889608032, value=true
1 row(s) in 0.0240 seconds

清空数据

hbase(main):053:0> truncate 'lqq:t_user'
Truncating 'lqq:t_user' table (it may take a while):- Disabling table...- Truncating table...
0 row(s) in 3.9640 secondshbase(main):054:0> t.scan
ROW                              COLUMN+CELL
0 row(s) in 0.1440 seconds

Java API

依赖

获取客户端

 private Connection connection;private Admin admin;@Beforepublic void getAdmin() throws Exception {Configuration conf = new Configuration();conf.set("hbase.zookeeper.quorum", "HadoopNode00");conf.set("hbase.zookeeper.property.clientPort", "2181");connection = ConnectionFactory.createConnection(conf);admin = connection.getAdmin();}

关闭资源

 @Afterpublic void close() throws Exception {admin.close();connection.close();}

命名空间操作

  • 创建命名空间
@Testpublic void createNameSpace() throws Exception {NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create("hadoop1").addConfiguration("lqq", "123").build();admin.createNamespace(namespaceDescriptor);}
  • 修改命名空间
 @Testpublic void changeNameSpace()  throws Exception{NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create("hadoop").removeConfiguration("lqq").build();admin.modifyNamespace(namespaceDescriptor);}
  • 删除命名空间
@Testpublic void deleteNameSpace() throws Exception{admin.deleteNamespace("hadoop");}
  • 列出命名空间
 @Testpublic void listNameSpace() throws Exception {NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) {System.out.println(namespaceDescriptor.getName());}}

表操作

  • 创建表
@Testpublic void createTable() throws Exception {/** 将表的名字信息封装到TableName中* */TableName tableName = TableName.valueOf("lqq:t_java");/*** 创建描述表的对象 并提供表的名字* */HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);/** 描述列簇的对象  并指定列簇的名字* */HColumnDescriptor cf1 = new HColumnDescriptor("cf1");// 设置 最大可存的版本cf1.setMaxVersions(3);/** 描述列簇的对象  并指定列簇的名字* */HColumnDescriptor cf2 = new HColumnDescriptor("cf2");// 设置 最大可存的版本cf2.setMaxVersions(3);// 在表中添加必要的属性:列簇tableDescriptor.addFamily(cf1);tableDescriptor.addFamily(cf2);/** 使用admin对象创建表* */admin.createTable(tableDescriptor);}
  • 删除表
@Testpublic void deleteTable() throws Exception {TableName tableName = TableName.valueOf("lqq:t_java");if (admin.tableExists(tableName)) {admin.disableTable(tableName);admin.deleteTable(tableName);}}

CURD

  • put

插入|更新单个记录

   @Testpublic void putData() throws Exception {TableName tableName = TableName.valueOf("lqq:t_user");Table table = connection.getTable(tableName);/** 封装  一行 数据* */Put put = new Put("1".getBytes());/** 参数列表  :  列簇  列名  值* */put.addColumn("cf1".getBytes(), "name".getBytes(), "zhangsan".getBytes());put.addColumn("cf1".getBytes(), "pwd".getBytes(), "123".getBytes());put.addColumn("cf2".getBytes(), "age".getBytes(), "18".getBytes());put.addColumn("cf2".getBytes(), "salary".getBytes(), "1000".getBytes());table.put(put);table.close();}

批量插入

 @Testpublic void putManyData() throws Exception {TableName tableName = TableName.valueOf("lqq:t_user");Table table = connection.getTable(tableName);/** 封装  一行 数据* */Put put = new Put("2".getBytes());/** 参数列表  :  列簇  列名  值* */put.addColumn("cf1".getBytes(), "name".getBytes(), "lisi".getBytes());put.addColumn("cf1".getBytes(), "pwd".getBytes(), "123".getBytes());put.addColumn("cf2".getBytes(), "age".getBytes(), "20".getBytes());put.addColumn("cf2".getBytes(), "salary".getBytes(), "20000".getBytes());ArrayList<Put> puts = new ArrayList<Put>();puts.add(put);table.put(puts);table.close();}
@Testpublic void putManyData() throws Exception {TableName tableName = TableName.valueOf("lqq:t_user");BufferedMutator bufferedMutator = connection.getBufferedMutator(tableName);/** 封装  一行 数据* */Put put = new Put("2".getBytes());/** 参数列表  :  列簇  列名  值* */put.addColumn("cf1".getBytes(), "name".getBytes(), "ls".getBytes());put.addColumn("cf1".getBytes(), "pwd".getBytes(), "123".getBytes());put.addColumn("cf2".getBytes(), "age".getBytes(), "20".getBytes());put.addColumn("cf2".getBytes(), "salary".getBytes(), "20000".getBytes());ArrayList<Put> puts = new ArrayList<Put>();puts.add(put);bufferedMutator.mutate(puts);bufferedMutator.close();}

delete

    @Testpublic void deleteData() throws Exception {TableName tableName = TableName.valueOf("lqq:t_user");Table table = connection.getTable(tableName);Delete delete = new Delete("2".getBytes());table.delete(delete);table.close();}
  • 批量删除
 @Testpublic void deleteManyData() throws Exception {TableName tableName = TableName.valueOf("lqq:t_user");Table table = connection.getTable(tableName);Delete delete = new Delete("1".getBytes());table.delete(delete);table.close();}

get

@Testpublic void getData() throws Exception{Table table = connection.getTable(TableName.valueOf("lqq:t_user"));Get get = new Get("2".getBytes());Result result = table.get(get);/** 列簇    列名* */byte[] name = result.getValue("cf1".getBytes(), "name".getBytes());byte[] pwd = result.getValue("cf1".getBytes(), "pwd".getBytes());byte[] age = result.getValue("cf2".getBytes(), "age".getBytes());byte[] salary = result.getValue("cf2".getBytes(), "salary".getBytes());System.out.println("名字为:"+Bytes.toString(name)+", 密码为:"+Bytes.toString(pwd)+",年龄为:"+Bytes.toString(age)+",工资为:"+Bytes.toString(salary));}
  • 获取多个版本的数据
 @Testpublic void getManyData() throws Exception {Table table = connection.getTable(TableName.valueOf("baizhi:t_user"));Get get = new Get("2".getBytes());get.setMaxVersions(3);get.addColumn("cf1".getBytes(), "name".getBytes());Result result = table.get(get);List<Cell> columnCells = result.getColumnCells("cf1".getBytes(), "name".getBytes());for (Cell columnCell : columnCells) {byte[] rowData = CellUtil.cloneRow(columnCell);byte[] cfData = CellUtil.cloneFamily(columnCell);byte[] qualifierData = CellUtil.cloneQualifier(columnCell);byte[] data = CellUtil.cloneValue(columnCell);System.out.println("行健为:" + Bytes.toString(rowData) + ", 列簇为:" + Bytes.toString(cfData) + ",列名为:" + Bytes.toString(qualifierData) + ",名字为:" + Bytes.toString(data));}}

scan

@Testpublic void scanData() throws Exception {Table table = connection.getTable(TableName.valueOf("baizhi:t_user"));Scan scan = new Scan();// scan.addFamily("cf1".getBytes());//scan.addColumn("cf1".getBytes(),"name".getBytes());PrefixFilter prefixFilter1 = new PrefixFilter("1".getBytes());PrefixFilter prefixFilter2 = new PrefixFilter("2".getBytes());FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE, prefixFilter1, prefixFilter2);scan.setFilter(filterList);ResultScanner scanner = table.getScanner(scan);for (Result result : scanner) {System.out.println("------------------");byte[] name = result.getValue("cf1".getBytes(), "name".getBytes());byte[] pwd = result.getValue("cf1".getBytes(), "pwd".getBytes());byte[] age = result.getValue("cf2".getBytes(), "age".getBytes());byte[] salary = result.getValue("cf2".getBytes(), "salary".getBytes());System.out.println("名字为:" + Bytes.toString(name) + ", 密码为:" + Bytes.toString(pwd) + ",年龄为:" + Bytes.toString(age) + ",工资为:" + Bytes.toString(salary));}scanner.close();table.close();}

HBase的安装和使用相关推荐

  1. Hadoop集群搭建(六:HBase的安装配置)

    实验 目的 要求 目的: 1.HBase的高可用完全分布模式的安装和验证 要求: 完成HBase的高可用完全分布模式的安装: HBase的相关服务进程能够正常的启动: HBase控制台能够正常使用: ...

  2. HBase的安装、写入和查询操作

    实验材料及说明 在Ubuntu系统的/学号(每个人之间的学号)/salesInfo目录下,有买家的购买记录文件Sales,该文件记录了买家的id,购买商品的id以及购买日期,文件为名为Sales.Sa ...

  3. 安装hbas_非常详细的HBase的安装与配置

    本文讲述如何安装.部署 HBase 集群,以及如何通过命令行方式来完成 HBase 集群的启动和停止. 首先介绍部署 HBase 之前需要做的准备工作,如 Java.SSH 和 Hadoop 这些先决 ...

  4. HBase的安装与使用

    1.安装 由于还是学习阶段,所以没有在生产环境练习,就在本地建了个虚拟机进行HBase的安装. 下载地址http://www.apache.org/dyn/closer.cgi/hbase/,选择一个 ...

  5. HBase单机版安装详细步骤

    HBase介绍 HBase是一个分布式的.面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文"Bigtable:一个结构化数据的分布式存储系统".就像B ...

  6. Hbase单点安装Version1.1.5

    Hbase单点安装,基于版本1.1.5, 使用hbase-1.1.5.tar.gz安装包. 1.安装说明 使用Hbase自带zookeeper和本地文件目录存储数据 2.安装规划 角色规划 IP/机器 ...

  7. 【hbase】HBASE的安装与配置的步骤详解

    在开发中,HBASE的安装一般都是有相关人员安装与维护,这里只是学习的写笔记而已.     一.上传解压:将需要安装的jar包上传解压到指定目录.   二.修改配置:下图是需要修改的配置文件的内容 配 ...

  8. hadoop+zookeeper+Hbase+spark安装部署总结

    hadoop+zookeeper+Hbase+spark安装部署总结 主要参考:https://blog.csdn.net/sunxiaoju/article/details/85918135 计算机 ...

  9. HBase Windows 安装

    一.写在前面   在安装HBase之前,我们需要先安装JDK和Hadoop,具体JDK和Hadoop的安装我前面已经做过了,需要的话,请看我的另一篇博客:Hadoop Windows 安装   还是那 ...

最新文章

  1. C++语言map和unordered_map的下标操作
  2. 第四范式先知平台成为首个通过金融信创适配验证的AI产品
  3. MobX - 基于响应式的状态管理
  4. wso2 esb_WSO2 ESB的一种消息传递方式
  5. centos7重装python_CentOS7重装yum和python
  6. 用Python批量更改图片大小
  7. [self Introduce]热情洋溢的白羊座
  8. Awvs 12.x安装教程
  9. 光环python培训
  10. c++ 计算正弦的近似值_C语言中计算正弦的相关函数总结
  11. nginx的access.log文件详解
  12. 2:STM32CubeMX配置STM32F103C8T6驱动-SPI驱动
  13. Redis中Set数据类型常用命令了解
  14. 如何给导师发邮件?【附带邮件模板】
  15. 中国民营企业的8+10种死法
  16. 打印20以内的素数c语言,c语言编程输出2~100之间的所有素数(每行输出10个),并将它们打印出来....
  17. 常见分布式ID生成方案
  18. IOS学习之苹果设备分辨率一览表
  19. Ubuntu16.04的安装教程
  20. js+jquery检测用户浏览器型号(包括对360浏览器的检测)

热门文章

  1. monkey的基本操作命令
  2. 《软件工程》-用户界面设计
  3. MIT6.828学习之Lab1
  4. 微信小程序获取当前页面url
  5. 任正非谈成功秘诀:28年只对准一个城墙口冲锋
  6. html5中插入样式表方法,如何插入css样式?
  7. Python度分秒与度的互转
  8. 实验十八 CISCO设备IOS的备份与升级
  9. 不要在意FIl短期,FIl未来价值可期
  10. 训练过程出现trian_dice一直大于1(mask范围0-255转为0-1)