数据仓库组件:HBase集群环境搭建和应用案例
本文源碼:GitHub || GitEE
一、Hbase簡(jiǎn)介
1、基礎(chǔ)描述
Hadoop原生的特點(diǎn)是解決大規(guī)模數(shù)據(jù)的離線批量處理場(chǎng)景,HDFS具備強(qiáng)大存儲(chǔ)能力,但是并沒有提供很強(qiáng)的數(shù)據(jù)查詢機(jī)制。HBase組件則是基于HDFS文件系統(tǒng)之上提供類似于BigTable服務(wù)。
HBase是一種分布式、可擴(kuò)展、支持海量結(jié)構(gòu)化數(shù)據(jù)存儲(chǔ)的NoSQL數(shù)據(jù)庫(kù)。HBase在Hadoop之上提供了類似于Bigtable的能力,基于列存儲(chǔ)模式的而不是基于行的模式。存儲(chǔ)數(shù)據(jù)特點(diǎn):非結(jié)構(gòu)化或者松散的半結(jié)構(gòu)化數(shù)據(jù),存儲(chǔ)大表自然是需要具備水平擴(kuò)展的能力,基于服務(wù)集群處理海量龐大數(shù)據(jù)。
2、數(shù)據(jù)模型
基于Hbase的數(shù)據(jù)結(jié)構(gòu)的基本描述;
- 表-Table:由行和列組成,列劃分為若干個(gè)列族;
- 行-Row:行鍵(Key)作標(biāo)識(shí),行代表數(shù)據(jù)對(duì)象;
- 列族:列族支持動(dòng)態(tài)擴(kuò)展,以字符串形式存儲(chǔ);
- 列標(biāo)識(shí):列族中的數(shù)據(jù)通過列標(biāo)識(shí)符來定位;
- 單元格:行鍵,列族,列標(biāo)識(shí)符共同確定一個(gè)單元;
- 單元數(shù)據(jù):存儲(chǔ)在單元里的數(shù)據(jù)稱為單元數(shù)據(jù);
- 時(shí)間戳:默認(rèn)基于時(shí)間戳來進(jìn)行版本標(biāo)識(shí);
HBase的數(shù)據(jù)模型同關(guān)系型數(shù)據(jù)庫(kù)很類似,數(shù)據(jù)存儲(chǔ)在一張表中,有行有列。但從HBase的底層物理存儲(chǔ)結(jié)構(gòu)看更像是Map(K-V)集合。
- 數(shù)據(jù)管理是基于列存儲(chǔ)的特點(diǎn);
- 簡(jiǎn)單的數(shù)據(jù)模型,內(nèi)容存儲(chǔ)為字符串;
- 沒有復(fù)雜的表關(guān)系,簡(jiǎn)單的增刪查操作;
從整體上看數(shù)據(jù)模型,HBase是一個(gè)稀疏、多維度、排序的映射表,這張表的索引是行鍵、列族、列限定符和時(shí)間戳每個(gè)值是一個(gè)未經(jīng)解釋的字符串。
二、搭建集群環(huán)境
1、解壓文件
tar -zxvf hbase-1.3.1-bin.tar.gz2、配置環(huán)境變量
vim /etc/profileexport HBASE_HOME=/opt/hbase-1.3.1 export PATH=$PATH:$HBASE_HOME/binsource /etc/profile3、配置:hbase-env
vim /opt/hbase-1.3.1/conf/hbase-env.shexport JAVA_HOME=/opt/jdk1.8 export HBASE_MANAGES_ZK=false4、配置:hbase-site
vim /opt/hbase-1.3.1/conf/hbase-site.xml<configuration><!--HDFS存儲(chǔ)--><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數(shù)據(jù)--> <property> <name>hbase.zookeeper.property.dataDir</name><value>/data/zookeeper/data/</value></property> </configuration>5、配置:regionservers
vim /opt/hbase-1.3.1/conf/regionservershop01 hop02 hop036、配置:軟連接
軟連接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.xml7、同步集群服務(wù)環(huán)境
也可以手動(dòng)配置集群,或者使用同步命令。
xsync hbase/8、啟動(dòng)集群
在hop01節(jié)點(diǎn)啟動(dòng)即可。
/opt/hbase-1.3.1/bin/start-hbase.sh啟動(dòng)日志:
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.out9、查看狀態(tài)
jpsHMaster:主節(jié)點(diǎn) HRegionServer:分區(qū)節(jié)點(diǎn)10、停止集群
在hop01節(jié)點(diǎn)停止即可。
/opt/hbase-1.3.1/bin/stop-hbase.sh11、查看界面
http://hop01:16010三、基礎(chǔ)Shell命令
1、切入客戶端
/opt/hbase-1.3.1/bin/hbase shell2、查看表
hbase(main):002:0> list3、創(chuàng)建表
hbase(main):003:0> create 'user','info' 0 row(s) in 2.7910 seconds => Hbase::Table - user4、查看表結(jié)構(gòu)
hbase(main):010:0> describe 'user'5、添加數(shù)據(jù)
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、查看表數(shù)據(jù)
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這些表結(jié)構(gòu)和數(shù)據(jù)會(huì)在集群之間自動(dòng)同步。
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=male8、統(tǒng)計(jì)行數(shù)
hbase(main):013:0> count 'user'9、刪除行數(shù)據(jù)
hbase(main):014:0> deleteall 'user','id02'10、清空表數(shù)據(jù)
hbase(main):016:0> truncate 'user'11、刪除表
hbase(main):018:0> disable 'user' hbase(main):019:0> drop 'user'四、JDBC基礎(chǔ)查詢
1、核心依賴
<dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-client</artifactId><version>1.3.1</version> </dependency>2、基礎(chǔ)配置
這里連接zookeeper集群地址即可。
zookeeper:address: 集群地址Url,逗號(hào)分隔編寫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 ;}/*** 關(guān)閉資源*/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、查詢案例
查詢數(shù)據(jù)參考上述全表掃描結(jié)果:
@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";}/*** 根據(jù)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推薦閱讀:編程體系整理
| 01 | Java描述設(shè)計(jì)模式,算法,數(shù)據(jù)結(jié)構(gòu) | GitHub·點(diǎn)這里 | GitEE·點(diǎn)這里 | ☆☆☆☆☆ |
| 02 | Java基礎(chǔ)、并發(fā)、面向?qū)ο蟆eb開發(fā) | GitHub·點(diǎn)這里 | GitEE·點(diǎn)這里 | ☆☆☆☆ |
| 03 | SpringCloud微服務(wù)基礎(chǔ)組件案例詳解 | GitHub·點(diǎn)這里 | GitEE·點(diǎn)這里 | ☆☆☆ |
| 04 | SpringCloud微服務(wù)架構(gòu)實(shí)戰(zhàn)綜合案例 | GitHub·點(diǎn)這里 | GitEE·點(diǎn)這里 | ☆☆☆☆☆ |
| 05 | SpringBoot框架基礎(chǔ)應(yīng)用入門到進(jìn)階 | GitHub·點(diǎn)這里 | GitEE·點(diǎn)這里 | ☆☆☆☆ |
| 06 | SpringBoot框架整合開發(fā)常用中間件 | GitHub·點(diǎn)這里 | GitEE·點(diǎn)這里 | ☆☆☆☆☆ |
| 07 | 數(shù)據(jù)管理、分布式、架構(gòu)設(shè)計(jì)基礎(chǔ)案例 | GitHub·點(diǎn)這里 | GitEE·點(diǎn)這里 | ☆☆☆☆☆ |
| 08 | 大數(shù)據(jù)系列、存儲(chǔ)、組件、計(jì)算等框架 | GitHub·點(diǎn)這里 | GitEE·點(diǎn)這里 | ☆☆☆☆☆ |
總結(jié)
以上是生活随笔為你收集整理的数据仓库组件:HBase集群环境搭建和应用案例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 收藏:Sql类型与.Net(C#)类型对
- 下一篇: GHOST光盘制作详细教程