hbase java

<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>

hbase java是什麼,讓我們一起了解一下?

HBase是一個分佈式的、面向列的開源數據庫,具有高可靠性、高性能、面向列、可伸縮的分佈式存儲系統,利用HBase技術可在廉價PC Server上搭建起大規模結構化存儲集羣。

如何使用JAVA語言操作Hbase、整合Hbase?

可分為五步驟:

步驟1:新創建一個Java Project 。

步驟2:導入JAR包,在工程根目錄下新建一個“lib”文件夾,將官方文檔中的lib目錄下的jar全部導入。

步驟3:修改開發機的hosts文件,在文件莫為增加一行虛擬機IP的映射信息。

步驟4:修改虛擬機的配置文件,修改虛擬機的設備名稱,名稱需要與之前兩個配置文件的映射名稱一致。

步驟5:實現查詢、新建、刪除等。

hbase java

案例代碼展示如下:

package hbase;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.Cell;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.Admin;import org.apache.hadoop.hbase.client.Connection;import org.apache.hadoop.hbase.client.ConnectionFactory;import org.apache.hadoop.hbase.client.Delete;import org.apache.hadoop.hbase.client.Get;import org.apache.hadoop.hbase.client.Put;import org.apache.hadoop.hbase.client.Result;import org.apache.hadoop.hbase.client.ResultScanner;import org.apache.hadoop.hbase.client.Scan;import org.apache.hadoop.hbase.client.Table;import org.apache.hadoop.hbase.exceptions.DeserializationException;import org.apache.hadoop.hbase.filter.Filter;import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;import org.apache.hadoop.hbase.util.Bytes;import org.junit.Before;import org.junit.Test;public class HBaseDemo {// 與HBase數據庫的連接對象Connection connection;// 數據庫元數據操作對象Admin admin;@Beforepublic void setUp() throws Exception {// 取得一個數據庫連接的配置參數對象Configuration conf = HBaseConfiguration.create();// 設置連接參數:HBase數據庫所在的主機IPconf.set("hbase.zookeeper.quorum", "192.168.137.13");// 設置連接參數:HBase數據庫使用的端口conf.set("hbase.zookeeper.property.clientPort", "2181");// 取得一個數據庫連接對象connection = ConnectionFactory.createConnection(conf);// 取得一個數據庫元數據操作對象admin = connection.getAdmin();}/**    * 創建表    */public void createTable() throws IOException{System.out.println("---------------創建表 START-----------------");// 數據表表名String tableNameString = "t_book";// 新建一個數據表表名對象TableName tableName = TableName.valueOf(tableNameString);// 如果需要新建的表已經存在if(admin.tableExists(tableName)){System.out.println("表已經存在!");}// 如果需要新建的表不存在else{// 數據表描述對象HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);// 列族描述對象HColumnDescriptor family= new HColumnDescriptor("base");;// 在數據表中新建一個列族hTableDescriptor.addFamily(family);// 新建數據表admin.createTable(hTableDescriptor);}System.out.println("---------------創建表 END-----------------");}/**    * 查詢整表數據    */@Testpublic void queryTable() throws IOException{System.out.println("---------------查詢整表數據 START-----------------");// 取得數據表對象Table table = connection.getTable(TableName.valueOf("t_book"));// 取得表中所有數據ResultScanner scanner = table.getScanner(new Scan());// 循環輸出表中的數據for (Result result : scanner) {byte[] row = result.getRow();System.out.println("row key is:" + new String(row));List<Cell> listCells = result.listCells();for (Cell cell : listCells) {byte[] familyArray = cell.getFamilyArray();byte[] qualifierArray = cell.getQualifierArray();byte[] valueArray = cell.getValueArray();System.out.println("row value is:" + new String(familyArray) + new String(qualifierArray)+ new String(valueArray));}}System.out.println("---------------查詢整表數據 END-----------------");}/**    * 按行鍵查詢表數據    */@Testpublic void queryTableByRowKey() throws IOException{System.out.println("---------------按行鍵查詢表數據 START-----------------");// 取得數據表對象Table table = connection.getTable(TableName.valueOf("t_book"));// 新建一個查詢對象作為查詢條件Get get = new Get("row8".getBytes());// 按行鍵查詢數據Result result = table.get(get);byte[] row = result.getRow();System.out.println("row key is:" + new String(row));List<Cell> listCells = result.listCells();for (Cell cell : listCells) {byte[] familyArray = cell.getFamilyArray();byte[] qualifierArray = cell.getQualifierArray();byte[] valueArray = cell.getValueArray();System.out.println("row value is:" + new String(familyArray) + new String(qualifierArray)+ new String(valueArray));}System.out.println("---------------按行鍵查詢表數據 END-----------------");}/**    * 按條件查詢表數據    */@Testpublic void queryTableByCondition() throws IOException{System.out.println("---------------按條件查詢表數據 START-----------------");// 取得數據表對象Table table = connection.getTable(TableName.valueOf("t_book"));// 創建一個查詢過濾器Filter filter = new SingleColumnValueFilter(Bytes.toBytes("base"), Bytes.toBytes("name"),CompareOp.EQUAL, Bytes.toBytes("bookName6"));// 創建一個數據表掃描器Scan scan = new Scan();// 將查詢過濾器加入到數據表掃描器對象scan.setFilter(filter);// 執行查詢操作,並取得查詢結果ResultScanner scanner = table.getScanner(scan);// 循環輸出查詢結果for (Result result : scanner) {byte[] row = result.getRow();System.out.println("row key is:" + new String(row));List<Cell> listCells = result.listCells();for (Cell cell : listCells) {byte[] familyArray = cell.getFamilyArray();byte[] qualifierArray = cell.getQualifierArray();byte[] valueArray = cell.getValueArray();System.out.println("row value is:" + new String(familyArray) + new String(qualifierArray)+ new String(valueArray));}}System.out.println("---------------按條件查詢表數據 END-----------------");}/**    * 清空表    */@Testpublic void truncateTable() throws IOException{System.out.println("---------------清空表 START-----------------");// 取得目標數據表的表名對象TableName tableName = TableName.valueOf("t_book");// 設置表狀態為無效admin.disableTable(tableName);// 清空指定表的數據admin.truncateTable(tableName, true);System.out.println("---------------清空表 End-----------------");}/**    * 刪除表    */@Testpublic void deleteTable() throws IOException{System.out.println("---------------刪除表 START-----------------");// 設置表狀態為無效admin.disableTable(TableName.valueOf("t_book"));// 刪除指定的數據表admin.deleteTable(TableName.valueOf("t_book"));System.out.println("---------------刪除表 End-----------------");}/**    * 刪除行    */@Testpublic void deleteByRowKey() throws IOException{System.out.println("---------------刪除行 START-----------------");// 取得待操作的數據表對象Table table = connection.getTable(TableName.valueOf("t_book"));// 創建刪除條件對象Delete delete = new Delete(Bytes.toBytes("row2"));// 執行刪除操作table.delete(delete);System.out.println("---------------刪除行 End-----------------");}/**    * 刪除行(按條件)    */@Testpublic void deleteByCondition() throws IOException, DeserializationException{System.out.println("---------------刪除行(按條件) START-----------------");// 步驟1:調用queryTableByCondition()方法取得需要刪除的數據列表// 步驟2:循環步驟1的查詢結果,對每個結果調用deleteByRowKey()方法System.out.println("---------------刪除行(按條件) End-----------------");}/**    * 新建列族    */@Testpublic void addColumnFamily() throws IOException{System.out.println("---------------新建列族 START-----------------");// 取得目標數據表的表名對象TableName tableName = TableName.valueOf("t_book");// 創建列族對象HColumnDescriptor columnDescriptor = new HColumnDescriptor("more");// 將新創建的列族添加到指定的數據表admin.addColumn(tableName, columnDescriptor);System.out.println("---------------新建列族 END-----------------");}/**    * 刪除列族    */@Testpublic void deleteColumnFamily() throws IOException{System.out.println("---------------刪除列族 START-----------------");// 取得目標數據表的表名對象TableName tableName = TableName.valueOf("t_book");// 刪除指定數據表中的指定列族admin.deleteColumn(tableName, "more".getBytes());System.out.println("---------------刪除列族 END-----------------");}/**    * 插入數據    */@Testpublic void insert() throws IOException{System.out.println("---------------插入數據 START-----------------");// 取得一個數據表對象Table table = connection.getTable(TableName.valueOf("t_book"));// 需要插入數據庫的數據集合List<Put> putList = new ArrayList<Put>();Put put;// 生成數據集合for(int i = 0; i < 10; i++){put = new Put(Bytes.toBytes("row" + i));put.addColumn(Bytes.toBytes("base"), Bytes.toBytes("name"), Bytes.toBytes("bookName" + i));putList.add(put);}// 將數據集合插入到數據庫table.put(putList);System.out.println("---------------插入數據 END-----------------");}}