大数据之HBaseAPI

编写HBaseAPI

判断表是否存在

public class HBASE_API {

    private static Configuration conf;

    static{
        //使用HbaseConfiguration的单例方法实例化
        conf = HBaseConfiguration.create();

        /**
         * "hbase.zookeeper.quorum" 在hbase的conf下 sist.xml下 hbase的name
         * 使用主机名之前,确保win的host配置文件中配置Linux对应的ip和主机映射
         * */
        conf.set("hbase.zookeeper.quorum", "bigdata111");

        //客户端  端口号
        conf.set("hbase.zookeeper.property.clientPort", "2181");

        //znode节点  在Hadoop的储存地址
        conf.set("zookeeper.znode.parent", "/hbase");
    }

    /**
     *判断表是否存在
     **/

    public static boolean is_table_exist(String table_name) throws IOException {
        //创建Hbase客户端
        Connection connection = ConnectionFactory.createConnection(conf);
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();

        //判断是否存在
         return admin.tableExists(Bytes.toBytes(table_name));
    }

   public static void main(String[] args) throws IOException {
        //判断表是否存在
        System.out.println(is_table_exist("aa"));

        //创建表
        createTable("idea1","cf1","cf2","cf3");

        //插入数据
        for (int i = 0; i < 100; i++){
            add_row_data("idea1",String.valueOf(i),"cf","name","wind"+ i);
        }

        //获取某一行指定列数据
        get_row_qualifier("idea1","1","cf","name");

        //获取某一行所有数据
        get_row("idea1","2");

        //获取所有数据
        get_all_rows("idea1");

        //删除多行数据
        delete_multi_row("idea1","1","2","3");

        //删除表
        drop_table("idea1");
    }
}

创建表

	/**
     * 创建表:表名,列簇(可以是多个)
     * */

    public static void createTable(String table_name, String...columnFamily) throws IOException {
        HBaseAdmin admin = new HBaseAdmin(conf);

        //判断表是否存在
        if(is_table_exist(table_name)){
            System.out.println(table_name+ "已存在");
        }else {
            //创建表属性对象,表名需要转换字节
            HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(table_name));
            //创建列簇
            for(String cf : columnFamily){
                hTableDescriptor.addFamily(new HColumnDescriptor(cf));
            }
            admin.createTable(hTableDescriptor);
            System.out.println(table_name+ "创建成功");
        }

    }

插入数据

	/**
     * 插入数据:表名,rowkey,列簇,列,value
     * */

    public static void add_row_data(String table_name, String row, String columnFamily,
                                    String column, String value) throws IOException {
        //创建HTable
        HTable hTable = new HTable(conf, table_name);
        //创建put对象
        Put put = new Put(Bytes.toBytes(row));
        //添加列簇,列,数据
        put.add(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(value));
        hTable.put(put);
        hTable.close();
        System.out.println(table_name+ "插入数据成功");
    }

获取某一行指定“列族:列”的数据

	/**
     * 获取某一行指定“列族:列”的数据
     * */

    public static void get_row_qualifier(String table_name, String row, String family, String qualifier) throws IOException {

        HTable hTable = new HTable(conf, table_name);

        Get get = new Get(Bytes.toBytes(row));
        get.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier));
        Result result = hTable.get(get);
        for (Cell cell : result.rawCells()){
            System.out.println("行键"+ Bytes.toString(result.getRow()));
            System.out.println("列簇"+ Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("列"+ Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("值"+ Bytes.toString(CellUtil.cloneValue(cell)));
        }
        System.out.println(table_name+ "插入数据完成");
    }

获取某一行所有数据

	/**
     *获取某一行所有数据
     * */

    public static void get_row(String table_name, String row) throws IOException {
        HTable hTable = new HTable(conf, table_name);
        Get get = new Get(Bytes.toBytes(row));
        //显示所有版本
        //get.setMaxVersions();
        //显示指定时间戳的版本
        //get.setTimeStamp();

        Result result = hTable.get(get);
        for (Cell cell : result.rawCells()){
            System.out.println("行键"+ Bytes.toString(result.getRow()));
            System.out.println("列簇"+ Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("列"+ Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("值"+ Bytes.toString(CellUtil.cloneValue(cell)));
            System.out.println("时间戳"+ cell.getTimestamp());
        }

    }

获取某一行所有数据

	 /**
     * 得到所有数据
     * */

    public static void get_all_rows(String table_name) throws IOException {
        HTable hTable = new HTable(conf,table_name);
        //得到用于扫描 region 的对象
        Scan scan = new Scan();
        //使用 HTable 得到 ResultScanner 实现类的对象
        ResultScanner scanner = hTable.getScanner(scan);
        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)));
            }
        }
    }

删除多行数据

	 /**
     * 删除多行数据
     * */

    public static void delete_multi_row(String table_name, String...rows) throws IOException {
        HTable hTable = new HTable(conf, table_name);
        List<Delete> deletes = new ArrayList<Delete>();
        for (String row : rows){
            Delete delete = new Delete(Bytes.toBytes(row));
            deletes.add(delete);
            hTable.delete(deletes);
            hTable.close();

        }
    }

删除表

	/**
     * 删除表
     * */

    public static void drop_table(String table_name) throws IOException {
        HBaseAdmin admin = new HBaseAdmin(conf);

        //判断表是否存在
        if (is_table_exist(table_name)){
            //停用表
            admin.disableTable(table_name);
            //删除表
            admin.deleteTable(table_name);
            System.out.println(table_name+ "已删除");
        }else {
            System.out.println(table_name+ "已存在");
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值