hbase api 1.0前后版本示例

HBase api的操作示例,0.98.x版本以后的语法有少许变化,



下面是详细例子


public class HBaseDAO{
    static Configuration conf = null;
    static {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "rhel124"); //config zookeeper
    }
    
    
    //ddl operation use admin
    //before version 1.0
    public void ddlBeforeVersionOne(){
        HBaseAdmin admin = null;
        try{
            admin = new HBaseAdmin(conf); //get admin method: new admin  
            if(admin.tableExists(tableName)){
                throw new Exception("table is exist");
            }
            
            HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
            for(int i=0; i<familys.length; i++){
                htd.addFamily(new HColumnDescriptor(familys[i]));
            }
            admin.createTable(htd);
            System.out.println("create table success");
        }finally{
            if(admin != null) admin.close();
        }
    }
    //after version 1.0
    public void ddlAfterVersionOne(){
        Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin = connection.getAdmin(); //get admin method: connection.getAdmin();
        try{
            if(admin.tableExists(TableName.valueOf(tableName))){
                throw new Exception("table is exist");
            }
            
            HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
            for(int i=0; i<familys.length; i++){
                htd.addFamily(new HColumnDescriptor(familys[i]));
            }
            admin.createTable(htd);
            System.out.println("create table success");
        }finally{
            if(admin != null) admin.close();
            System.out.println("create table complete, close HBaseAdmin");
        }
    }
    
    //dml operation use table
    //before version 1.0
    public static void testHbase()throws Exception{
        HTable table = new HTable(conf, "test_hbase_m");
        int n = 10000000;
        table.setAutoFlush(false);
        List<Put> puts = new ArrayList<Put>();
        long start = System.currentTimeMillis();
        for(int i=1;i<=n;i++){
            Put put = new Put(Bytes.toBytes(i+""));
            put.add(Bytes.toBytes("0"), Bytes.toBytes("name"), Bytes.toBytes("val"+i));
            puts.add(put);
            if(i%300000 == 0){
                System.out.println(i);
                table.put(puts);
                table.flushCommits();
                System.out.println("insert complete:"+(System.currentTimeMillis()-start)+"ms");
                puts.clear();
            }
            
        }
        
        
        
        //get
        Get get = new Get(Bytes.toBytes("1"));
        get.addColumn(Bytes.toBytes("0"), Bytes.toBytes("name"));
        long start = System.currentTimeMillis();
        Result result = table.get(get);
        System.out.println("get row:"+(System.currentTimeMillis()-start)+"ms");
        System.out.println(JSON.toString(result)+":"+(System.currentTimeMillis()-start)+"ms");
        
        //scan all
        Scan scan = new Scan();
        ResultScanner rs = null;
        start = System.currentTimeMillis();
        rs = table.getScanner(scan);
        System.out.println("scan all:"+(System.currentTimeMillis()-start)+"ms");
        for(Result r:rs){
            for(Cell cell:r.rawCells()){
//                System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
        System.out.println("scan all handle complete:"+(System.currentTimeMillis()-start)+"ms");
        //scan  filter
        RowFilter filter = new RowFilter(CompareOp.EQUAL,
            new RegexStringComparator("name"));
        scan.setFilter(filter);
        start = System.currentTimeMillis();
        rs = table.getScanner(scan);
        System.out.println("scan filter:"+(System.currentTimeMillis()-start)+"ms");
        for(Result r:rs){
            for(Cell cell:r.rawCells()){
//                System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
        System.out.println("scan filter handle complete:"+(System.currentTimeMillis()-start)+"ms");
        
    }
    
    //after version 1.0
    public static void dmlAfterVersionOne()throws Exception{
        Connection connection = ConnectionFactory.createConnection(conf);
        Table table = connection.getTable(TableName.valueOf("test_hbase_m")); //get table method: connection.getTable
        BufferedMutator mutator = connection.getBufferedMutator(TableName.valueOf("test_hbase_m")); //like table, use buffer, batch commit
        int n = 10000000;
        List<Put> puts = new ArrayList<Put>();
        long start = System.currentTimeMillis();
        for(int i=1;i<=n;i++){
            Put put = new Put(Bytes.toBytes(i+""));
            put.addColumn(Bytes.toBytes("0"), Bytes.toBytes("name"), Bytes.toBytes("val"+i));
            puts.add(put);
            if(i%300000 == 0){
                System.out.println(i);
//                table.put(puts);
                mutator.mutate(puts);
                mutator.flush();
                System.out.println("insert complete:"+(System.currentTimeMillis()-start)+"ms");
                puts.clear();
            }
            
        }
        
        
        
        //get
        Get get = new Get(Bytes.toBytes("1"));
        get.addColumn(Bytes.toBytes("0"), Bytes.toBytes("name"));
        start = System.currentTimeMillis();
        Result result = table.get(get);
        System.out.println("get row:"+(System.currentTimeMillis()-start)+"ms");
        System.out.println(JSON.toString(result)+":"+(System.currentTimeMillis()-start)+"ms");
        
        //scan all
        Scan scan = new Scan();
        ResultScanner rs = null;
        start = System.currentTimeMillis();
        rs = table.getScanner(scan);
        System.out.println("scan all:"+(System.currentTimeMillis()-start)+"ms");
        for(Result r:rs){
            for(Cell cell:r.rawCells()){
//                System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
        System.out.println("scan all handle complete:"+(System.currentTimeMillis()-start)+"ms");
        //scan  filter
        RowFilter filter = new RowFilter(CompareOp.EQUAL,
            new RegexStringComparator("name"));
        scan.setFilter(filter);
        start = System.currentTimeMillis();
        rs = table.getScanner(scan);
        System.out.println("scan filter:"+(System.currentTimeMillis()-start)+"ms");
        for(Result r:rs){
            for(Cell cell:r.rawCells()){
//                System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
        System.out.println("scan filter handle complete:"+(System.currentTimeMillis()-start)+"ms");
        
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值