java对hbase的基本操作,新版API实验

本文中的实验时基于hbase0.98版的,数据库的操作最基本的还是无异于增删改查,但是由于hbase对于很多像我一样的初接触者还是有很多不熟悉的地方,下面就自己对着前人经验以及最新API进行相关的介绍:


在具体展示代码之前有必要将相关的类进行一个整理,这些操作中主要涉及以下几种类型的类:

(1)配置:Configuration,HBaseConfiguration

(2)数据库标的设计也就是schema设计相关:HBaseAdmin、HTableDescriptor、HColumnDescriptor

(3)数据库表操作控制类:HTable(非线程安全)、HConnection、HTableInterface

(4)具体操作封装的类:Put、Delete、Get、Scan
具体类的用法可以参考hbase的API

1、创建数据库表

    /**
     * create table
     * @param tn  table name
     * @param cfs column families
     */
    public static void createTable(TableName tn, String cfs[])throws Exception{
        HBaseAdmin admin= new HBaseAdmin(conf);
        if(admin.tableExists(tn)){
            System.out.println(tn+" already exists !");
        }else{
            HTableDescriptor dsc =new HTableDescriptor(tn);
            int len = cfs.length;
            for(int i=0; i<len; ++i){
                HColumnDescriptor hc = new HColumnDescriptor(cfs[i]);
                dsc.addFamily(hc);
            }
            admin.createTable(dsc);
            System.out.print("# create table success !");
        }
    }

2、删除数据库表
 /**
     * delete table
     * @param tn table name
     */
    public static void deleteTable(TableName tn){
        try {
            HBaseAdmin admin = new HBaseAdmin(conf);
            admin.disableTable(tn);
            admin.deleteTable(tn);
            System.out.println("# delete table success!");
        } catch (Exception e) {
            System.out.print(e.getMessage()+"\n");
        }

    }

3、向数据库表中插入一行记录
    /**
     * insert a row record
     * @param tn table name
     * @param cfs
     * @param rowkey
     */
    public static void insertRow(TableName tn, String[] cfs, String rowkey) throws IOException{
        HConnection connection=null;
        HTableInterface htable=null;
        try {
            connection = HConnectionManager.createConnection(conf);
            htable = connection.getTable(tn);
            Put put = new Put(Bytes.toBytes(rowkey));
            int len = cfs.length;
            for(int i=0;i<len;++i){
                put.add(Bytes.toBytes(cfs[i]),Bytes.toBytes(String.valueOf(1)),Bytes.toBytes("value"));
                htable.put(put);
            }
        } catch (Exception e){
            System.out.print(e.getMessage()+"\n");
        }finally {
            htable.close();
            connection.close();
        }
    }

4、删除一行数据

    /**
     * delete a row
     * @param tn table name
     * @param rowkey
     * @throws IOException
     */
    public static void deleteRow(TableName tn, String rowkey) throws IOException {
        HConnection connection=  HConnectionManager.createConnection(conf);
        HTableInterface htable = connection.getTable(tn);
        try {
            Delete delete =new Delete(rowkey.getBytes());
            htable.delete(delete);
        } catch (Exception e){
            System.out.print(e.getMessage()+"\n");
        }finally {
            htable.close();
            connection.close();
        }
    }

5、数据库标的查询相关数据
    /**
     * select a row from table
     * @param tn table name
     * @param rowKey row key
     * @throws IOException
     */
    public static void selectRow(TableName tn, String rowKey)throws IOException {

        HConnection connection=  HConnectionManager.createConnection(conf);
        HTableInterface  htable = connection.getTable(tn);
        try {
            Get get= new Get(rowKey.getBytes());
            Result rs = htable.get(get);

            //new api
            CellScanner cs=rs.cellScanner();
            Cell cell =null;
            System.out.print("start printing row information !\n");
            while(cs.advance()){
                cell=cs.current();
                String str=new String(cell.getValueArray());//somthing wrong!
                System.out.println(cell.toString()+"  "+str);
            }

            //old api
            for (KeyValue kv : rs.raw()) {
                System.out.print(new String(kv.getRow()) + "  ");
                System.out.print(new String(kv.getFamily()) + ":");
                System.out.print(new String(kv.getQualifier()) + "  ");
                System.out.print(kv.getTimestamp() + "  ");
                System.out.println(new String(kv.getValue()));
            }

        } catch (Exception e){
            System.out.print(e.getMessage()+"\n");
        }finally {
            htable.close();
            connection.close();
        }
    }


    /**
     * select all rows form table
     * @param tn table name
     */
    public static void selectRows(TableName tn) {
        try {
            HConnection connection=  HConnectionManager.createConnection(conf);
            HTableInterface  htable = connection.getTable(tn);
            Scan s = new Scan();
            ResultScanner rs = htable.getScanner(s);
            for (Result r : rs) {
                KeyValue[] kv = r.raw();
                for (int i = 0; i < kv.length; i++) {
                    System.out.print(new String(kv[i].getRow()) + "  ");
                    System.out.print(new String(kv[i].getFamily()) + ":");
                    System.out.print(new String(kv[i].getQualifier()) + "  ");
                    System.out.print(kv[i].getTimestamp() + "  ");
                    System.out.println(new String(kv[i].getValue()));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值