Hbase工具类

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37294838/article/details/89099979
package com.hbase.test.util;

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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Hbase工具类
 */
public class HBaseUtil {
    //本地线程
    private static ThreadLocal<Connection> connect = new ThreadLocal<Connection>();

    /**
     * 产生connect
     *
     * @throws Exception
     */
    public static void makeHBaseConnection() throws Exception {
        //获取本地连接
        Connection connection = connect.get();
        //连接是否为空
        if (connection == null) {
            //为空就进行创建 
            //获取配置信息
            Configuration conf = HBaseConfiguration.create();
            //创建连接
            connection = ConnectionFactory.createConnection(conf);
            //设置到本地线程中
            connect.set(connection);
        }
    }

    /**
     * 关闭连接
     *
     * @throws IOException
     */
    public static void close() throws IOException {
        Connection connection = connect.get();
        if (connection != null) {
            //进行关闭连接
            connection.close();
            //从本地线程中删除
            connect.remove();
        }
    }

    /**
     * 添加数据
     * @param tableName 表名
     * @param rowKey rowkey
     * @param family 列族
     * @param qualifier 列名
     * @param value 值
     * @throws IOException
     */
    public static void insertData(String tableName, String rowKey,String family, String qualifier, String value) throws IOException {
        //获取连接
        Connection connection = connect.get();
        //获取表
        Table table = connection.getTable(TableName.valueOf(tableName));
        //设置rowkey
        Put put = new Put(Bytes.toBytes(rowKey));
        //添加列族、列名、列值
        put.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value));
        //进行添加数据
        table.put(put);
        //关闭
        table.close();
    }

    /**
     * 添加表
     * @param tableName 表名
     */
    public static void addTable(String tableName,String famliy) throws IOException {
        //获取连接
        Connection connection = connect.get();

        //获取操作对象
        Admin admin = connection.getAdmin();
        //创建表
        HTableDescriptor desc=new HTableDescriptor(TableName.valueOf(tableName));
        //添加列族
        HColumnDescriptor d=new HColumnDescriptor(famliy);
        desc.addFamily(d);
        //创建表
        admin.createTable(desc);
    }

    /**
     * 判断表是否存在
     * @param tableName
     * @return
     */
    public static boolean tableExits(String tableName) throws IOException {
        //获取连接
        Connection connection = connect.get();
        //获取操作者admin
        Admin admin = connection.getAdmin();
        //判断表是否存在
        return admin.tableExists(TableName.valueOf(tableName));
    }

    /**
     * 删除表
     * @param tableName
     * @throws IOException
     */
    public static void deleteTable(String tableName) throws IOException {
        //获取连接
        Connection connection = connect.get();
        //获取操作者admin
        Admin admin = connection.getAdmin();
        //先禁用表
        admin.disableTable(TableName.valueOf(tableName));
        //删除表
        admin.deleteTable(TableName.valueOf(tableName));
    }

    /**
     *删除多条记录
     * @param tableName 表名
     * @param rowKeys rowkey
     */
    public static void deleteListRecord(String tableName,String... rowKeys) throws IOException {
        //获取连接
        Connection connection = connect.get();
        //获取表
        Table table=connection.getTable(TableName.valueOf(tableName));
        //删除记录的集合
        List<Delete> deleteList=new ArrayList<Delete>();
        //遍历rowkey
        for (String rowKey : rowKeys) {
            //向集合中添加要删除的记录
            Delete delete=new Delete(Bytes.toBytes(rowKey));
            deleteList.add(delete);
        }
        //进行批量删除
        table.delete(deleteList);
        //关闭表
        table.close();
    }

    /**
     *删除单条记录
     * @param tableName 表名
     * @param rowKey rowkey
     */
    public static void deleteRecored(String tableName,String rowKey) throws IOException {
        //获取连接
        Connection connection = connect.get();
        //获取表
        Table table = connection.getTable(TableName.valueOf(tableName));
        //删除表操作
        Delete d=new Delete(Bytes.toBytes(rowKey));
        table.delete(d);
        //关闭表
        table.close();
    }

    /**
     * 获取多条表记录
     * @param tableName
     */
    public static void tableAll(String tableName) throws IOException {
        //获取连接
        Connection connection = connect.get();
        //获取表
        Table table = connection.getTable(TableName.valueOf(tableName));
        //查询所有记录
        Scan s=new Scan();
        ResultScanner scanner = table.getScanner(s);
        //遍历结果
        for (Result result : scanner) {
            //获取每一个单元格
            Cell[] cells = result.rawCells();
            //进行遍历单元格
            for (Cell cell : cells) {
                System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
                System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
    }

    /**
     * 获取单条表记录(根据rowkey)
     * @param tableName
     */
    public static void tableSingleRecore(String tableName,String rowKey) throws IOException {
        //获取连接
        Connection connection = connect.get();
        //获取表
        Table table = connection.getTable(TableName.valueOf(tableName));
        //进行单条记录的查询
        Get get=new Get(Bytes.toBytes(rowKey));
        //获取结果对象
        Result result = table.get(get);
        //获取结果记录的所有单元格
        Cell[] cells = result.rawCells();
        //进行遍历单元格
        for (Cell cell : cells) {
            //对记录进行打印
            System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
            System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
        }
    }

    /**
     *获取某一行指定“列族:列”的数据
     * @param tableName
     * @param famliy
     * @param qualifier
     */
    public static void getColumnRecord(String tableName, String rowKey,String famliy,String qualifier) throws IOException {
        //获取连接
        Connection connection = connect.get();
        //获取表
        Table table = connection.getTable(TableName.valueOf(tableName));
        //进行指定“列族:列”的数据查询
        Get get=new Get(Bytes.toBytes(rowKey));
        get.addColumn(Bytes.toBytes(famliy),Bytes.toBytes(qualifier));
        //结果对象
        Result result = table.get(get);
        //获取所有单元格的数据
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
            System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
        }
    }
}

 /**
     * 创建命名空间
     * @param nameSpaceName
     */
    public static void nameSpace(String nameSpaceName) throws IOException {
        //获取连接
        Connection connection = connect.get();
        //获取操作对象
        Admin admin = connection.getAdmin();
        //创建命名空间
        NamespaceDescriptor descriptor=NamespaceDescriptor.create(nameSpaceName).build();
        admin.createNamespace(descriptor);
    }



调用方式:

public class TestHbaseAPI {

    public static void main(String[] args) throws Exception {

        // 创建连接
        HBaseUtil.makeHBaseConnection();

        // 增加数据
        HBaseUtil.insertData("test:student", "1002","info", "name", "lisi");

        // 关闭连接
        HBaseUtil.close();

    }
}

猜你喜欢

转载自blog.csdn.net/m0_37294838/article/details/89099979