HBase的java API 开发案例

五、java api操作

        导入开发包

                将hbase安装包中lib下包导入java项目

 

        创建表

               

                Configuration conf = HBaseConfiguration.create();

        conf.set("hbase.zookeeper.quorum","CentOS01:2181,CentOS02:2181,CentOS03:2181");

 

                HBaseAdmin admin = new HBaseAdmin(conf);

 

                HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("tabe"));

                HColumnDescriptor hcd_fam1 = new HColumnDescriptor("fam1");

                hcd_fam1.setMaxVersions(3);

                HColumnDescriptor hcd_fam2 = new HColumnDescriptor("fam2");

                htd.addFamily(hcd_fam1);

                htd.addFamily(hcd_fam2);

 

                admin.createTable(htd);

 

                admin.close();

       

 

        插入数据

                Configuration conf = HBaseConfiguration.create();

        conf.set("hbase.zookeeper.quorum","CentOS01:2181,CentOS02:2181,CentOS03:2181");

               

                HTable table = new HTable(conf,"tabe");

                Put put = new Put(Bytes.toBytes("row1"));

        put.add(Bytes.toBytes("fam1"),Bytes.toBytes("col1"),Bytes.toBytes("val1"));

        put.add(Bytes.toBytes("fam1"),Bytes.toBytes("col2"),Bytes.toBytes("val2"));

        put.add(Bytes.toBytes("fam2"),Bytes.toBytes("col3"),Bytes.toBytes("val3"));

                table.put(put);

               

                table.close();

 

       

 

                **javaapi操作hbase时,入口类为HTable,此对象创建时需要扫描.META表,以及其他操作,这非常耗时,所以,应该将该对象设置为单例,复用该对象,如果需要多个HTable对象,应该使用HTable

                Pool,通过对象池复用对象。

                        HTablePool pool = new HTablePool(conf,10);//不知道为什么过时了?

                **hbase所有修改数据的操作都保证了行级别的原子性,

 

                试验:一次插入100万条数据

                        HTable table = new HTable(conf,"tabx");

                        List<Put> puts = new ArrayList<Put>();

                        for(int i=1;i<=1000000;i++){

                                Put put = new Put(Bytes.toBytes("row"+i));

                        put.add(Bytes.toBytes("fam1"),Bytes.toBytes("col1"),Bytes.toBytes("val"+i))

                                puts.add(put);

 

                                if(i % 10000 == 0){

                                        table.put(puts);

                                        puts = new ArrayList<Put>();

                                }

                        }

                        table.put(puts);

                        table.close();

 

 

        获取数据

                Configuration conf = HBaseConfiguration.create();

        conf.set("hbase.zookeeper.quorum","CentOS01:2181,CentOS02:2181,CentOS03:2181");

               

                HTable table = new HTable(conf,"tabe");

                Get get = new Get(Bytes.toBytes("row1"));

                Result result = table.get(get);

                byte [] bs = result.getValue(Bytes.toBytes("fam1"),Bytes.toBytes("col1"));

                String str = Bytes.toString(bs);

                System.out.println(str);

               

                table.close();

       

 

        获取数据集

                Configuration conf = HBaseConfiguration.create();

        conf.set("hbase.zookeeper.quorum","CentOS01:2181,CentOS02:2181,CentOS03:2181");

       

                HTable table = new HTable(conf,"tabe");

                Scan scan = new Scan(Bytes.toBytes("row1"));

                ResultScanner scanner = table.getScanner(scan);

                Iterator it = scanner.iterator();

                while(it.hasNext()){

                        Result result = (Result) it.next();

                        byte [] bs = result.getValue(Bytes.toBytes("fam1"),Bytes.toBytes("col1"));

                        String str = Bytes.toString(bs);

                        System.out.println(str);

                }

                table.close();

       

        删除数据

                Configuration conf = HBaseConfiguration.create();

        conf.set("hbase.zookeeper.quorum","CentOS01:2181,CentOS02:2181,CentOS03:2181");

       

                HTable table = new HTable(conf,"tabe");

                Delete delete = new Delete(Bytes.toBytes("row1"));

                table.delete(delete);

                table.close();

               

        删除表

               

                //1.创建配置对象

                HBaseConfiguration conf = new HBaseConfiguration();

                conf.set("hbase.zookeeper.quorum", "CentOS01");

                //2.创建HBaseAdmin对象

                HBaseAdmin admin = new HBaseAdmin(conf);

                //3.删除表

                admin.disableTable(Bytes.toBytes("tab1"));

                admin.deleteTable(Bytes.toBytes("tab1"));

                //4.关闭连接

                admin.close();

 

        HBase的高级查询

               

                (1)实现范围查询

                        如果只设置scan但是不做任何限制 则查询所有数据

                                Scan scan = new Scan();

                        如果设置scan并且设置scan的扫描开始和结束为止则查询范围数据 注意含头不含尾

                                Scan scan = new Scan();

                                scan.setStartRow("rk2".getBytes());

                                scan.setStopRow("rk4".getBytes());

                (2)过滤器实现过滤查询

                        在scan上提供了方法来实现过滤查询

                                Scan scan = new Scan();

                                Filter filter = ...

                                scan.setFilter(filter)

                        Hbase内置器

                                HBase为筛选数据提供了一组过滤器,通过这个过滤器可以在HBase中的数据的多个维度(行,列,数据版本)上进行对数据的筛选操作,也就是说过滤器最终能够筛选的数据能够细化到具体的一个存储单元格上(由行键,列明,时间戳定位)。

                                Filter filter = new RowFilter(CompareOp.GREATER_OR_EQUAL,new BinaryComparator("rk3".getBytes()));

                                //--RowFilter配合正则过滤器 可以通过正则表达式从hbase表中筛选所有行键符合正则的数据

                                !!Filter filter = new RowFilter(CompareOp.EQUAL,new RegexStringComparator("^[^x]*x[^x]*$"));

                                Filter filter = new PrefixFilter("rkx".getBytes());

                                Filter filter = new KeyOnlyFilter();

                                Filter filter = new RandomRowFilter(0.2f);

                                Filter filter = new InclusiveStopFilter("rk4".getBytes());

                                Filter filter = new FirstKeyOnlyFilter();

                                //--ColumnPrefixFilter可以实现按照列的前缀过滤数据

                                !!Filter filter = new ColumnPrefixFilter("c2".getBytes());

                                //--ValueFilter可以按照值来过滤数据

                                !!Filter filter = new ValueFilter(CompareOp.EQUAL,new RegexStringComparator("^[^2]*2.*$"));

                                //--SingleColumnValueFilter按照某一个指定列的值决定该行是否返回

                                !!Filter filter = new SingleColumnValueFilter("cf1".getBytes(), "c1".getBytes(), CompareOp.EQUAL, new RegexStringComparator("^[^3]*3.*$"));

                                //--FilterList 可以将多个过滤器的效果合并起作用

                                !!Filter f1 = new RowFilter(CompareOp.EQUAL,new RegexStringComparator("^rk\\d+$"));

                                !!Filter f2 = new KeyOnlyFilter();

                                !!FilterList fl = new FilterList(Operator.MUST_PASS_ALL, f1,f2);

                                scan.setFilter(fl);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值