您的位置:首页 > 其它

hbase api 1.0前后版本示例

2016-06-22 09:12 260 查看
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)));

            }

        }

      
4000
 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");

        

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hbase