java操作HBase2.x增删改查

直接上代码

package com.imooc.spark.project.utils;

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;

/**
 * HBase操作工具类:Java工具类建议采用单例模式封装
 * 启动hbase
 * list查看表
 * Hbase表的设计 创建时不需要指定列
 *     先创建表
 *         create 'imooc_course_clickcount','info'
 *         abbac ab
 *
 *         7.5
 */
public class HBaseUtils {
    HBaseAdmin admin = null;
    Configuration configuration = null;
    /**
     * 私有改造方法
     * hbase安装目录conf下hbase-site.xml的配置信息
     */
    private HBaseUtils(){
        configuration = new Configuration();
        configuration.set("hbase.zookeeper.quorum", "yourip:2181");
        configuration.set("hbase.rootdir", "hdfs://yourip/hbase");

        try {
            admin = new HBaseAdmin(configuration);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static HBaseUtils instance = null;

    public  static synchronized HBaseUtils getInstance() {
        if(null == instance) {
            instance = new HBaseUtils();
        }
        return instance;
    }
    /**
     * 根据表名获取到HTable实例
     */
    public HTable getTable(String tableName) {
        HTable table = null;
        try {
            table = new HTable(configuration, tableName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return table;
    }

    /**
     * 创建表
     * @param tableNmae
     * @param cols
     * @throws IOException
     */
    public void createTable(String tableNmae, String[] cols) throws IOException {
        TableName tableName = TableName.valueOf(tableNmae);
        if (admin.tableExists(tableName)) {
            System.out.println("talbe is exists!");
        } else {
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
            for (String col : cols) {
                HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col);
                hTableDescriptor.addFamily(hColumnDescriptor);
            }
            admin.createTable(hTableDescriptor);
        }
        close();
    }

    /**
     * 删除表
     * @param tableName
     * @throws IOException
     */
    public void deleteTable(String tableName) throws IOException {
        TableName tn = TableName.valueOf(tableName);
        if (admin.tableExists(tn)) {
            admin.disableTable(tn);
            admin.deleteTable(tn);
        }
        close();
    }

    /**
     * 获取所有表
     * @throws IOException
     */
    public void listTables() throws IOException {
        HTableDescriptor hTableDescriptors[] = admin.listTables();
        for (HTableDescriptor hTableDescriptor : hTableDescriptors) {
            System.out.println(hTableDescriptor.getNameAsString());
        }
        close();
    }
    /**
     * 添加一条记录到HBase表
     * @param tableName HBase表名
     * @param rowkey  HBase表的rowkey
     * @param cf HBase表的columnfamily
     * @param column HBase表的列
     * @param value  写入HBase表的值
     */
    public void put(String tableName, String rowkey, String cf, String column, String value) {
        HTable table = getTable(tableName);
        Put put = new Put(Bytes.toBytes(rowkey));

        put.add(Bytes.toBytes(cf), Bytes.toBytes(column), Bytes.toBytes(value));
        try {
            table.put(put);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取指定内容
     * @param tableName
     * @param rowkey
     * @throws IOException
     */
    public void get(String tableName, String rowkey) throws IOException {
        Table table = getTable(tableName);
        Get get = new Get(Bytes.toBytes(rowkey));
        // 获取指定列族数据
        // get.addFamily(Bytes.toBytes(colFamily));
        // 获取指定列数据
        // get.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
        Result result = table.get(get);
        showCell(result);
        table.close();
        close();
    }

    /**
     * 删除指定内容
     * @param tableName
     * @param rowkey
     * @param colFamily
     * @param col
     * @throws IOException
     */
    public void deleRow(String tableName, String rowkey, String colFamily, String col) throws IOException {
        Table table = getTable(tableName);
        Delete delete = new Delete(Bytes.toBytes(rowkey));
        // 删除指定列族
        // delete.addFamily(Bytes.toBytes(colFamily));
        // 删除指定列
        // delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
        table.delete(delete);
        // 批量删除
        /*
         * List<Delete> deleteList = new ArrayList<Delete>();
         * deleteList.add(delete); table.delete(deleteList);
         */
        table.close();
        close();
    }

    /**
     * 获取一个表所有内容
     * @param tableName
     * @param startRow
     * @param stopRow
     * @throws IOException
     */
    public void scanData(String tableName, String startRow, String stopRow) throws IOException {
        Table table = getTable(tableName);
        Scan scan = new Scan();
        // scan.setStartRow(Bytes.toBytes(startRow));
        // scan.setStopRow(Bytes.toBytes(stopRow));
        ResultScanner resultScanner = table.getScanner(scan);
        for (Result result : resultScanner) {
            showCell(result);
        }
        table.close();
        close();
    }

    /**
     * 关闭连接
     */
    public  void close() {
        try {
            if (null != admin){
                admin.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 格式化输出
     * @param result
     */
    public void showCell(Result result) {
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            System.out.print("RowName:" + new String(CellUtil.cloneRow(cell)) + " ");
            System.out.print(",Timetamp:" + cell.getTimestamp() + " ");
            System.out.print(",column Family:" + new String(CellUtil.cloneFamily(cell)) + " ");
            System.out.print(",row Name:" + new String(CellUtil.cloneQualifier(cell)) + " ");
            System.out.print(",value:" + new String(CellUtil.cloneValue(cell)) + " ");
            System.out.println();
        }
    }
    public static void main(String[] args) {
        //这是测试
//        HTable table = HBaseUtils.getInstance().getTable("imooc_course_clickcount");
//        System.out.println(table.getName().getNameAsString());
//        String tableName = "imooc_course_clickcount" ;
//        String rowkey = "20171111_90";
//        String cf = "info" ;
//        String column = "click_count";
//        String value = "2";
//        HBaseUtils.getInstance().put(tableName, rowkey, cf, column, value);
//        System.out.println("插入一条数据");
        try {
            HBaseUtils.getInstance().scanData("imooc_course_clickcount", "20171111_90","20171111_90");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/sinat_36748650/article/details/90726373
今日推荐