依赖jar
HbaseUtils.java
package javax.utils;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;import org.apache.hadoop.hbase.Cell;
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.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.util.Bytes;/*** Hbase 操作工具类* * @author Logan* @version 1.0.0* @createDate 2019-05-03**/
public class HbaseUtils {// ===============Common=====================================/*** 根据表名获取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);}// =============== Put =====================================/*** 根据rowKey生成一个Put对象* * @param rowKey rowKey* @return Put对象*/public static Put createPut(String rowKey) {return new Put(Bytes.toBytes(rowKey));}/*** 在Put对象上增加Cell* * @param put Put对象* @param cell cell对象* @throws IOException 有异常抛出,由调用者捕获处理*/public static void addCellOnPut(Put put, Cell cell) throws IOException {put.add(cell);}/*** 在Put对象上增加值* * @param put Put对象* @param family 列簇* @param qualifier 列* @param value 字符串类型的值*/public static void addValueOnPut(Put put, String family, String qualifier, String value) {addValueOnPut(put, family, qualifier, Bytes.toBytes(value));}/*** 在Put对象上增加值* * @param put Put对象* @param family 列簇* @param qualifier 列* @param value 字节数组类型的值,可以是任意对象序列化而成*/public static void addValueOnPut(Put put, String family, String qualifier, byte[] value) {put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), value);}/*** 在Put对象上增加值* * @param put Put对象* @param family 列簇* @param qualifier 列* @param ts Timestamp时间戳* @param value 字符串类型的值*/public static void addValueOnPut(Put put, String family, String qualifier, long ts, String value) {addValueOnPut(put, family, qualifier, ts, Bytes.toBytes(value));}/*** 在Put对象上增加值* * @param put Put对象* @param family 列簇* @param qualifier 列* @param ts Timestamp时间戳* @param value 字节数组类型的值,可以是任意对象序列化而成*/public static void addValueOnPut(Put put, String family, String qualifier, long ts, byte[] value) {put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), ts, value);}/*** 按表名插入一个Put对象包含的数据* * @param tableName 表名,必要时可指定命名空间,比如:“default:user”* @param put 要插入的数据对象* @throws IOException 有异常抛出,由调用者捕获处理*/public static void put(String tableName, Put put) throws IOException {try (Table table = getTable(tableName);) {table.put(put);}}/*** 按表名批量插入Put对象包含的数据* * @param tableName 表名,必要时可指定命名空间,比如:“default:user”* @param puts 要插入的数据对象集合* @throws IOException 有异常抛出,由调用者捕获处理*/public static void put(String tableName, List
以下是测试类
HbaseClientDemo.java
package com.java.demo;import java.util.ArrayList;
import java.util.List;import javax.utils.HbaseUtils;import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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.util.Bytes;
import org.junit.Test;/*** Hbase 客户端测试* * @author Logan* @version 1.0.0* @createDate 2019-05-03**/
public class HbaseClientDemo {/*** 向user表中插入数据*/@Testpublic void put() {String tableName = "default:user";try {List
puts.add(put);put = HbaseUtils.createPut("key1006");HbaseUtils.addValueOnPut(put, "info", "name", "沙悟净");HbaseUtils.addValueOnPut(put, "info", "age", "1000");HbaseUtils.addValueOnPut(put, "info", "address", "流沙河");puts.add(put);HbaseUtils.put(tableName, puts);} catch (Exception e) {e.printStackTrace();}}/*** 按rowKey批量查询user表中全部列簇全部列的值*/@Testpublic void getAllFamily() {try {String tableName = "default:user";String[] rowKeys = { "key1001", "key1002", "key1003", "key1005", "key1006" };// 按表名和rowKey查询所有列Result[] results = HbaseUtils.get(tableName, rowKeys);for (Result result : results) {// 打印查询结果
printResult(result);}} catch (Exception e) {e.printStackTrace();}}/*** 按rowKey查询user表中指定列簇指定列的值*/@Testpublic void get() {try {String tableName = "default:user";String rowKey = "key1002";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 = HbaseUtils.get(tableName, get);printResult(result);} catch (Exception e) {e.printStackTrace();}}@Testpublic void scan() {try {String tableName = "default:user";String startRow = "key1001";String stopRow = "key1006";ResultScanner resultScanner = HbaseUtils.scan(tableName, startRow, stopRow);for (Result result : resultScanner) {printResult(result);}} catch (Exception e) {e.printStackTrace();}}/*** 打印查询结果* * @param result 查询结果对象*/private void printResult(Result result) {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中取rowKeyString 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));}}/*** 删除指定列*/@Testpublic void deleteColumn() {try {String tableName = "default:user";List
deletes.add(delete);delete = HbaseUtils.createDelete("key1006");HbaseUtils.addColumnOnDelete(delete, "info", "address");deletes.add(delete);HbaseUtils.delete(tableName, deletes);} catch (Exception e) {e.printStackTrace();}}/*** 删除指定列簇*/@Testpublic void deleteFamily() {try {String tableName = "default:user";List
deletes.add(delete);delete = HbaseUtils.createDelete("key1006");HbaseUtils.addFamilyOnDelete(delete, "info");deletes.add(delete);HbaseUtils.delete(tableName, deletes);} catch (Exception e) {e.printStackTrace();}}}
Hbase 操作工具类
.