表的预切割,版本数,原生扫描,TTL,计数器

本文详细介绍了HBase中表的创建、预切割、版本控制、数据查询、删除及计数器等核心功能,展示了如何通过HBase Shell命令与Java API实现数据的高效管理和操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

预先切割
---------------
    创建表时,预先对表进行切割。
    切割线是rowkey.
    $hbase>create 'ns1:t2','f1',SPLITS=>['row3000','row6000']


创建表时指定列族的版本数,该列族的所有列都具有相同数量版本
-----------------------------------------------------------
    $hbase>create 'ns1:t3',{NAME=>'f1',VERSIONS=>3}            //创建表时,指定列族的版本数。
    $hbase>get 'ns1:t3','row1',{COLUMN=>'f1',VERSIONS=>4}    //检索的时候,查询多少版本。

@Test/**  * @Description:按照指定版本数进行查询  * @param ${tags}  * @return ${return_type}  * @throws  * @author 邹培贤  * @date 2018/7/21 11:04  */public void getWithVersions() throws IOException {    Configuration conf = HBaseConfiguration.create();    Connection conn = ConnectionFactory.createConnection(conf);    TableName tableName = TableName.valueOf("ns1:t3");    Table table = conn.getTable(tableName);    Get get = new Get(Bytes.toBytes("row1"));    //设置要检查的版本数(setMaxVersions(可以设置要检查的版本数,不设置默认最大值2147483647))    get.setMaxVersions();    Result result = table.get(get);    List<Cell> cells = result.getColumnCells(Bytes.toBytes("f1"), Bytes.toBytes("name"));    for(Cell c:cells){        String f=Bytes.toString(c.getFamily());        String col=Bytes.toString(c.getQualifier());        long time=c.getTimestamp();        String val=Bytes.toString(c.getValue());        System.out.println(f + "/" + col + "/" + time + "=" + val);    }    }


原生扫描(专家拥有)
-------------
    1.原生扫描
        $hbase>scan 'ns1:t3',{COLUMN=>'f1',RAW=>true,VERSIONS=>10}        //包含标记了delete的数据

 

 


        
    2.删除数据
        $hbase>delete 'ns1:t3','row1','f1:name',1532094430085(时间戳)            //删除数据,标记为删除.

 

         

 

 

 

 

 

使用scan 'ns1:t3'扫面一下,发现没有数据,因为删除了最新事件的数据,所以小于该删除时间的数据都作废。

                                                                  
    3.TTL
        time to live ,存活时间。
        影响所有的数据,包括没有删除的数据。
        超过该时间,原生扫描也扫不到数据。
        $hbase>create 'ns1:tx' , {NAME=>'f1',TTL=>10,VERSIONS=>3}

   计数器
------------------------

有点像数据库的自增,没调用一次,加1,比如在统计网站点击量时候,不可能每次都get取值,然后加1再put。使用incr可以自行控制。
    $hbase>incr 'ns1:t8','row1','f1:click',步长(步长如果省略,默认加1)
    $hbase>get_counter 'ns1:t8','row1','f1:click'

     

 

 

           

 

 

 

 

 

@Test
    public void testIncr() throws IOException {

        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tname = TableName.valueOf("ns1:t8");
        Table t = conn.getTable(tname);
        Increment incr = new Increment(Bytes.toBytes("row1"));
        incr.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("daily"),1);
        incr.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("weekly"),10);
        incr.addColumn(Bytes.toBytes("f1"),Bytes.toBytes("monthly"),100);
        t.increment(incr);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值