Hbase 简单封装(Hbase 2.0+ API)

前言

封装了一些常用的方法

  • 添加一行数据
  • 创建表(单列族)
  • 创建表(多列族)
  • 删除表
  • 判断表是否存在
  • 获取一行数据(根据rowkey)
  • 获取某个列族某个列的某行数据
  • 打印出result(方便展示数据)

工具类

类代码:

package com.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class HbaseUtil {

    private static ThreadLocal<Connection> connHolder= new ThreadLocal<Connection>();


    /**
     * 创建connection
     * @throws IOException
     */
    public static void makeHbaseConnection() throws IOException {
        Connection connection = connHolder.get();
        if (connection == null){
            Configuration conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum","hadoop100,hadoop101,hadoop102");
            conf.set("hbase.zookeeper.property.clientPort", "2181");
            connection = ConnectionFactory.createConnection(conf);
            connHolder.set(connection);
        }
    }

    /**
     * 关闭连接
     * @throws IOException
     */
    public static void closeHbseConn() throws IOException {
        Connection connection = connHolder.get();
        if (connection != null){
            connection.close();
            connHolder.remove();
        }
    }

    /**
     * 添加一行数据
     * @param tableName 表名
     * @param rowKey 行号
     * @param family 列族
     * @param column 列名
     * @param value
     * @throws IOException
     */
    public static void insertData(String tableName,String rowKey,String family,String column,String value) throws IOException {
        //获取连接
        Connection connection = connHolder.get();
        //获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));
        //获取添加对象
        Put put = new Put(Bytes.toBytes(rowKey));
        //添加一列
        put.addColumn(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value));
        //添加
        table.put(put);
        //关闭
        table.close();

    }

    /**
     * 创建表
     * @param tableName
     * @param family
     * @throws IOException
     */
    public static void createTable(String tableName,String family) throws IOException {
        Connection connection = connHolder.get();
        //获取admin
        Admin admin = connection.getAdmin();

        //列族描述对象建造者
        ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(family));
        //设置最大版本号
        columnFamilyDescriptorBuilder.setMaxVersions(3);
        //列族描述对象
        ColumnFamilyDescriptor columnFamilyDescriptor = columnFamilyDescriptorBuilder.build();

        //表描述对象建造者
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
        //将列族对象添加进表描述
        tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
        //创建表描述对象
        TableDescriptor tableDescriptor = tableDescriptorBuilder.build();

        //创建表
        admin.createTable(tableDescriptor);
        admin.close();
    }


    /**
     * 创建表(多列族)
     * @param tableName
     * @param familys
     * @throws IOException
     */
    public static void createTable(String tableName,String[] familys) throws IOException {
        Connection connection = connHolder.get();
        //获取admin
        Admin admin = connection.getAdmin();
        //表描述对象建造者
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));

        for (String family : familys) {
            //列族描述对象建造者
            ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(family));
            //设置最大版本号
            columnFamilyDescriptorBuilder.setMaxVersions(3);
            //将列族对象添加进表描述
            tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
        }

        //创建表描述对象
        TableDescriptor tableDescriptor = tableDescriptorBuilder.build();

        //创建表
        admin.createTable(tableDescriptor);
        admin.close();
    }

    /**
     * 根据行号查询数据
     * @param tableName
     * @param rowKey
     * @return
     * @throws IOException
     */
    public static Result selectDataByRowkey(String tableName,String rowKey) throws IOException {
        Connection connection = connHolder.get();
        //获取表
        Table table = connection.getTable(TableName.valueOf(tableName));
        //获取表描述对象
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = table.get(get);
        return result;
    }

    /**
     * 获取某一列族中某一列的某一行数据
     * @param tableName
     * @param rowKey
     * @param family
     * @param column
     * @return
     * @throws IOException
     */
    public static Result selectDataByCol(String tableName,String rowKey,String family,String column) throws IOException {
        Connection connection = connHolder.get();
        //获取表
        Table table = connection.getTable(TableName.valueOf(tableName));
        //获取表描述对象
        Get get = new Get(Bytes.toBytes(rowKey));
        get.addColumn(Bytes.toBytes(family),Bytes.toBytes(column));
        Result result = table.get(get);
        return result;

    }





    /**
     * 打印result
     * @param result
     */
    public static void showResult(Result result){
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            System.out.println("family:" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("row:" + Bytes.toString(CellUtil.cloneRow(cell)));
            System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
        }
    }

    /**
     * 删除表
     * @param tableName
     * @throws IOException
     */
    public static void deleteTable(String tableName) throws IOException {
        Connection connection = connHolder.get();
        Admin admin = connection.getAdmin();
        TableName name = TableName.valueOf(tableName);
        if (admin.tableExists(name)){
            admin.deleteTable(name);
        }
    }

    /**
     * 是否存在表
     * @param tableName
     * @return
     * @throws IOException
     */
    public static boolean tableExists(String tableName) throws IOException {
        Connection connection = connHolder.get();
        Admin admin = connection.getAdmin();
        return  admin.tableExists(TableName.valueOf(tableName));
    }


}

原文地址:https://www.cnblogs.com/wuren-best/p/13830456.html