本文原始碼: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=https://www.cnblogs.com/cicada-smile/archive/2021/01/11/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=https://www.cnblogs.com/cicada-smile/archive/2021/01/11/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 = "https://www.cnblogs.com/cicada-smile/archive/2021/01/11/);
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·點這里 | ☆☆☆☆☆ |
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/247141.html
標籤:其他
下一篇:MySQL資料庫條件查詢
