文章做者:foochane html
原文連接:https://foochane.cn/article/2019062801.htmljava
Hbase
是一個分佈式數據庫,能夠提供數據的實時隨機讀寫。node
Hbase
與mysql
、oralce
、db2
、sqlserver
等關係型數據庫不一樣,它是一個NoSQL
數據庫(非關係型數據庫),而且有以下特色:mysql
Hbase
的表模型與關係型數據庫的表模型不一樣:Hbase
的表沒有固定的字段定義;Hbase
的表中每行存儲的都是一些key-value
對Hbase
的表中有列族的劃分,用戶能夠指定將哪些kv插入哪一個列族Hbase
的表在物理存儲上,是按照列族來分割的,不一樣列族的數據必定存儲在不一樣的文件中Hbase
的表中的每一行都固定有一個行鍵,並且每一行的行鍵在表中不能重複Hbase
中的數據,包含行鍵,包含key
,包含value
,都是byte[ ]
類型,hbase
不負責爲用戶維護數據類型Hbase
對事務的支持不好HBASE
相比於其餘nosql數據庫(mongodb
、redis
、cassendra
、hazelcast
)的特色:
由於Hbase
的表數據存儲在HDFS
文件系統中,因此存儲容量能夠線性擴展; 數據存儲的安全性可靠性極高!redis
rowkey:行鍵 | base_info | extra_info |
---|---|---|
001 | name:zs,age:22,sex:male | hobbiy:read,addr:beijing |
002 | name:laowang,sex:male |
hbase的表模型跟mysql之類的關係型數據庫的表模型差異巨大sql
hbase的表模型中有:行的概念;但沒有字段的概念mongodb
行中存的都是key-value對,每行中的key-value對中的key能夠是各類各樣的。shell
hbase表模型的要點數據庫
key-value
叫作一個cell
hbase會對插入的數據按順序存儲:apache
hbase的表數據類型:
hbase中只支持byte[] ,此處的byte[] 包括了: rowkey,key,value,列族名,表名。
表劃分爲不一樣的region。
[圖片上傳失敗...(image-ec30fc-1561887883664)]
Hbase分佈式系統包含兩個角色
Hbase
不作數據處理的話,不須要yarn
,yarn
是複製Mapreduce計算的,Hbase
只是負責數據管理
首先,要有一個HDFS
集羣,並正常運行; Hbase
的regionserver
應該跟hdfs
中的datanode
在一塊兒
其次,還須要一個zookeeper
集羣,並正常運行,因此安裝Hbase
要先安裝zookeeper
,zookeeper
前面已經安裝過了。
而後,安裝Hbase
各個節點角色分配以下:
節點 | 安裝的服務 |
---|---|
Master | namenode datanode regionserver hmaster zookeeper |
Slave01 | datanode regionserver zookeeper |
Slave02 | datanode regionserver zookeeper |
解壓hbase
安裝包 hbase-2.0.5-bin.tar.gz
修改hbase-env.sh
export JAVA_HOME=/usr/local/bigdata/java/jdk1.8.0_211 # 不啓動hbase自帶的zookeeper,咱們本身已經裝了 export HBASE_MANAGES_ZK=false
修改hbase-site.xml
<configuration> <!-- 指定hbase在HDFS上存儲的路徑 --> <property> <name>hbase.rootdir</name> <value>hdfs://Master:9000/hbase</value> </property> <!-- 指定hbase是分佈式的 --> <property> <name>hbase.cluster.distributed</name> <value>true</value> </property> <!-- 指定zk的地址,多個用「,」分割 --> <property> <name>hbase.zookeeper.quorum</name> <value>Master:2181,Slave01:2181,Slave02:2181</value> </property> </configuration>
修改 regionservers
Master Slave01 Slave02
修改完成後,將安裝文件夾放到三個節點的/usr/local/bigdata/
目錄下
先檢查hdfs
和zookeeper
是否正常啓動,
Master:
hadoop@Master:~$ jps 4918 DataNode 2744 QuorumPeerMain 4748 NameNode 9949 Jps 5167 SecondaryNameNode hadoop@Master:~$ /usr/local/bigdata/zookeeper-3.4.6/bin/zkServer.sh status JMX enabled by default Using config: /usr/local/bigdata/zookeeper-3.4.6/bin/../conf/zoo.cfg Mode: follower
Slave01:
hadoop@Slave1:~$ jps 3235 QuorumPeerMain 3779 DataNode 5546 Jps hadoop@Slave1:~$ /usr/local/bigdata/zookeeper-3.4.6/bin/zkServer.sh status JMX enabled by default Using config: /usr/local/bigdata/zookeeper-3.4.6/bin/../conf/zoo.cfg Mode: leader
Slave02:
hadoop@Slave2:~$ jps 11958 DataNode 13656 Jps 11390 QuorumPeerMain hadoop@Slave2:~$ /usr/local/bigdata/zookeeper-3.4.6/bin/zkServer.sh status JMX enabled by default Using config: /usr/local/bigdata/zookeeper-3.4.6/bin/../conf/zoo.cfg Mode: follower
而後執行start-hbase.sh
$ bin/start-hbase.sh
上面的命令會啓動配置文件regionserver
裏添加的全部機器,若是想手動啓動其中一臺能夠用:
$ bin/hbase-daemon.sh start regionserver
啓動完成後在Master上會啓動HRegionServer
和HMaster
兩個服務,Slave01
和Slave02
會啓動HMaster
服務。
高可用Hbase
集羣應配置兩臺master
一臺處於active
狀態一臺處於standby
狀態,用於監聽regionserver
能夠再從另外兩條機器中再啓動一個HRegionServer
服務。
$ bin/hbase-daemon.sh start master
新啓的這個master會處於backup狀態
使用命令hbase shell
bin/hbase shell Hbase> list // 查看錶 Hbase> status // 查看集羣狀態 Hbase> version // 查看集羣版本
ERROR: org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not running yet at org.apache.hadoop.hbase.master.HMaster.checkServiceStarted(HMaster.java:2932) at org.apache.hadoop.hbase.master.MasterRpcServices.isMasterRunning(MasterRpcServices.java:1084) at org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos$MasterService$2.callBlockingMethod(MasterProtos.java) at org.apache.hadoop.hbase.ipc.RpcServer.call(RpcServer.java:413) at org.apache.hadoop.hbase.ipc.CallRunner.run(CallRunner.java:130) at org.apache.hadoop.hbase.ipc.RpcExecutor$Handler.run(RpcExecutor.java:324) at org.apache.hadoop.hbase.ipc.RpcExecutor$Handler.run(RpcExecutor.java:304)
$ hdfs dfsadmin -safemode leave
create 't_user_info','base_info','extra_info' 表名 列族名 列族名
hbase(main):011:0> put 't_user_info','001','base_info:username','zhangsan' 0 row(s) in 0.2420 seconds hbase(main):012:0> put 't_user_info','001','base_info:age','18' 0 row(s) in 0.0140 seconds hbase(main):013:0> put 't_user_info','001','base_info:sex','female' 0 row(s) in 0.0070 seconds hbase(main):014:0> put 't_user_info','001','extra_info:career','it' 0 row(s) in 0.0090 seconds hbase(main):015:0> put 't_user_info','002','extra_info:career','actoress' 0 row(s) in 0.0090 seconds hbase(main):016:0> put 't_user_info','002','base_info:username','liuyifei' 0 row(s) in 0.0060 seconds
hbase(main):017:0> scan 't_user_info' ROW COLUMN+CELL 001 column=base_info:age, timestamp=1496567924507, value=18 001 column=base_info:sex, timestamp=1496567934669, value=female 001 column=base_info:username, timestamp=1496567889554, value=zhangsan 001 column=extra_info:career, timestamp=1496567963992, value=it 002 column=base_info:username, timestamp=1496568034187, value=liuyifei 002 column=extra_info:career, timestamp=1496568008631, value=actoress 2 row(s) in 0.0420 seconds
hbase(main):020:0> get 't_user_info','001' COLUMN CELL base_info:age timestamp=1496568160192, value=19 base_info:sex timestamp=1496567934669, value=female base_info:username timestamp=1496567889554, value=zhangsan extra_info:career timestamp=1496567963992, value=it 4 row(s) in 0.0770 seconds
hbase(main):021:0> delete 't_user_info','001','base_info:sex' 0 row(s) in 0.0390 seconds 刪除整行數據: hbase(main):024:0> deleteall 't_user_info','001' 0 row(s) in 0.0090 seconds hbase(main):025:0> get 't_user_info','001' COLUMN CELL 0 row(s) in 0.0110 seconds 3.4.1.6. 刪除整個表: hbase(main):028:0> disable 't_user_info' 0 row(s) in 2.3640 seconds hbase(main):029:0> drop 't_user_info' 0 row(s) in 1.2950 seconds hbase(main):030:0> list TABLE 0 row(s) in 0.0130 seconds => []
插入到hbase
中去的數據,hbase
會自動排序存儲:
排序規則: 首先看行鍵,而後看列族名,而後看列(key
)名; 按字典順序
Hbase的這個特性跟查詢效率有極大的關係
好比:一張用來存儲用戶信息的表,有名字,戶籍,年齡,職業....等信息
而後,在業務系統中常常須要:
查詢某個省的全部用戶
常常須要查詢某個省的指定姓的全部用戶
思路:若是能將相同省的用戶在hbase
的存儲文件中連續存儲,而且能將相同省中相同姓的用戶連續存儲,那麼,上述兩個查詢需求的效率就會提升!!!
作法:將查詢條件拼到rowkey
內
代碼流程:
Connection conn = ConnectionFactory.createConnection(conf);
adminAdmin admin = conn.getAdmin();
admin.createTable(HTableDescriptor descriptor);
@Before public void getConn() throws Exception{ // 構建一個鏈接對象 Configuration conf = HBaseConfiguration.create(); // 會自動加載hbase-site.xml conf.set("hbase.zookeeper.quorum", "192.168.233.200:2181,192.168.233.201:2181,192.168.233.202:2181"); conn = ConnectionFactory.createConnection(conf); } /** * DDL * @throws Exception */ @Test public void testCreateTable() throws Exception{ // 從鏈接中構造一個DDL操做器 Admin admin = conn.getAdmin(); // 建立一個表定義描述對象 HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("user_info")); // 建立列族定義描述對象 HColumnDescriptor hColumnDescriptor_1 = new HColumnDescriptor("base_info"); hColumnDescriptor_1.setMaxVersions(3); // 設置該列族中存儲數據的最大版本數,默認是1 HColumnDescriptor hColumnDescriptor_2 = new HColumnDescriptor("extra_info"); // 將列族定義信息對象放入表定義對象中 hTableDescriptor.addFamily(hColumnDescriptor_1); hTableDescriptor.addFamily(hColumnDescriptor_2); // 用ddl操做器對象:admin 來建表 admin.createTable(hTableDescriptor); // 關閉鏈接 admin.close(); conn.close(); } /** * 刪除表 * @throws Exception */ @Test public void testDropTable() throws Exception{ Admin admin = conn.getAdmin(); // 停用表 admin.disableTable(TableName.valueOf("user_info")); // 刪除表 admin.deleteTable(TableName.valueOf("user_info")); admin.close(); conn.close(); } // 修改表定義--添加一個列族 @Test public void testAlterTable() throws Exception{ Admin admin = conn.getAdmin(); // 取出舊的表定義信息 HTableDescriptor tableDescriptor = admin.getTableDescriptor(TableName.valueOf("user_info")); // 新構造一個列族定義 HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("other_info"); hColumnDescriptor.setBloomFilterType(BloomType.ROWCOL); // 設置該列族的布隆過濾器類型 // 將列族定義添加到表定義對象中 tableDescriptor.addFamily(hColumnDescriptor); // 將修改過的表定義交給admin去提交 admin.modifyTable(TableName.valueOf("user_info"), tableDescriptor); admin.close(); conn.close(); }
HBase
的增刪改查
Connection conn = null; @Before public void getConn() throws Exception{ // 構建一個鏈接對象 Configuration conf = HBaseConfiguration.create(); // 會自動加載hbase-site.xml conf.set("hbase.zookeeper.quorum", "Master:2181,Slave01:2181,Slave02:2181"); conn = ConnectionFactory.createConnection(conf); } /** * 增 * 改:put來覆蓋 * @throws Exception */ @Test public void testPut() throws Exception{ // 獲取一個操做指定表的table對象,進行DML操做 Table table = conn.getTable(TableName.valueOf("user_info")); // 構造要插入的數據爲一個Put類型(一個put對象只能對應一個rowkey)的對象 Put put = new Put(Bytes.toBytes("001")); put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("張三")); put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes("18")); put.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("addr"), Bytes.toBytes("北京")); Put put2 = new Put(Bytes.toBytes("002")); put2.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("李四")); put2.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes("28")); put2.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("addr"), Bytes.toBytes("上海")); ArrayList<Put> puts = new ArrayList<>(); puts.add(put); puts.add(put2); // 插進去 table.put(puts); table.close(); conn.close(); } /** * 循環插入大量數據 * @throws Exception */ @Test public void testManyPuts() throws Exception{ Table table = conn.getTable(TableName.valueOf("user_info")); ArrayList<Put> puts = new ArrayList<>(); for(int i=0;i<100000;i++){ Put put = new Put(Bytes.toBytes(""+i)); put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("張三"+i)); put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes((18+i)+"")); put.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("addr"), Bytes.toBytes("北京")); puts.add(put); } table.put(puts); } /** * 刪 * @throws Exception */ @Test public void testDelete() throws Exception{ Table table = conn.getTable(TableName.valueOf("user_info")); // 構造一個對象封裝要刪除的數據信息 Delete delete1 = new Delete(Bytes.toBytes("001")); Delete delete2 = new Delete(Bytes.toBytes("002")); delete2.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("addr")); ArrayList<Delete> dels = new ArrayList<>(); dels.add(delete1); dels.add(delete2); table.delete(dels); table.close(); conn.close(); } /** * 查 * @throws Exception */ @Test public void testGet() throws Exception{ Table table = conn.getTable(TableName.valueOf("user_info")); Get get = new Get("002".getBytes()); Result result = table.get(get); // 從結果中取用戶指定的某個key的value byte[] value = result.getValue("base_info".getBytes(), "age".getBytes()); System.out.println(new String(value)); System.out.println("-------------------------"); // 遍歷整行結果中的全部kv單元格 CellScanner cellScanner = result.cellScanner(); while(cellScanner.advance()){ Cell cell = cellScanner.current(); byte[] rowArray = cell.getRowArray(); //本kv所屬的行鍵的字節數組 byte[] familyArray = cell.getFamilyArray(); //列族名的字節數組 byte[] qualifierArray = cell.getQualifierArray(); //列名的字節數據 byte[] valueArray = cell.getValueArray(); // value的字節數組 System.out.println("行鍵: "+new String(rowArray,cell.getRowOffset(),cell.getRowLength())); System.out.println("列族名: "+new String(familyArray,cell.getFamilyOffset(),cell.getFamilyLength())); System.out.println("列名: "+new String(qualifierArray,cell.getQualifierOffset(),cell.getQualifierLength())); System.out.println("value: "+new String(valueArray,cell.getValueOffset(),cell.getValueLength())); } table.close(); conn.close(); } /** * 按行鍵範圍查詢數據 * @throws Exception */ @Test public void testScan() throws Exception{ Table table = conn.getTable(TableName.valueOf("user_info")); // 包含起始行鍵,不包含結束行鍵,可是若是真的想查詢出末尾的那個行鍵,那麼,能夠在末尾行鍵上拼接一個不可見的字節(\000) Scan scan = new Scan("10".getBytes(), "10000\001".getBytes()); ResultScanner scanner = table.getScanner(scan); Iterator<Result> iterator = scanner.iterator(); while(iterator.hasNext()){ Result result = iterator.next(); // 遍歷整行結果中的全部kv單元格 CellScanner cellScanner = result.cellScanner(); while(cellScanner.advance()){ Cell cell = cellScanner.current(); byte[] rowArray = cell.getRowArray(); //本kv所屬的行鍵的字節數組 byte[] familyArray = cell.getFamilyArray(); //列族名的字節數組 byte[] qualifierArray = cell.getQualifierArray(); //列名的字節數據 byte[] valueArray = cell.getValueArray(); // value的字節數組 System.out.println("行鍵: "+new String(rowArray,cell.getRowOffset(),cell.getRowLength())); System.out.println("列族名: "+new String(familyArray,cell.getFamilyOffset(),cell.getFamilyLength())); System.out.println("列名: "+new String(qualifierArray,cell.getQualifierOffset(),cell.getQualifierLength())); System.out.println("value: "+new String(valueArray,cell.getValueOffset(),cell.getValueLength())); } System.out.println("----------------------"); } } @Test public void test(){ String a = "000"; String b = "000\0"; System.out.println(a); System.out.println(b); byte[] bytes = a.getBytes(); byte[] bytes2 = b.getBytes(); System.out.println(""); }