第三章: hbase api 代码

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;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * HBaseAdmin :管理表(创建,删除)
 *      HTableDescriptor:表描述器,用于创建表
 *      HColumnDescriptor: 列描述器(构建列族)
 *
 * HTable:    用于表中数据的操作
 *      Put:      用于封装待存放的数据
 *      Delete:   用于封装带删除的数据
 *      Get:      用于得到某一个具体的数据
 *
 * Scan: 用于扫描表的配置信息
 * ResultScanner: 通过配置的扫描器,得到一个扫描表的实例扫描器
 * Cell: 用于封装一个rowkey下面所有单元格中的数据(rowkey,cf,cn,value)
 */
public class HBaseDemo {

    public static Configuration conf;
    public static ExecutorService pool;

    static {
        conf = HBaseConfiguration.create();
        pool = Executors.newFixedThreadPool(3);
    }

    /**
     * 判断表是否存在
     */
    public static boolean isExist(String tableName) throws Exception {
        HBaseAdmin admin = new HBaseAdmin(conf);
        return admin.tableExists(tableName);
    }

    /**
     * 创建表
     */
    public static void createTable(String tableName, String... columnFamily) throws Exception {
        if(isExist(tableName)){
            System.out.println("表已存在!");
        }else{
            HBaseAdmin admin = new HBaseAdmin(conf);
            HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
            for(String cf : columnFamily){
                HColumnDescriptor hcd = new HColumnDescriptor(cf);
                htd.addFamily(hcd);
            }
            admin.createTable(htd);
            System.out.println("表创建成功!");
        }
    }

    /**
     * HBase表的删除
     */
    public static void deleteTable(String tableName) throws Exception {
        if(isExist(tableName)){
            HBaseAdmin admin = new HBaseAdmin(conf);
            if(admin.isTableAvailable(TableName.valueOf(tableName))){
                admin.disableTable(TableName.valueOf(tableName));
            }
            admin.deleteTable(TableName.valueOf(tableName));
            System.out.println("表删除成功!");
        }else{
            System.out.println("表不存在!");
        }
    }

    /**
     * 添加数据
     */
    public static void addRow(String tableName,
                              String rowKey, String cf,
                              String column, String value) throws Exception {

        HTable hTable = new HTable(conf, tableName);
        hTable.setAutoFlush(false, true); //性能优化,在批量插入时使用, 设置是否自动提交
        hTable.setWriteBufferSize(5 * 1024 * 1024); //性能优化,在批量插入时使用, 设置buffer大小
        Put put = new Put(Bytes.toBytes(rowKey));
        put.setDurability(Durability.USE_DEFAULT);//性能优化,在批量插入时使用, 是否记录HLog日志
        put.add(Bytes.toBytes(cf), Bytes.toBytes(column), Bytes.toBytes(value));
        hTable.put(put);
    }

    /**
     * 添加数据,使用线程池
     */
    public static void addRow1(String tableName,
                              String rowKey, String cf,
                              String column, String value) throws Exception {

        HConnection hTablePool = getHTablePool();
        HTableInterface hTable = hTablePool.getTable(tableName.getBytes());

        hTable.setAutoFlush(false, true); //性能优化,在批量插入时使用, 设置是否自动提交
        hTable.setWriteBufferSize(5 * 1024 * 1024); //性能优化,在批量插入时使用, 设置buffer大小
        Put put = new Put(Bytes.toBytes(rowKey));
        put.setDurability(Durability.USE_DEFAULT);//性能优化,在批量插入时使用, 是否记录HLog日志
        put.add(Bytes.toBytes(cf), Bytes.toBytes(column), Bytes.toBytes(value));
        hTable.put(put);

        hTable.close();
        hTablePool.close();
    }

    /**
     * 删除一行数据
     */
    public static void delRow(String tableName, String rowKey,
                              String cf) throws Exception {
        HTable hTable = new HTable(conf, tableName);
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        hTable.delete(delete);
    }

    /**
     * 删除多行数据
     */
    public static void deleteMulitiRow(String tableName, String... rowKey) throws Exception {
        HTable hTable = new HTable(conf, tableName);
        List<Delete> deletes = new ArrayList<Delete>(rowKey.length);
        for(String row : rowKey){
            Delete delete = new Delete(Bytes.toBytes(row));
            deletes.add(delete);
        }
        hTable.delete(deletes);
    }


    public static void getAllRows(String tableName) throws Exception {
        HTable hTable = new HTable(conf, tableName);
        Scan scan = new Scan();
        scan.setCaching(30);//HBase scanner一次从服务端抓取的数据条数 , 性能优化
//        scan.addFamily(family)   //性能优化  可以减少网络传输数据量
        ResultScanner resultScanner = hTable.getScanner(scan);
        for(Result result : resultScanner){
//          System.out.println(Bytes.toString(result.getRow()));
            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)));
            }
        }
        resultScanner.close();//不关闭导致Server资源无法释放
    }

    public static void getRow(String tableName, String rowKey) throws Exception {
        HTable hTable = new HTable(conf, tableName);
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = hTable.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)));
        }
    }

    private static HConnection getHTablePool() throws IOException {
        HConnection connection = HConnectionManager.createConnection(conf, pool);
        return connection;
    }

    private static void shutDown(){
        pool.shutdown();
    }

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

//        System.out.println(HBaseDemo.isExist("student"));
//        createTable("staff","info1","info2");
//        deleteTable("student");
//        addRow("staff", "1000", "info1",
//                "name", "tom");
//        addRow("staff", "1001", "info1",
//                "age", "25");
//            delRow("staff", "1000", null);
//        deleteMulitiRow("staff","1000","1001");
//
//        getAllRows("staff");
//
//        getRow("staff","1000");

        addRow1("staff", "1111", "info1",
                "name", "tom");

        shutDown();
    }
}

  

posted on 2019-04-17 00:02  唯伊  阅读(78)  评论(0)    收藏  举报

导航