Hbase 操作工具类

依赖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 操作工具类

.

猜你喜欢

转载自www.cnblogs.com/jonban/p/10805971.html
今日推荐