IDEA+Hadoop2.10.1+Zookeeper3.4.10+Hbase 2.3.5 操作JavaAPI
阿新 • • 發佈:2021-07-13
在此之前要配置好三節點的hadoop叢集,zookeeper叢集,並啟動它們,然後再配置好HBase環境
本文只是HBase2.3.5API操作作相應說明,如果前面環境還沒有配置好,可以翻看我之前的部落格,歡迎留言交流
節點hadoop01
節點hadoop02
節點hadoop03
1 maven依賴
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>2.3.5</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.3.5</version> </dependency> <dependency> <groupId>jdk.tools</groupId> <artifactId>jdk.tools</artifactId> <version>1.8</version> <scope>system</scope> <systemPath>C:/Program Files/Java/jdk1.8.0_261/lib/tools.jar</systemPath> </dependency>
2 API操作
package com.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import org.stringtemplate.v4.ST; import scala.util.control.Exception; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class ConfigulationHBase { private static Configuration configuration; private static Connection connection; private static Admin admin; static { configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum", "192.168.161.141"); configuration.set("hbase.zookeeper.property.clientPort", "2181"); try { connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); } catch (IOException e) { e.printStackTrace(); } } /** * 判斷表是否存在 * <br>存在則返回true * @param tableName * @return * @throws IOException */ public static boolean isTableExist(String tableName) throws IOException { boolean b = admin.tableExists(TableName.valueOf(tableName)); return b; } /** * 建立表 * * 引數tableName為表的名稱,字串陣列fields為儲存記錄各個域名稱的陣列。<br> * 要求當HBase已經存在名為tableName的表時,先刪除原有的表,然後再<br> * 建立新的表 field:列族<br> * @param tableName 表名 * @param fields 列族名 * @throws IOException */ public static void createTable(String tableName,String[] fields) throws IOException { if(isTableExist(tableName)){ System.out.println(tableName + " table is alreadly exist..."); admin.disableTable(TableName.valueOf(tableName)); admin.deleteTable(TableName.valueOf(tableName)); System.out.println(tableName + " table is deleted..."); } HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName)); for(String str:fields){ HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str); hTableDescriptor.addFamily(hColumnDescriptor); } admin.createTable(hTableDescriptor); System.out.println(tableName + " table is created!"); admin.close(); } /** * 新增資料 * * 向表tableName,行鍵rowKey和fields欄位指定的單元格中新增對應的值values<br> * 例如:表名:student,行鍵:1001,新增的欄位:info:name,新增的值:Janna<br> * put 'student','1001','info:name','Janna'<br> * * @param tableName 表名 * @param rowKey 行鍵 * @param family 列族 * @param qualifier 列族值 * @param value 值 * @throws IOException */ public static void addRow(String tableName, String rowKey, String family, String qualifier, String value) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); byte[] rowKeyAsBytes = rowKey.getBytes(); Put put = new Put(rowKeyAsBytes); put.addColumn(family.getBytes(), qualifier.getBytes(), value.getBytes()); table.put(put); table.close(); admin.close(); } /** * 刪除一行或者多行資料 * @param tableName * @param rows * @throws IOException */ public static void deleteMultiRow(String tableName, String... rows) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); List<Delete> deleteList = new ArrayList<Delete>(); for (int i = 0; i < rows.length; i++) { Delete delete = new Delete(rows[i].getBytes()); deleteList.add(delete); } table.delete(deleteList); table.close(); admin.close(); } /** * * 查詢某tableName所有的資料 * @param tableName 表名 * @throws IOException */ public static void getAllRows(String tableName) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); for (Result result:scanner){ Cell[] cells = result.rawCells(); for (Cell cell:cells){ System.out.println("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell))); System.out.println("Famliy:" + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell))); System.out.println("--------------------------------------"); } System.out.println("======================================="); } table.close(); } /** * 查詢tableName表的rowKey行鍵的資料 * get 'student','1001' * @param tableName 表名 * @param rowKey 行鍵 */ public static void getRow(String tableName, String rowKey) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Get get = new Get(rowKey.getBytes()); Result result = table.get(get); for (Cell cell:result.rawCells()){ System.out.println("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell))); System.out.println("Famliy:" + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell))); System.out.println("--------------------------------------"); } } /** * 獲取某表某行鍵某列族的值 * * @param tableName 表名 * @param rowKey 行鍵 * @param famliy 列族 * @param qualifier 列族值 * @throws IOException */ public static void getRowQualifier(String tableName, String rowKey, String famliy, String qualifier) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Get get = new Get(rowKey.getBytes()); get.addColumn(famliy.getBytes(), qualifier.getBytes()); Result result = table.get(get); for (Cell cell:result.rawCells()){ System.out.println("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell))); System.out.println("Famliy:" + Bytes.toString(CellUtil.cloneFamily(cell))); System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell))); System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell))); System.out.println("timestamp:" + cell.getTimestamp()); System.out.println("--------------------------------------"); } } /** * main 程式入口 * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // 建立表 // String[] fileds = {"base_info","other_info"}; // createTable("football", fileds); // 新增資料 // addRow("football", "11002", "base_info", "name", "lin"); // addRow("football", "11002", "base_info", "sex", "female"); // addRow("football", "11003", "base_info", "sex", "male"); // 刪除資料 // deleteMultiRow("football", new String[]{"11002"}); // 得到所有的資料 // getAllRows("student"); // 查詢某表某行鍵資料 // getRow("student","1001"); // 查詢某表某行鍵某列族資料 getRowQualifier("student", "1001", "info", "name"); } }
3 部分結果截圖
僅供參考,有錯誤還請指出!
有什麼想法,評論區留言,互相指教指教。
藍天和白雲是標配。