Hbase 操作工具類
阿新 • • 發佈:2019-05-03
fault 操作工具類 onf 。。 class utils address 讀取 ted
依賴jar
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.0.5</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase</artifactId> <version>2.0.5</version> <type>pom</type> </dependency>
HbaseUtils.java
package javax.utils; import java.io.IOException; import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; /** * Hbase 操作工具類 * *@author Logan * @createDate 2019-05-03 * @version 1.0.0 * */ public class HbaseUtils { /** * 根據表名獲取Table 對象 * * @param name 表名,必要時可指定命名空間,比如:“default:user” * @return Hbase Table 對象 * @throws IOException 有異常拋出,由調用者捕獲處理 */ public static Table getTable(String name) throws IOException { TableName tableName = TableName.valueOf(name); Connection connection = ConnectionFactory.createConnection(); return connection.getTable(tableName); } /** * 根據rowKey生成一個查詢的Get 對象 * * @param rowKey rowKey * @return Get 對象 */ public static Get createGet(String rowKey) { return new Get(Bytes.toBytes(rowKey)); } /** * 對查詢的Get 對象增加指定列簇 * * @param get * @param family */ public static void addFamilyOnGet(Get get, String family) { get.addFamily(Bytes.toBytes(family)); } /** * 對查詢的Get 對象增加指定列簇和列 * * @param get * @param family * @param qualifier */ public static void addColumnOnGet(Get get, String family, String qualifier) { get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier)); } /** * 根據表名和rowKey 查詢結果(包含全部列簇和列) * * @param tableName 表名,必要時可指定命名空間,比如:“default:user” * @param rowKey 查詢rowKey * @return 查詢結果Result * @throws IOException 有異常拋出,由調用者捕獲處理 */ public static Result get(String tableName, String rowKey) throws IOException { Get get = createGet(rowKey); return get(tableName, get); } /** * 根據表名和rowKey 查詢結果(包含Get 指定的列) * * @param tableName 表名,必要時可指定命名空間,比如:“default:user” * @param get Hbase查詢對象 * @return 查詢結果Result * @throws IOException 有異常拋出,由調用者捕獲處理 */ public static Result get(String tableName, Get get) throws IOException { Table table = getTable(tableName); return table.get(get); } }
持續更新中。。。
待續。。。
以下是測試類
HbaseClientDemo.java
package com.java.demo; import javax.utils.HbaseUtils; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; import org.junit.Test; /** * Hbase 客戶端測試 * * @author Logan * @createDate 2019-05-03 * @version 1.0.0 * */ public class HbaseClientDemo { @Test public void getAllFamily() { try { String tableName = "default:user"; String rowKey = "key1002"; // 按表名和rowKey查詢所有列 Result result = HbaseUtils.get(tableName, rowKey); Cell[] cells = result.rawCells(); // 從Result中讀取 rowKey System.out.println(Bytes.toString(result.getRow())); String print = "%s\t %s:%s \t %s"; for (Cell cell : cells) { // 從Cell中取rowKey String row = Bytes.toString(CellUtil.cloneRow(cell)); String family = Bytes.toString(CellUtil.cloneFamily(cell)); String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell)); String value = Bytes.toString(CellUtil.cloneValue(cell)); System.out.println(String.format(print, row, family, qualifier, value)); } } catch (Exception e) { e.printStackTrace(); } } @Test public void get() { try { String tableName = "default:user"; String rowKey = "key1002"; // 按表名和rowKey查詢指定列 Table table = HbaseUtils.getTable(tableName); Get get = HbaseUtils.createGet(rowKey); HbaseUtils.addColumnOnGet(get, "info", "name"); HbaseUtils.addColumnOnGet(get, "info", "age"); // 不存在的列,查詢結果不顯示 HbaseUtils.addColumnOnGet(get, "info", "address"); // 如果在增加列後增加已有的列簇,會返回該列簇的全部列數據,覆蓋前邊的增加列 // HbaseUtils.addFamilyOnGet(get, "info"); Result result = table.get(get); Cell[] cells = result.rawCells(); // 從Result中讀取 rowKey System.out.println(Bytes.toString(result.getRow())); String print = "%s\t %s:%s \t %s"; for (Cell cell : cells) { // 從Cell中取rowKey String row = Bytes.toString(CellUtil.cloneRow(cell)); String family = Bytes.toString(CellUtil.cloneFamily(cell)); String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell)); String value = Bytes.toString(CellUtil.cloneValue(cell)); System.out.println(String.format(print, row, family, qualifier, value)); } } catch (Exception e) { e.printStackTrace(); } } }
持續更新中。。。
待續。。。
Hbase 操作工具類
.
Hbase 操作工具類