共计 6691 个字符,预计需要花费 17 分钟才能阅读完成。
本文源码:GitHub || GitEE
一、Hbase 简介
1、根底形容
Hadoop 原生的特点是解决大规模数据的离线批量解决场景,HDFS 具备弱小存储能力,然而并没有提供很强的数据查问机制。HBase 组件则是基于 HDFS 文件系统之上提供相似于 BigTable 服务。
HBase 是一种分布式、可扩大、反对海量结构化数据存储的 NoSQL 数据库。HBase 在 Hadoop 之上提供了相似于 Bigtable 的能力,基于列存储模式的而不是基于行的模式。存储数据特点:非结构化或者涣散的半结构化数据,存储大表天然是须要具备程度扩大的能力,基于服务集群解决海量宏大数据。
2、数据模型
基于 Hbase 的数据结构的根本形容;
- 表 -Table:由行和列组成,列划分为若干个列族;
- 行 -Row:行键 (Key) 作标识,行代表数据对象;
- 列族:列族反对动静扩大,以字符串模式存储;
- 列标识:列族中的数据通过列标识符来定位;
- 单元格:行键,列族,列标识符独特确定一个单元;
- 单元数据:存储在单元里的数据称为单元数据;
- 工夫戳:默认基于工夫戳来进行版本标识;
HBase 的数据模型同关系型数据库很相似,数据存储在一张表中,有行有列。但从 HBase 的底层物理存储构造看更像是 Map(K-V)汇合。
- 数据管理是基于列存储的特点;
- 简略的数据模型,内容存储为字符串;
- 没有简单的表关系,简略的增删查操作;
从整体上看数据模型,HBase 是一个稠密、多维度、排序的映射表,这张表的索引是行键、列族、列限定符和工夫戳每个值是一个未经解释的字符串。
二、搭建集群环境
1、解压文件
tar -zxvf hbase-1.3.1-bin.tar.gz
2、配置环境变量
vim /etc/profile
export HBASE_HOME=/opt/hbase-1.3.1
export PATH=$PATH:$HBASE_HOME/bin
source /etc/profile
3、配置:hbase-env
vim /opt/hbase-1.3.1/conf/hbase-env.sh
export JAVA_HOME=/opt/jdk1.8
export HBASE_MANAGES_ZK=false
4、配置:hbase-site
vim /opt/hbase-1.3.1/conf/hbase-site.xml
<configuration>
<!--HDFS 存储 -->
<property>
<name>hbase.rootdir</name>
<value>hdfs://hop01:9000/HBase</value>
</property>
<!-- 开启集群 -->
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<!-- 端口 -->
<property>
<name>hbase.master.port</name>
<value>16000</value>
</property>
<!--ZK 集群 -->
<property>
<name>hbase.zookeeper.quorum</name>
<value>hop01,hop02,hop03</value>
</property>
<!--ZK 数据 -->
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/data/zookeeper/data/</value>
</property>
</configuration>
5、配置:regionservers
vim /opt/hbase-1.3.1/conf/regionservers
hop01
hop02
hop03
6、配置:软连贯
软连贯 hadoop 配置文件到 HBase
ln -s /opt/hadoop2.7/etc/hadoop/core-site.xml /opt/hbase-1.3.1/conf/core-site.xml
ln -s /opt/hadoop2.7/etc/hadoop/hdfs-site.xml /opt/hbase-1.3.1/conf/hdfs-site.xml
7、同步集群服务环境
也能够手动配置集群,或者应用同步命令。
xsync hbase/
8、启动集群
在 hop01 节点启动即可。
/opt/hbase-1.3.1/bin/start-hbase.sh
启动日志:
hop03: starting regionserver, logging to /opt/hbase-1.3.1/bin/../logs/hbase-root-regionserver-hop03.out
hop02: starting regionserver, logging to /opt/hbase-1.3.1/bin/../logs/hbase-root-regionserver-hop02.out
hop01: starting regionserver, logging to /opt/hbase-1.3.1/bin/../logs/hbase-root-regionserver-hop01.out
9、查看状态
jps
HMaster:主节点
HRegionServer:分区节点
10、进行集群
在 hop01 节点进行即可。
/opt/hbase-1.3.1/bin/stop-hbase.sh
11、查看界面
http://hop01:16010
三、根底 Shell 命令
1、切入客户端
/opt/hbase-1.3.1/bin/hbase shell
2、查看表
hbase(main):002:0> list
3、创立表
hbase(main):003:0> create 'user','info'
0 row(s) in 2.7910 seconds
=> Hbase::Table - user
4、查看表构造
hbase(main):010:0> describe 'user'
5、增加数据
put 'user','id01','info:name','tom'
put 'user','id01','info:age','18'
put 'user','id01','info:sex','male'
put 'user','id02','info:name','jack'
put 'user','id02','info:age','20'
put 'user','id02','info:sex','female'
6、查看表数据
hbase(main):010:0> scan 'user'
ROW COLUMN+CELL
id01 column=info:age, timestamp=1594448524308, value=18
id01 column=info:name, timestamp=1594448513534, value=tom
id01 column=info:sex, timestamp=1594448530817, value=male
id02 column=info:age, timestamp=1594448542631, value=20
id02 column=info:name, timestamp=1594448536520, value=jack
id02 column=info:sex, timestamp=1594448548005, value=female
这些表构造和数据会在集群之间主动同步。
7、查问指定列
hbase(main):012:0> get 'user','id01'
COLUMN CELL
info:age timestamp=1594448524308, value=18
info:name timestamp=1594448513534, value=tom
info:sex timestamp=1594448530817, value=male
8、统计行数
hbase(main):013:0> count 'user'
9、删除行数据
hbase(main):014:0> deleteall 'user','id02'
10、清空表数据
hbase(main):016:0> truncate 'user'
11、删除表
hbase(main):018:0> disable 'user'
hbase(main):019:0> drop 'user'
四、JDBC 根底查问
1、外围依赖
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.1</version>
</dependency>
2、根底配置
这里连贯 zookeeper 集群地址即可。
zookeeper:
address: 集群地址 Url,逗号分隔
编写 HBase 配置和常用工具办法。
@Component
public class HBaseConfig {
private static String address;
private static final Object lock=new Object();
public static Configuration configuration = null;
public static ExecutorService executor = null;
public static Connection connection = null;
/**
* 获取连贯
*/
public static Connection getConnection(){if(null == connection){synchronized (lock) {if(null == connection){configuration = new Configuration();
configuration.set("hbase.zookeeper.quorum", address);
try {executor = Executors.newFixedThreadPool(10);
connection = ConnectionFactory.createConnection(configuration, executor);
} catch (Exception e) {e.printStackTrace();
}
}
}
}
return connection;
}
/**
* 获取 HBaseAdmin
*/
public static HBaseAdmin getHBaseAdmin(){
HBaseAdmin admin = null;
try{admin = (HBaseAdmin)getConnection().getAdmin();
}catch(Exception e){e.printStackTrace();
}
return admin;
}
/**
* 获取 Table
*/
public static Table getTable(TableName tableName) {
Table table = null ;
try{table = getConnection().getTable(tableName);
}catch(Exception e){e.printStackTrace();
}
return table ;
}
/**
* 敞开资源
*/
public static void close(HBaseAdmin admin,Table table){
try {if(admin!=null) {admin.close();
}
if(table!=null) {table.close();
}
} catch (IOException e) {e.printStackTrace();
}
}
@Value("${zookeeper.address}")
public void setAddress (String address) {HBaseConfig.address = address;}
}
3、查问案例
查问数据参考上述全表扫描后果:
@RestController
public class HBaseController {
/**
* 扫描全表
*/
@GetMapping("/scanTable")
public String scanTable () throws Exception {Table table = HBaseConfig.getTable(TableName.valueOf("user"));
try {ResultScanner resultScanner = table.getScanner(new Scan());
for (Result result : resultScanner) {printResult(result);
}
} finally {HBaseConfig.close(null, table);
}
return "success";
}
/**
* 依据 RowKey 扫描
*/
@GetMapping("/scanRowKey")
public void scanRowKey() throws Exception {
String rowKey = "id02";
Table table = HBaseConfig.getTable(TableName.valueOf("user"));
try {Result result = table.get(new Get(rowKey.getBytes()));
printResult(result);
} finally {HBaseConfig.close(null, table);
}
}
/**
* 输入 Result
*/
private void printResult (Result result){NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = result.getMap();
Set<Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>>> set = map.entrySet();
for (Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> entry : set) {Set<Map.Entry<byte[], NavigableMap<Long, byte[]>>> entrySet = entry.getValue().entrySet();
for (Map.Entry<byte[], NavigableMap<Long, byte[]>> entry2 : entrySet) {System.out.print(new String(result.getRow()));
System.out.print("\t");
System.out.print(new String(entry.getKey()));
System.out.print(":");
System.out.print(new String(entry2.getKey()));
System.out.print("value =");
System.out.println(new String(entry2.getValue().firstEntry().getValue()));
}
}
}
}
五、源代码地址
GitHub·地址
https://github.com/cicadasmile/big-data-parent
GitEE·地址
https://gitee.com/cicadasmile/big-data-parent
举荐浏览:编程体系整顿
序号 | 项目名称 | GitHub 地址 | GitEE 地址 | 举荐指数 |
---|---|---|---|---|
01 | Java 形容设计模式, 算法, 数据结构 | GitHub·点这里 | GitEE·点这里 | ☆☆☆☆☆ |
02 | Java 根底、并发、面向对象、Web 开发 | GitHub·点这里 | GitEE·点这里 | ☆☆☆☆ |
03 | SpringCloud 微服务根底组件案例详解 | GitHub·点这里 | GitEE·点这里 | ☆☆☆ |
04 | SpringCloud 微服务架构实战综合案例 | GitHub·点这里 | GitEE·点这里 | ☆☆☆☆☆ |
05 | SpringBoot 框架根底利用入门到进阶 | GitHub·点这里 | GitEE·点这里 | ☆☆☆☆ |
06 | SpringBoot 框架整合开发罕用中间件 | GitHub·点这里 | GitEE·点这里 | ☆☆☆☆☆ |
07 | 数据管理、分布式、架构设计根底案例 | GitHub·点这里 | GitEE·点这里 | ☆☆☆☆☆ |
08 | 大数据系列、存储、组件、计算等框架 | GitHub·点这里 | GitEE·点这里 | ☆☆☆☆☆ |